public async Task ShouldDeleteDownloadsWhenPersistentContextCloses() { var userProfile = new TempDirectory(); var browser = await Playwright[TestConstants.BrowserName] .LaunchPersistentContextAsync(userProfile.Path, new() { AcceptDownloads = true, DownloadsPath = _tmp.Path }); var page = await browser.NewPageAsync(); await page.SetContentAsync($"<a href=\"{Server.Prefix}/download\">download</a>"); var download = await page.RunAndWaitForDownloadAsync(() => page.ClickAsync("a")); var path = await download.PathAsync(); Assert.IsTrue(File.Exists(path)); await browser.CloseAsync(); Assert.IsFalse(File.Exists(path)); }
public async Task ShouldRestoreCookiesFromUserDataDir() { using var userDataDir = new TempDirectory(); await using (var browserContext = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path)) { var page = await browserContext.NewPageAsync(); await page.GotoAsync(Server.EmptyPage); string documentCookie = await page.EvaluateAsync <string>(@"() => { document.cookie = 'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'; return document.cookie; }"); Assert.AreEqual("doSomethingOnlyOnce=true", documentCookie); } await using (var browserContext2 = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path)) { var page = await browserContext2.NewPageAsync(); await page.GotoAsync(Server.EmptyPage); Assert.AreEqual("doSomethingOnlyOnce=true", await page.EvaluateAsync <string>("() => document.cookie")); } using var userDataDir2 = new TempDirectory(); await using (var browserContext2 = await BrowserType.LaunchPersistentContextAsync(userDataDir2.Path)) { var page = await browserContext2.NewPageAsync(); await page.GotoAsync(Server.EmptyPage); Assert.That("doSomethingOnlyOnce=true", Is.Not.EqualTo(await page.EvaluateAsync <string>("() => document.cookie"))); } }
public async Task ShouldRoundTripThroughTheFile() { var page1 = await Context.NewPageAsync(); await page1.RouteAsync("**/*", (route) => { route.FulfillAsync(new() { Body = "<html></html>" }); }); await page1.GotoAsync("https://www.example.com"); await page1.EvaluateAsync(@"() => { localStorage['name1'] = 'value1'; document.cookie = 'username=John Doe'; }"); using var tempDir = new TempDirectory(); string path = Path.Combine(tempDir.Path, "storage-state.json"); string storage = await Context.StorageStateAsync(new() { Path = path }); Assert.AreEqual(storage, File.ReadAllText(path)); await using var context = await Browser.NewContextAsync(new() { StorageStatePath = path }); var page2 = await context.NewPageAsync(); await page2.RouteAsync("**/*", (route) => { route.FulfillAsync(new() { Body = "<html></html>" }); }); await page2.GotoAsync("https://www.example.com"); Assert.AreEqual("value1", await page2.EvaluateAsync <string>("localStorage['name1']")); Assert.AreEqual("username=John Doe", await page2.EvaluateAsync <string>("document.cookie")); }
public async Task ShouldSaveHar() { using var tempDirectory = new TempDirectory(); var harPath = tempDirectory.Path + "/test.har"; var browser = await BrowserType.ConnectAsync(_remoteServer.WSEndpoint); var context = await browser.NewContextAsync(new() { RecordHarPath = harPath }); var page = await context.NewPageAsync(); await page.GotoAsync(Server.EmptyPage); await context.CloseAsync(); await browser.CloseAsync(); Assert.That(harPath, Does.Exist); var logString = System.IO.File.ReadAllText(harPath); StringAssert.Contains(Server.EmptyPage, logString); }
public async Task ShouldHavePagesInPersistentContext() { using var harPath = new TempDirectory(); using var _persistentContextDir = new TempDirectory(); string harFilePath = Path.Combine(harPath.Path, "test.har"); var context = await BrowserType.LaunchPersistentContextAsync(_persistentContextDir.Path, recordHarPath : harFilePath); var page = context.Pages.FirstOrDefault(); await TaskUtils.WhenAll( page.GoToAsync("data:text/html,<title>Hello</title>"), page.WaitForLoadStateAsync(LoadState.DOMContentLoaded)); await context.CloseAsync(); var log = GetHarResult(harFilePath).Log; Assert.Single(log.Pages); var pageEntry = log.Pages.First(); Assert.Equal("page_0", pageEntry.Id); Assert.Equal("Hello", pageEntry.Title); }
public async Task ShouldRestoreStateFromUserDataDir() { using var userDataDir = new TempDirectory(); await using (var browserContext = await BrowserType.LaunchDefaultPersistentContext(userDataDir.Path)) { var page = await browserContext.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); await page.EvaluateAsync("() => localStorage.hey = 'hello'"); } await using (var browserContext2 = await BrowserType.LaunchDefaultPersistentContext(userDataDir.Path)) { var page = await browserContext2.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); Assert.Equal("hello", await page.EvaluateAsync <string>("() => localStorage.hey")); } using var userDataDir2 = new TempDirectory(); await using (var browserContext2 = await BrowserType.LaunchDefaultPersistentContext(userDataDir2.Path)) { var page = await browserContext2.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); Assert.NotEqual("hello", await page.EvaluateAsync <string>("() => localStorage.hey")); } userDataDir2.Dispose(); userDataDir.Dispose(); }