public async Task ShouldRestoreStateFromUserDataDir() { using var userDataDir = new TempDirectory(); await using (var browserContext = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path)) { var page = await browserContext.NewPageAsync(); await page.GotoAsync(Server.EmptyPage); await page.EvaluateAsync("() => localStorage.hey = 'hello'"); } await using (var browserContext2 = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path)) { var page = await browserContext2.NewPageAsync(); await page.GotoAsync(Server.EmptyPage); Assert.AreEqual("hello", await page.EvaluateAsync <string>("() => localStorage.hey")); } 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("hello", Is.Not.EqualTo(await page.EvaluateAsync <string>("() => localStorage.hey"))); } }
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, recordHar : new RecordHarOptions { Path = harFilePath }); var page = context.Pages[0]; await TaskUtils.WhenAll( page.GoToAsync("data:text/html,<title>Hello</title>"), page.WaitForLoadStateAsync(LifecycleEvent.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.LaunchPersistentContextAsync(userDataDir.Path, TestConstants.GetDefaultBrowserOptions())) { var page = await browserContext.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); await page.EvaluateAsync("() => localStorage.hey = 'hello'"); } await using (var browserContext2 = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path, TestConstants.GetDefaultBrowserOptions())) { 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.LaunchPersistentContextAsync(userDataDir2.Path, TestConstants.GetDefaultBrowserOptions())) { var page = await browserContext2.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); Assert.NotEqual("hello", await page.EvaluateAsync <string>("() => localStorage.hey")); } userDataDir2.Dispose(); userDataDir.Dispose(); }
public async Task ShouldWorkWithPersistentContext() { using var harFolder = new TempDirectory(); var harPath = Path.Combine(harFolder.Path, "har.json"); using var userDataDir = new TempDirectory(); var browserContext = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path, new() { RecordHarPath = harPath, }); var page = browserContext.Pages[0]; await page.GotoAsync("data:text/html,<title>Hello</title>"); // For data: load comes before domcontentloaded... await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded); await browserContext.CloseAsync(); var content = await File.ReadAllTextAsync(harPath); var log = JsonSerializer.Deserialize <dynamic>(content); Assert.AreEqual(1, log.GetProperty("log").GetProperty("pages").GetArrayLength()); var pageEntry = log.GetProperty("log").GetProperty("pages")[0]; Assert.AreEqual("Hello", pageEntry.GetProperty("title").ToString()); }
public async Task ShouldThrowIfPageArgumentIsPassed() { using var tmp = new TempDirectory(); var args = new[] { Server.EmptyPage }; await PlaywrightAssert.ThrowsAsync <PlaywrightException>(() => BrowserType.LaunchPersistentContextAsync(tmp.Path, new() { Args = args })); }
public async Task HeadlessShouldBeAbleToReadCookiesWrittenByHeadful() { using var userDataDir = new TempDirectory(); // Write a cookie in headful chrome var headfulOptions = TestConstants.GetHeadfulOptions(); headfulOptions.UserDataDir = userDataDir.Path; await using var headfulContext = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path, TestConstants.GetHeadfulOptions()); var headfulPage = await headfulContext.NewPageAsync(); await headfulPage.GoToAsync(TestConstants.EmptyPage); await headfulPage.EvaluateAsync("() => document.cookie = 'foo=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'"); await headfulContext.CloseAsync(); var headlessContext = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path, TestConstants.GetDefaultBrowserOptions()); var headlessPage = await headlessContext.NewPageAsync(); await headlessPage.GoToAsync(TestConstants.EmptyPage); string cookie = await headlessPage.EvaluateAsync <string>("() => document.cookie"); await headlessContext.CloseAsync(); Assert.Equal("foo=true", cookie); }
public async Task ShouldCaptureStaticPageInPersistentContext() { using var userDirectory = new TempDirectory(); using var tempDirectory = new TempDirectory(); var context = await BrowserType.LaunchPersistentContextAsync( userDirectory.Path, recordVideo : new RecordVideoOptions { Dir = tempDirectory.Path, Size = new ViewportSize { Width = 100, Height = 100 } }); var page = await context.NewPageAsync(); await page.EvaluateAsync("() => document.body.style.backgroundColor = 'red'"); await Task.Delay(1000); await context.CloseAsync(); Assert.NotEmpty(new DirectoryInfo(tempDirectory.Path).GetFiles("*.webm")); }
public async Task ShouldHaveDefaultUrlWhenLaunchingBrowser() { using var tempDir = new TempDirectory(); await using var browserContext = await BrowserType.LaunchPersistentContextAsync(tempDir.Path, TestConstants.GetHeadfulOptions()); string[] pages = browserContext.Pages.Select(page => page.Url).ToArray(); Assert.Equal(new[] { "about:blank" }, pages); }
public async Task ShouldConstructANewURLWhenABaseURLIsInPersistentContext() { using var dir = new TempDirectory(); await using var context = await BrowserType.LaunchPersistentContextAsync(dir.Path, new() { BaseURL = Server.Prefix }); var page = await context.NewPageAsync(); Assert.AreEqual(Server.EmptyPage, (await page.GotoAsync("/empty.html")).Url); }
private async Task <(TempDirectory tmp, IBrowserContext context, IPage page)> LaunchAsync(BrowserTypeLaunchPersistentContextOptions options = null) { var tmp = new TempDirectory(); var context = await BrowserType.LaunchPersistentContextAsync(tmp.Path, options); var page = context.Pages.First(); return(tmp, context, page); }
public async Task ShouldValidateFirefoxUserPrefs() { var options = TestConstants.GetDefaultBrowserOptions(); options.FirefoxUserPrefs = new Dictionary <string, object> { ["Foo"] = "Var" }; await Assert.ThrowsAsync <ArgumentException>(() => BrowserType.LaunchPersistentContextAsync("foo", options)); }
private async Task <(TempDirectory tmp, IBrowserContext context, IPage page)> LaunchAsync(BrowserContextOptions options = null) { var tmp = new TempDirectory(); var context = await BrowserType.LaunchPersistentContextAsync( tmp.Path, TestConstants.GetDefaultBrowserOptions().ToPersistentOptions() + (options ?? new BrowserContextOptions())); var page = context.Pages.First(); return(tmp, context, page); }
public async Task ShouldThrowIfPageArgumentIsPassed() { var tmp = new TempDirectory(); var options = TestConstants.GetDefaultBrowserOptions(); options.Args = new[] { TestConstants.EmptyPage }; await Assert.ThrowsAnyAsync <PlaywrightSharpException>(() => BrowserType.LaunchPersistentContextAsync(tmp.Path, options)); tmp.Dispose(); }
public async Task ShouldCloseBrowserWithBeforeunloadPage() { using var userDataDir = new TempDirectory(); await using var browserContext = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path, TestConstants.GetHeadfulOptions()); var page = await browserContext.NewPageAsync(); await page.GoToAsync(TestConstants.ServerUrl + "/beforeunload.html"); // We have to interact with a page so that 'beforeunload' handlers fire. await page.ClickAsync("body"); }
public async Task ShouldRestoreCookiesFromUserDataDir() { var userDataDir = new TempDirectory(); await using (var browserContext = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path, TestConstants.GetDefaultBrowserOptions())) { var page = await browserContext.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); string documentCookie = await page.EvaluateAsync <string>(@"() => { document.cookie = 'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT'; return document.cookie; }"); Assert.Equal("doSomethingOnlyOnce=true", documentCookie); } await using (var browserContext2 = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path, TestConstants.GetDefaultBrowserOptions())) { var page = await browserContext2.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); Assert.Equal("doSomethingOnlyOnce=true", await page.EvaluateAsync <string>("() => document.cookie")); } var userDataDir2 = new TempDirectory(); await using (var browserContext2 = await BrowserType.LaunchPersistentContextAsync(userDataDir2.Path, TestConstants.GetDefaultBrowserOptions())) { var page = await browserContext2.NewPageAsync(); await page.GoToAsync(TestConstants.EmptyPage); Assert.NotEqual("doSomethingOnlyOnce=true", await page.EvaluateAsync <string>("() => document.cookie")); } userDataDir2.Dispose(); userDataDir.Dispose(); }
public async Task ShouldReturnBackgroundPages() { using var userDataDir = new TempDirectory(); string extensionPath = TestUtils.GetWebServerFile("simple-extension"); var options = TestConstants.GetDefaultBrowserOptions(); options.Headless = false; options.Args = new[] { $"--disable-extensions-except={extensionPath}", $"--load-extension={extensionPath}", }; await using var context = await BrowserType.LaunchPersistentContextAsync(userDataDir.Path, options); var backgroundPage = ((IChromiumBrowserContext)context).BackgroundPages.Any() ? ((IChromiumBrowserContext)context).BackgroundPages.First() : (await context.WaitForEventAsync(ContextEvent.BackgroundPage)).Page; Assert.NotNull(backgroundPage); Assert.Contains(backgroundPage, ((IChromiumBrowserContext)context).BackgroundPages); Assert.DoesNotContain(backgroundPage, context.Pages); }
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"))); } }
private Task <IBrowserContext> LaunchPersistentHeaded(string path) { return(BrowserType.LaunchPersistentContextAsync(path, new() { Headless = false })); }