public async Task ShouldDeleteDownloadsOnBrowserGone() { var browser = await BrowserType.LaunchDefaultAsync(); var page = await browser.NewPageAsync(new BrowserNewPageOptions { AcceptDownloads = true }); await page.SetContentAsync($"<a href=\"{TestConstants.ServerUrl}/download\">download</a>"); var download1Task = page.WaitForEventAsync(PageEvent.Download); await TaskUtils.WhenAll( download1Task, page.ClickAsync("a")); var download2Task = page.WaitForEventAsync(PageEvent.Download); await TaskUtils.WhenAll( download2Task, page.ClickAsync("a")); string path1 = await download1Task.Result.PathAsync(); string path2 = await download2Task.Result.PathAsync(); Assert.True(new FileInfo(path1).Exists); Assert.True(new FileInfo(path2).Exists); await browser.CloseAsync(); Assert.False(new FileInfo(path1).Exists); Assert.False(new FileInfo(path2).Exists); Assert.False(new FileInfo(Path.Combine(path1, "..")).Exists); }
public async Task ShouldBeCallableTwice() { await using var browser = await BrowserType.LaunchDefaultAsync(); await TaskUtils.WhenAll(browser.CloseAsync(), browser.CloseAsync()); await browser.CloseAsync(); }
public async Task ShouldRejectAllPromisesWhenBrowserIsClosed() { await using var browser = await BrowserType.LaunchDefaultAsync(); var page = await(await browser.NewContextAsync()).NewPageAsync(); var neverResolves = page.EvaluateHandleAsync("() => new Promise(r => {})"); await browser.CloseAsync(); var exception = await Assert.ThrowsAsync <TargetClosedException>(() => neverResolves); Assert.Contains("Protocol error", exception.Message); }
public async Task ShouldFireCloseEventForAllContexts() { await using var browser = await BrowserType.LaunchDefaultAsync(); var context = await browser.NewContextAsync(); var closeTask = new TaskCompletionSource <bool>(); context.Close += (_, _) => closeTask.TrySetResult(true); await TaskUtils.WhenAll(browser.CloseAsync(), closeTask.Task); }
public async Task ShouldIsolateLocalStorageAndCookies() { // Create two incognito contexts. await using var browser = await BrowserType.LaunchDefaultAsync(); var context1 = await browser.NewContextAsync(); var context2 = await browser.NewContextAsync(); Assert.Empty(context1.Pages); Assert.Empty(context2.Pages); // Create a page in first incognito context. var page1 = await context1.NewPageAsync(); await page1.GoToAsync(TestConstants.EmptyPage); await page1.EvaluateAsync(@"() => { localStorage.setItem('name', 'page1'); document.cookie = 'name=page1'; }"); Assert.Single(context1.Pages); Assert.Empty(context2.Pages); // Create a page in second incognito context. var page2 = await context2.NewPageAsync(); await page2.GoToAsync(TestConstants.EmptyPage); await page2.EvaluateAsync(@"() => { localStorage.setItem('name', 'page2'); document.cookie = 'name=page2'; }"); Assert.Single(context1.Pages); Assert.Equal(page1, context1.Pages.FirstOrDefault()); Assert.Single(context2.Pages); Assert.Equal(page2, context2.Pages.FirstOrDefault()); // Make sure pages don't share localstorage or cookies. Assert.Equal("page1", await page1.EvaluateAsync <string>("() => localStorage.getItem('name')")); Assert.Equal("name=page1", await page1.EvaluateAsync <string>("() => document.cookie")); Assert.Equal("page2", await page2.EvaluateAsync <string>("() => localStorage.getItem('name')")); Assert.Equal("name=page2", await page2.EvaluateAsync <string>("() => document.cookie")); // Cleanup contexts. await TaskUtils.WhenAll(context1.CloseAsync(), context2.CloseAsync()); Assert.Empty(browser.Contexts); }
public async Task ShouldCreateNewContext() { await using var browser = await BrowserType.LaunchDefaultAsync(); Assert.Empty(browser.Contexts); await using var context = await browser.NewContextAsync(); Assert.Single(browser.Contexts); Assert.Contains(context, browser.Contexts); Assert.Same(browser, context.Browser); await context.CloseAsync(); Assert.Empty(browser.Contexts); Assert.Same(browser, context.Browser); }
public async Task ShouldWorkWitIgnoreHTTPSErrors() { await using var browser = await BrowserType.LaunchDefaultAsync(); var context = await browser.NewContextAsync(new BrowserContextOptions { IgnoreHTTPSErrors = true }); var page = await context.NewPageAsync(); await page.RouteAsync("**/*", (route) => route.ResumeAsync()); var response = await page.GoToAsync(TestConstants.HttpsPrefix + "/empty.html"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public async Task ShouldThrowIfPageArgumentIsPassed() { await Assert.ThrowsAnyAsync <PlaywrightSharpException>(() => BrowserType.LaunchDefaultAsync(args: new[] { TestConstants.EmptyPage })); }