Exemplo n.º 1
0
        public async Task ShouldNotOverrideViewportSizeWhenPassedNull()
        {
            await using var browser = await BrowserType.LaunchAsync(TestConstants.GetHeadfulOptions());

            var context = await browser.NewContextAsync(new BrowserContextOptions { Viewport = null });

            var page = await context.NewPageAsync();

            await page.GoToAsync(TestConstants.EmptyPage);

            var popupTask = page.WaitForEventAsync(PageEvent.Popup);

            await TaskUtils.WhenAll(
                popupTask,
                page.EvaluateAsync(@"() => {
                    const win = window.open(window.location.href, 'Title', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=300,top=0,left=0');
                    win.resizeTo(500, 450);
                }"));

            var popup = popupTask.Result.Page;
            await popup.WaitForLoadStateAsync();

            await popup.WaitForFunctionAsync("() => window.outerWidth === 500 && window.outerHeight === 450");
        }
Exemplo n.º 2
0
        public async Task PageBringToFrontShouldWork()
        {
            await using var browser = await BrowserType.LaunchAsync(TestConstants.GetHeadfulOptions());

            var context = await browser.NewContextAsync(new BrowserContextOptions { Viewport = null });

            var page1 = await context.NewPageAsync();

            await page1.SetContentAsync("Page1");

            var page2 = await context.NewPageAsync();

            await page2.SetContentAsync("Page2");

            await page1.BringToFrontAsync();

            Assert.Equal("visible", await page1.EvaluateAsync <string>("document.visibilityState"));
            Assert.Equal("visible", await page2.EvaluateAsync <string>("document.visibilityState"));

            await page2.BringToFrontAsync();

            Assert.Equal("visible", await page1.EvaluateAsync <string>("document.visibilityState"));
            Assert.Equal("visible", await page2.EvaluateAsync <string>("document.visibilityState"));
        }
Exemplo n.º 3
0
        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");
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 /// <inheritdoc/>
 public PageSelectOptionTests(ITestOutputHelper output) : base(output)
 {
     DefaultOptions = TestConstants.GetHeadfulOptions();
 }
Exemplo n.º 7
0
        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();
        }
Exemplo n.º 8
0
        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 ShouldIsolateCookiesBetweenLaunches()
        {
            await using (var browser1 = await Playwright[TestConstants.Product].LaunchAsync(TestConstants.GetDefaultBrowserOptions()))
            {
                var context1 = await Browser.NewContextAsync();

                await Context.AddCookiesAsync(new SetNetworkCookieParam
                {
                    Name    = "cookie-in-context-1",
                    Value   = "value",
                    Expires = DateTimeOffset.Now.ToUnixTimeSeconds() + 10000,
                    Url     = TestConstants.EmptyPage
                });
            }

            await using (var browser2 = await Playwright[TestConstants.Product].LaunchAsync(TestConstants.GetDefaultBrowserOptions()))
            {
                var context1 = await Browser.NewContextAsync();

                var cookies = await context1.GetCookiesAsync();

                Assert.Empty(cookies);
            }
        }
        public async Task ShouldWork()
        {
            await using var browser = await Playwright[TestConstants.Product].LaunchAsync(TestConstants.GetDefaultBrowserOptions());

            const string func = "() => new Date(1479579154987).toString()";

            await using (var context = await browser.NewContextAsync(new BrowserContextOptions {
                TimezoneId = "America/Jamaica"
            }))
            {
                var page = await context.NewPageAsync();

                string result = await page.EvaluateAsync <string>(func);

                Assert.Equal(
                    "Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)",
                    result);
            }

            await using (var context = await browser.NewContextAsync(new BrowserContextOptions {
                TimezoneId = "Pacific/Honolulu"
            }))
            {
                var page = await context.NewPageAsync();

                Assert.Equal(
                    "Sat Nov 19 2016 08:12:34 GMT-1000 (Hawaii-Aleutian Standard Time)",
                    await page.EvaluateAsync <string>(func));
            }

            await using (var context = await browser.NewContextAsync(new BrowserContextOptions {
                TimezoneId = "America/Buenos_Aires"
            }))
            {
                var page = await context.NewPageAsync();

                Assert.Equal(
                    "Sat Nov 19 2016 15:12:34 GMT-0300 (Argentina Standard Time)",
                    await page.EvaluateAsync <string>(func));
            }

            await using (var context = await browser.NewContextAsync(new BrowserContextOptions {
                TimezoneId = "Europe/Berlin"
            }))
            {
                var page = await context.NewPageAsync();

                Assert.Equal(
                    "Sat Nov 19 2016 19:12:34 GMT+0100 (Central European Standard Time)",
                    await page.EvaluateAsync <string>(func));
            }
        }