コード例 #1
0
        public async Task HeadlessShouldBeAbleToReadCookiesWrittenByHeadful()
        {
            using var userDataDir = new TempDirectory();

            // Write a cookie in headful chrome
            await using var headfulContext = await BrowserType.LaunchDefaultPersistentContext(userDataDir.Path, headless : false);

            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.LaunchDefaultPersistentContext(userDataDir.Path, headless : false);

            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);
        }
コード例 #2
0
        public async Task ShouldThrowIfPageArgumentIsPassed()
        {
            var tmp = new TempDirectory();
            await Assert.ThrowsAnyAsync <PlaywrightException>(() =>
                                                              BrowserType.LaunchDefaultPersistentContext(tmp.Path, new[] { TestConstants.EmptyPage }));

            tmp.Dispose();
        }
コード例 #3
0
        public async Task ShouldHaveDefaultUrlWhenLaunchingBrowser()
        {
            using var tempDir = new TempDirectory();
            await using var browserContext = await BrowserType.LaunchDefaultPersistentContext(tempDir.Path, headless : false);

            string[] pages = browserContext.Pages.Select(page => page.Url).ToArray();
            Assert.Equal(new[] { "about:blank" }, pages);
        }
コード例 #4
0
        private async Task <(TempDirectory tmp, IBrowserContext context, IPage page)> LaunchAsync(BrowserTypeLaunchPersistentContextOptions options = null)
        {
            var tmp     = new TempDirectory();
            var context = await BrowserType.LaunchDefaultPersistentContext(tmp.Path, null, options);

            var page = context.Pages.First();

            return(tmp, context, page);
        }
コード例 #5
0
        public async Task ShouldCloseBrowserWithBeforeunloadPage()
        {
            using var userDataDir          = new TempDirectory();
            await using var browserContext = await BrowserType.LaunchDefaultPersistentContext(userDataDir.Path, headless : false);

            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");
        }
コード例 #6
0
        public async Task ShouldRestoreCookiesFromUserDataDir()
        {
            var userDataDir = new TempDirectory();

            await using (var browserContext = await BrowserType.LaunchDefaultPersistentContext(userDataDir.Path))
            {
                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.LaunchDefaultPersistentContext(userDataDir.Path))
            {
                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.LaunchDefaultPersistentContext(userDataDir2.Path))
            {
                var page = await browserContext2.NewPageAsync();

                await page.GotoAsync(TestConstants.EmptyPage);

                Assert.NotEqual("doSomethingOnlyOnce=true", await page.EvaluateAsync <string>("() => document.cookie"));
            }

            userDataDir2.Dispose();
            userDataDir.Dispose();
        }
コード例 #7
0
        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();
        }