File tree Expand file tree Collapse file tree 3 files changed +23
-6
lines changed
PuppeteerSharp.Tests/PuppeteerTests Expand file tree Collapse file tree 3 files changed +23
-6
lines changed Original file line number Diff line number Diff line change @@ -33,6 +33,20 @@ public async Task BackgroundPageTargetTypeShouldBeAvailable()
3333 }
3434 }
3535
36+ [ Fact ]
37+ public async Task TargetPageShouldReturnABackgroundPage ( )
38+ {
39+ using ( var browserWithExtension = await Puppeteer . LaunchAsync (
40+ TestConstants . BrowserWithExtensionOptions ( ) ,
41+ TestConstants . LoggerFactory ) )
42+ {
43+ var targets = browserWithExtension . Targets ( ) ;
44+ var backgroundPageTarget = targets . FirstOrDefault ( target => target . Type == TargetType . BackgroundPage ) ;
45+ var page = await backgroundPageTarget . PageAsync ( ) ;
46+ Assert . Equal ( 6 , await page . EvaluateFunctionAsync ( "() => 2 * 3" ) ) ;
47+ }
48+ }
49+
3650 [ Fact ]
3751 public async Task ShouldHaveDefaultUrlWhenLaunchingBrowser ( )
3852 {
Original file line number Diff line number Diff line change @@ -80,7 +80,7 @@ public Browser(
8080 private readonly BrowserContext _defaultContext ;
8181 private readonly ChromiumProcess _chromiumProcess ;
8282 private Task _closeTask ;
83-
83+
8484 #endregion
8585
8686 #region Properties
@@ -200,10 +200,13 @@ public BrowserContext[] BrowserContexts()
200200
201201 /// <summary>
202202 /// Returns a Task which resolves to an array of all open pages.
203+ /// Non visible pages, such as <c>"background_page"</c>, will not be listed here. You can find them using <see cref="Target.PageAsync"/>
203204 /// </summary>
204205 /// <returns>Task which resolves to an array of all open pages.</returns>
205206 public async Task < Page [ ] > PagesAsync ( )
206- => ( await Task . WhenAll ( Targets ( ) . Select ( target => target . PageAsync ( ) ) ) . ConfigureAwait ( false ) ) . Where ( x => x != null ) . ToArray ( ) ;
207+ => ( await Task . WhenAll (
208+ Targets ( ) . Where ( t => t . Type == TargetType . Page ) . Select ( target => target . PageAsync ( ) )
209+ ) . ConfigureAwait ( false ) ) . ToArray ( ) ;
207210
208211 /// <summary>
209212 /// Gets the browser's version
Original file line number Diff line number Diff line change @@ -13,8 +13,8 @@ public class Target
1313 {
1414 #region Private members
1515 private TargetInfo _targetInfo ;
16- private string _targetId ;
17- private Func < TargetInfo , Task < CDPSession > > _sessionFactory ;
16+ private readonly string _targetId ;
17+ private readonly Func < TargetInfo , Task < CDPSession > > _sessionFactory ;
1818 private Task < Page > _pageTask ;
1919 #endregion
2020
@@ -85,12 +85,12 @@ internal Target(
8585 #endregion
8686
8787 /// <summary>
88- /// Creates a new <see cref="Page"/>. If the target is not <c>"page"</c> returns <c>null</c>
88+ /// Creates a new <see cref="Page"/>. If the target is not <c>"page"</c> or <c>"background_page"</c> returns <c>null</c>
8989 /// </summary>
9090 /// <returns>a task that returns a new <see cref="Page"/></returns>
9191 public Task < Page > PageAsync ( )
9292 {
93- if ( _targetInfo . Type == TargetType . Page && _pageTask == null )
93+ if ( ( _targetInfo . Type == TargetType . Page || _targetInfo . Type == TargetType . BackgroundPage ) && _pageTask == null )
9494 {
9595 _pageTask = CreatePageAsync ( ) ;
9696 }
You can’t perform that action at this time.
0 commit comments