Skip to content

Commit

Permalink
Background page tests should wait for the page (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok authored Sep 17, 2019
1 parent a13dbee commit 39c3c2d
Showing 1 changed file with 14 additions and 34 deletions.
48 changes: 14 additions & 34 deletions lib/PuppeteerSharp.Tests/PuppeteerTests/HeadfulTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task BackgroundPageTargetTypeShouldBeAvailable()
TestConstants.LoggerFactory))
using (var page = await browserWithExtension.NewPageAsync())
{
var backgroundPageTarget = await WaitForBackgroundPageTargetAsync(browserWithExtension);
var backgroundPageTarget = await browserWithExtension.WaitForTargetAsync(t => t.Type == TargetType.BackgroundPage);
Assert.NotNull(backgroundPageTarget);
}
}
Expand All @@ -34,10 +34,12 @@ public async Task TargetPageShouldReturnABackgroundPage()
TestConstants.BrowserWithExtensionOptions(),
TestConstants.LoggerFactory))
{
var backgroundPageTarget = await WaitForBackgroundPageTargetAsync(browserWithExtension);
var page = await backgroundPageTarget.PageAsync();
Assert.Equal(6, await page.EvaluateFunctionAsync<int>("() => 2 * 3"));
Assert.Equal(42, await page.EvaluateFunctionAsync<int>("() => window.MAGIC"));
var backgroundPageTarget = await browserWithExtension.WaitForTargetAsync(t => t.Type == TargetType.BackgroundPage);
using (var page = await backgroundPageTarget.PageAsync())
{
Assert.Equal(6, await page.EvaluateFunctionAsync<int>("() => 2 * 3"));
Assert.Equal(42, await page.EvaluateFunctionAsync<int>("() => window.MAGIC"));
}
}
}

Expand All @@ -62,13 +64,13 @@ public async Task HeadlessShouldBeAbleToReadCookiesWrittenByHeadful()
var options = TestConstants.DefaultBrowserOptions();
options.Args = options.Args.Concat(new[] { $"--user-data-dir=\"{userDataDir}\"" }).ToArray();
options.Headless = false;

var browser = await launcher.LaunchAsync(options);
var page = await browser.NewPageAsync();
await page.GoToAsync(TestConstants.EmptyPage);
await page.EvaluateExpressionAsync(
"document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'");
await browser.CloseAsync();
using (var browser = await launcher.LaunchAsync(options))
using (var page = await browser.NewPageAsync())
{
await page.GoToAsync(TestConstants.EmptyPage);
await page.EvaluateExpressionAsync(
"document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'");
}

await TestUtils.WaitForCookieInChromiumFileAsync(userDataDir.Path, "foo");

Expand Down Expand Up @@ -145,27 +147,5 @@ public async Task BringToFrontShouldWork()
await newPage.CloseAsync();
}
}

private Task<Target> WaitForBackgroundPageTargetAsync(Browser browser)
{
var target = browser.Targets().FirstOrDefault(t => t.Type == TargetType.BackgroundPage);
if (target != null)
{
return Task.FromResult(target);
}
var targetCreatedTcs = new TaskCompletionSource<Target>();
void targetCreated(object sender, TargetChangedArgs e)
{
if (e.Target.Type != TargetType.BackgroundPage)
{
return;
}
targetCreatedTcs.TrySetResult(e.Target);
browser.TargetCreated -= targetCreated;
}
browser.TargetCreated += targetCreated;

return targetCreatedTcs.Task;
}
}
}

0 comments on commit 39c3c2d

Please sign in to comment.