public async Task ShouldSupportCustomWebSocket()
        {
            var options             = TestConstants.DefaultBrowserOptions();
            var customSocketCreated = false;

            options.WebSocketFactory = (uri, socketOptions, cancellationToken) =>
            {
                customSocketCreated = true;
                return(Connection.DefaultWebSocketFactory(uri, socketOptions, cancellationToken));
            };

            using (await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory))
            {
                Assert.True(customSocketCreated);
            }
        }
        public async Task ShouldSupportIgnoreHTTPSErrorsOption()
        {
            var options = TestConstants.DefaultBrowserOptions();

            options.IgnoreHTTPSErrors = true;

            using (var browser = await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory))
                using (var page = await browser.NewPageAsync())
                {
                    var response = await page.GoToAsync(TestConstants.HttpsPrefix + "/empty.html");

                    Assert.Equal(HttpStatusCode.OK, response.Status);
                    Assert.NotNull(response.SecurityDetails);
                    Assert.Equal("TLS 1.2", response.SecurityDetails.Protocol);
                }
        }
        public async Task ChromeShouldBeClosedOnDispose()
        {
            var options  = TestConstants.DefaultBrowserOptions();
            var launcher = new Launcher(TestConstants.LoggerFactory);

            using (var browser = await launcher.LaunchAsync(options))
                using (var page = await browser.NewPageAsync())
                {
                    var response = await page.GoToAsync(TestConstants.EmptyPage);

                    Assert.Equal(HttpStatusCode.OK, response.Status);
                }

            Assert.True(await launcher.Process.WaitForExitAsync(TimeSpan.FromSeconds(10)));
            Assert.True(launcher.Process.HasExited);
        }
        public async Task ShouldRejectAllPromisesWhenBrowserIsClosed()
        {
            using (var browser = await Puppeteer.LaunchAsync(
                       TestConstants.DefaultBrowserOptions(),
                       TestConstants.LoggerFactory))
                using (var page = await browser.NewPageAsync())
                {
                    var neverResolves = page.EvaluateFunctionHandleAsync("() => new Promise(r => {})");
                    await browser.CloseAsync();

                    var exception = await Assert.ThrowsAsync <EvaluationFailedException>(() => neverResolves);

                    Assert.IsType <TargetClosedException>(exception.InnerException);
                    Assert.Contains("Protocol error", exception.Message);
                }
        }
        public async Task ShouldSupportCustomTransport()
        {
            var customTransportCreated = false;
            var options = TestConstants.DefaultBrowserOptions();

            options.TransportFactory = (url, opt, cancellationToken) =>
            {
                customTransportCreated = true;
                return(WebSocketTransport.DefaultTransportFactory(url, opt, cancellationToken));
            };

            using (await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory))
            {
                Assert.True(customTransportCreated);
            }
        }
        public async Task ShouldTakeFullPageScreenshotsWhenDefaultViewportIsNull()
        {
            var options = TestConstants.DefaultBrowserOptions();

            options.DefaultViewport = null;

            using (var browser = await Puppeteer.LaunchAsync(options))
                using (var page = await browser.NewPageAsync())
                {
                    await page.GoToAsync(TestConstants.ServerUrl + "/grid.html");

                    Assert.NotEmpty(await page.ScreenshotDataAsync(new ScreenshotOptions {
                        FullPage = true
                    }));
                }
        }
        public async Task ShouldBeAbleToCloseRemoteBrowser()
        {
            var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions());

            var remoteBrowser = await Puppeteer.ConnectAsync(new ConnectOptions
            {
                BrowserWSEndpoint = originalBrowser.WebSocketEndpoint
            });

            var tcsDisconnected = new TaskCompletionSource <bool>();

            originalBrowser.Disconnected += (sender, e) => tcsDisconnected.TrySetResult(true);
            await Task.WhenAll(
                tcsDisconnected.Task,
                remoteBrowser.CloseAsync());
        }
示例#8
0
        public async Task ShouldThrowWhenTryingToConnectToNonExistingBrowser()
        {
            var options = TestConstants.DefaultBrowserOptions();

            options.Args = new string[] { "--remote-debugging-port=21222" };
            var originalBrowser = await Puppeteer.LaunchAsync(options);

            var browserURL = "http://127.0.0.1:2122";

            await Assert.ThrowsAsync <ProcessException>(() => Puppeteer.ConnectAsync(new ConnectOptions
            {
                BrowserURL = browserURL
            }));

            await originalBrowser.CloseAsync();
        }
        public async Task ShouldThrowWhenUsingBothBrowserWSEndpointAndBrowserURL()
        {
            var options = TestConstants.DefaultBrowserOptions();

            options.Args = new string[] { "--remote-debugging-port=21222" };
            var originalBrowser = await Puppeteer.LaunchAsync(options);

            var browserURL = "http://127.0.0.1:21222";

            await Assert.ThrowsAsync <PuppeteerException>(() => Puppeteer.ConnectAsync(new ConnectOptions
            {
                BrowserURL        = browserURL,
                BrowserWSEndpoint = originalBrowser.WebSocketEndpoint
            }));

            await originalBrowser.CloseAsync();
        }
示例#10
0
        public async Task ShouldHaveCustomUrlWhenLaunchingBrowser()
        {
            var options = TestConstants.DefaultBrowserOptions();

            options.Args = options.Args.Prepend(TestConstants.EmptyPage).ToArray();
            using (var browser = await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory))
            {
                var pages = await browser.PagesAsync();

                Assert.Single(pages);
                if (pages[0].Url != TestConstants.EmptyPage)
                {
                    await pages[0].WaitForNavigationAsync();
                }
                Assert.Equal(TestConstants.EmptyPage, pages[0].Url);
            }
        }
        public async Task ChromeShouldBeClosed()
        {
            var options  = TestConstants.DefaultBrowserOptions();
            var launcher = new Launcher();

            using (var browser = await launcher.LaunchAsync(options, TestConstants.ChromiumRevision))
                using (var page = await browser.NewPageAsync())
                {
                    var response = await page.GoToAsync(TestConstants.EmptyPage);

                    Assert.Equal(response.Status.ToString(), "OK");

                    await browser.CloseAsync();

                    Assert.True(launcher.IsChromeClosed);
                }
        }
        public async Task UserDataDirArgument()
        {
            var launcher    = new Launcher(TestConstants.LoggerFactory);
            var userDataDir = Launcher.GetTemporaryDirectory();
            var options     = TestConstants.DefaultBrowserOptions();

            options.Args = options.Args.Concat(new[] { $"--user-data-dir=\"{userDataDir}\"" }).ToArray();

            using (var browser = await launcher.LaunchAsync(options))
            {
                Assert.True(Directory.GetFiles(userDataDir).Length > 0);
                await browser.CloseAsync();

                Assert.True(Directory.GetFiles(userDataDir).Length > 0);
                await launcher.TryDeleteUserDataDir();
            }
        }
示例#13
0
        public async Task ShouldWorkInSlowMo()
        {
            var options = TestConstants.DefaultBrowserOptions();

            options.SlowMo   = 100;
            options.Headless = false;

            await using (var browser = await Puppeteer.LaunchAsync(options))
                await using (var page = await browser.NewPageAsync())
                {
                    await page.GoToAsync("https://duckduckgo.com/");

                    var input = await page.WaitForSelectorAsync("#search_form_input_homepage");

                    await input.TypeAsync("Lorem ipsum dolor sit amet.");
                }
        }
        public async Task ShouldSupportIgnoreHTTPSErrorsOption()
        {
            using (var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions()))
                using (var browser = await Puppeteer.ConnectAsync(new ConnectOptions
                {
                    BrowserWSEndpoint = originalBrowser.WebSocketEndpoint,
                    IgnoreHTTPSErrors = true
                }))
                    using (var page = await browser.NewPageAsync())
                    {
                        var response = await page.GoToAsync(TestConstants.HttpsPrefix + "/empty.html");

                        Assert.True(response.Ok);
                        Assert.NotNull(response.SecurityDetails);
                        Assert.Equal("TLS 1.2", response.SecurityDetails.Protocol);
                    }
        }
示例#15
0
        public async Task UserDataDirOption()
        {
            var launcher    = new Launcher(TestConstants.LoggerFactory);
            var userDataDir = Launcher.GetTemporaryDirectory();
            var options     = TestConstants.DefaultBrowserOptions();

            options.UserDataDir = userDataDir;

            using (var browser = await launcher.LaunchAsync(options))
            {
                Assert.True(Directory.GetFiles(userDataDir).Length > 0);
                await browser.CloseAsync();

                Assert.True(Directory.GetFiles(userDataDir).Length > 0);
                await launcher.TryDeleteUserDataDir();
            }
        }
示例#16
0
        public async Task ChromeShouldBeClosed()
        {
            var options  = TestConstants.DefaultBrowserOptions();
            var launcher = new Launcher(TestConstants.LoggerFactory);

            using (var browser = await launcher.LaunchAsync(options))
                using (var page = await browser.NewPageAsync())
                {
                    var response = await page.GoToAsync(TestConstants.EmptyPage);

                    Assert.Equal(HttpStatusCode.OK, response.Status);

                    await browser.CloseAsync();

                    Assert.True(launcher.IsChromeClosed);
                }
        }
示例#17
0
        public async Task ShouldSetTheDefaultViewport()
        {
            var options = TestConstants.DefaultBrowserOptions();

            options.DefaultViewport = new ViewPortOptions
            {
                Width  = 456,
                Height = 789
            };

            using (var browser = await Puppeteer.LaunchAsync(options))
                using (var page = await browser.NewPageAsync())
                {
                    Assert.Equal(456, await page.EvaluateExpressionAsync <int>("window.innerWidth"));
                    Assert.Equal(789, await page.EvaluateExpressionAsync <int>("window.innerHeight"));
                }
        }
示例#18
0
        public async Task ShouldNotOpenTwoChromesUsingTheSameLauncher()
        {
            var launcher = new Launcher(TestConstants.LoggerFactory);

            using (var browser = await launcher.LaunchAsync(
                       TestConstants.DefaultBrowserOptions(),
                       TestConstants.ChromiumRevision))
            {
                var exception = await Assert.ThrowsAsync <InvalidOperationException>(() =>
                {
                    return(launcher.LaunchAsync(
                               TestConstants.DefaultBrowserOptions(),
                               TestConstants.ChromiumRevision));
                });

                Assert.Equal("Unable to create or connect to another chromium process", exception.Message);
            }
        }
示例#19
0
        public async Task ShouldWorkInRealLifeWithOptions()
        {
            var options = TestConstants.DefaultBrowserOptions();

            using (var browser = await Puppeteer.LaunchAsync(options, TestConstants.LoggerFactory))
                using (var page = await browser.NewPageAsync())
                {
                    var response = await page.GoToAsync(
                        "https://www.google.com",
                        new NavigationOptions
                    {
                        Timeout   = 10000,
                        WaitUntil = new[] { WaitUntilNavigation.Networkidle0 }
                    });

                    Assert.Equal(HttpStatusCode.OK, response.Status);
                }
        }
示例#20
0
        public async Task NetworkRedirectsShouldReportSecurityDetails()
        {
            var options = TestConstants.DefaultBrowserOptions();

            options.IgnoreHTTPSErrors = true;

            using (var browser = await Puppeteer.LaunchAsync(options, TestConstants.ChromiumRevision, TestConstants.LoggerFactory))
                using (var page = await browser.NewPageAsync())
                {
                    var responses = new List <Response>();
                    page.Response += (sender, e) => responses.Add(e.Response);

                    await page.GoToAsync(TestConstants.HttpsPrefix + "/plzredirect");

                    Assert.Equal(2, responses.Count);
                    Assert.Equal(HttpStatusCode.Redirect, responses[0].Status);
                    var securityDetails = responses[0].SecurityDetails;
                    Assert.Equal("TLS 1.2", securityDetails.Protocol);
                }
        }
示例#21
0
        public async Task ShouldFailGracefullyWhenTryingToWorkWithFilechoosersWithinMultipleConnections()
        {
            // 1. Launch a browser and connect to all pages.
            var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions());

            await originalBrowser.PagesAsync();

            // 2. Connect a remote browser and connect to first page.
            var remoteBrowser = await Puppeteer.ConnectAsync(new ConnectOptions
            {
                BrowserWSEndpoint = originalBrowser.WebSocketEndpoint
            });

            var page = (await remoteBrowser.PagesAsync())[0];
            // 3. Make sure |page.waitForFileChooser()| does not work with multiclient.
            var ex = await Assert.ThrowsAsync <PuppeteerException>(() => page.WaitForFileChooserAsync());

            Assert.Equal("File chooser handling does not work with multiple connections to the same page", ex.Message);
            await originalBrowser.CloseAsync();
        }
示例#22
0
        public async Task UserDataDirArgument()
        {
            using (var userDataDir = new TempDirectory())
            {
                var launcher = new Launcher(TestConstants.LoggerFactory);
                var options  = TestConstants.DefaultBrowserOptions();
                options.Args = options.Args.Concat(new[] { $"--user-data-dir=\"{userDataDir}\"" }).ToArray();

                using (var browser = await launcher.LaunchAsync(options))
                {
                    // Open a page to make sure its functional.
                    await browser.NewPageAsync();

                    Assert.True(Directory.GetFiles(userDataDir.Path).Length > 0);
                    await browser.CloseAsync();

                    Assert.True(Directory.GetFiles(userDataDir.Path).Length > 0);
                }
            }
        }
        public async Task ShouldEmittedWhenBrowserGetsClosedDisconnectedOrUnderlyingWebsocketGetsClosed()
        {
            var originalBrowser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions(), TestConstants.LoggerFactory);

            var connectOptions = new ConnectOptions {
                BrowserWSEndpoint = originalBrowser.WebSocketEndpoint
            };
            var remoteBrowser1 = await Puppeteer.ConnectAsync(connectOptions, TestConstants.LoggerFactory);

            var remoteBrowser2 = await Puppeteer.ConnectAsync(connectOptions, TestConstants.LoggerFactory);

            var disconnectedOriginal = 0;
            var disconnectedRemote1  = 0;
            var disconnectedRemote2  = 0;

            originalBrowser.Disconnected += (sender, e) => ++ disconnectedOriginal;
            remoteBrowser1.Disconnected  += (sender, e) => ++ disconnectedRemote1;
            remoteBrowser2.Disconnected  += (sender, e) => ++ disconnectedRemote2;

            var remoteBrowser2Disconnected = WaitForBrowserDisconnect(remoteBrowser2);

            remoteBrowser2.Disconnect();
            await remoteBrowser2Disconnected;

            Assert.Equal(0, disconnectedOriginal);
            Assert.Equal(0, disconnectedRemote1);
            Assert.Equal(1, disconnectedRemote2);

            var remoteBrowser1Disconnected  = WaitForBrowserDisconnect(remoteBrowser1);
            var originalBrowserDisconnected = WaitForBrowserDisconnect(originalBrowser);

            await Task.WhenAll(
                originalBrowser.CloseAsync(),
                remoteBrowser1Disconnected,
                originalBrowserDisconnected
                );

            Assert.Equal(1, disconnectedOriginal);
            Assert.Equal(1, disconnectedRemote1);
            Assert.Equal(1, disconnectedRemote2);
        }
        public async Task ShouldRejectWaitForSelectorWhenBrowserCloses()
        {
            Server.SetRoute("/empty.html", _ => Task.Delay(10000));

            await using (var browser = await Puppeteer.LaunchAsync(TestConstants.DefaultBrowserOptions()))
            {
                var remote = await Puppeteer.ConnectAsync(new ConnectOptions
                {
                    BrowserWSEndpoint = browser.WebSocketEndpoint
                });

                var page = await remote.NewPageAsync();

                var watchdog = page.WaitForSelectorAsync("div", new WaitForSelectorOptions {
                    Timeout = 60000
                });
                remote.Disconnect();
                var exception = await Assert.ThrowsAsync <TargetClosedException>(() => watchdog);

                Assert.Equal("Connection disposed", exception.CloseReason);
            }
        }
示例#25
0
        public async Task ShouldAllowSyncPageMethod()
        {
            var options = TestConstants.DefaultBrowserOptions();

            if (!options.EnqueueAsyncMessages)
            {
                return;
            }

            await using (var browser = await Puppeteer.LaunchAsync(options))
            {
                // Async low-level use
                await using var page = await browser.NewPageAsync().ConfigureAwait(false);

                await page.GoToAsync("http://ipecho.net/plain", WaitUntilNavigation.DOMContentLoaded).ConfigureAwait(false);

                await page.SetContentAsync("<html><body>REPLACED</body></html>").ConfigureAwait(false);

                // Deep inside an existing mostly sync app...
                var content = page.GetContentAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                Assert.Contains("REPLACE", content);
            }
        }
        public async Task ShouldBeAbleToConnectUsingBrowserURLWithAndWithoutTrailingSlash()
        {
            var options = TestConstants.DefaultBrowserOptions();

            options.Args = new string[] { "--remote-debugging-port=21222" };
            var originalBrowser = await Puppeteer.LaunchAsync(options);

            var browserURL = "http://127.0.0.1:21222";

            var browser1 = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserURL = browserURL });

            var page1 = await browser1.NewPageAsync();

            Assert.Equal(56, await page1.EvaluateExpressionAsync <int>("7 * 8"));
            browser1.Disconnect();

            var browser2 = await Puppeteer.ConnectAsync(new ConnectOptions { BrowserURL = browserURL + "/" });

            var page2 = await browser2.NewPageAsync();

            Assert.Equal(56, await page2.EvaluateExpressionAsync <int>("7 * 8"));
            browser2.Disconnect();
            await originalBrowser.CloseAsync();
        }
示例#27
0
 public OOPIFTests(ITestOutputHelper output) : base(output)
 {
     DefaultOptions      = TestConstants.DefaultBrowserOptions();
     DefaultOptions.Args = new[] { "--site-per-process" };
 }
 public IgnoreHttpsErrorsTests(ITestOutputHelper output) : base(output)
 {
     DefaultOptions = TestConstants.DefaultBrowserOptions();
     DefaultOptions.IgnoreHTTPSErrors = true;
 }
 public ResponseSecurityDetailsTests(ITestOutputHelper output) : base(output)
 {
     DefaultOptions = TestConstants.DefaultBrowserOptions();
     DefaultOptions.IgnoreHTTPSErrors = true;
 }