示例#1
0
        public async Task ShouldUseContextOptions()
        {
            var options = new BrowserNewContextOptions
            {
                Geolocation = new Geolocation
                {
                    Latitude  = 10,
                    Longitude = 10
                },
                Permissions = new[] { "geolocation" },
            };

            await using var context = await Browser.NewContextAsync(options);

            var page = await context.NewPageAsync();

            await page.GotoAsync(TestConstants.EmptyPage);

            var geolocation = await page.EvaluateAsync <Geolocation>(@"() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {
                resolve({latitude: position.coords.latitude, longitude: position.coords.longitude});
            }))");

            Assert.Equal(options.Geolocation.Latitude, geolocation.Latitude);
            Assert.Equal(options.Geolocation.Longitude, geolocation.Longitude);
        }
示例#2
0
    protected virtual BrowserNewContextOptions CreateContextOptions()
    {
        var options = new BrowserNewContextOptions()
        {
            IgnoreHTTPSErrors = true,
            Locale            = "en-GB",
            TimezoneId        = "Europe/London",
        };

        if (IsRunningInGitHubActions)
        {
            options.RecordVideoDir = VideosDirectory;
        }

        return(options);
    }
示例#3
0
        public async Task ShouldMakeACopyOfDefaultOptions()
        {
            var options = new BrowserNewContextOptions
            {
                UserAgent = "foobar"
            };

            await using var context = await Browser.NewContextAsync(options);

            options.UserAgent = "wrong";
            var page = await context.NewPageAsync();

            var(userAgent, _) = await TaskUtils.WhenAll(
                Server.WaitForRequest("/empty.html", request => request.Headers["User-Agent"].ToString()),
                page.GotoAsync(TestConstants.EmptyPage)
                );

            Assert.Equal("foobar", userAgent);
        }
示例#4
0
        public async Task ShouldUnroutePageWithBaseUrl()
        {
            var options = new BrowserNewContextOptions();

            options.BaseURL = Server.Prefix;

            await using var context = await Browser.NewContextAsync(options);

            var page = await context.NewPageAsync();

            var intercepted = new List <int>();

            await page.RouteAsync("/empty.html", (route) =>
            {
                intercepted.Add(1);
                route.ContinueAsync();
            });

            Action <IRoute> handler2 = (route) =>
            {
                intercepted.Add(2);
                route.ContinueAsync();
            };
            await page.RouteAsync("/empty.html", handler2);

            await page.GotoAsync(Server.EmptyPage);

            Assert.AreEqual(new List <int>()
            {
                2
            }, intercepted);

            intercepted.Clear();
            await page.UnrouteAsync("/empty.html", handler2);

            await page.GotoAsync(Server.EmptyPage);

            Assert.AreEqual(new List <int>()
            {
                1
            }, intercepted);
        }
示例#5
0
        public async Task ShouldNotModifyPassedDefaultOptionsObject()
        {
            var geolocation = new Geolocation {
                Latitude = 10, Longitude = 10
            };
            BrowserNewContextOptions options = new BrowserNewContextOptions {
                Geolocation = geolocation
            };

            await using var context = await Browser.NewContextAsync(options);

            await Page.GotoAsync(TestConstants.EmptyPage);

            await Context.SetGeolocationAsync(new Geolocation
            {
                Longitude = 20,
                Latitude  = 20
            });

            Assert.Equal(options.Geolocation.Latitude, geolocation.Latitude);
            Assert.Equal(options.Geolocation.Longitude, geolocation.Longitude);
        }
示例#6
0
    internal BrowserNewContextOptions ConfigureUniqueHarPath(BrowserNewContextOptions browserContextOptions)
    {
        var uploadDir = Environment.GetEnvironmentVariable("HELIX_WORKITEM_UPLOAD_ROOT");

        if (browserContextOptions?.RecordHarPath != null)
        {
            var identifier = Guid.NewGuid().ToString("N");
            browserContextOptions.RecordHarPath = Path.Combine(
                string.IsNullOrEmpty(uploadDir) ? browserContextOptions.RecordHarPath : uploadDir,
                $"{identifier}.har");
            _harPath = browserContextOptions.RecordHarPath;
        }

        if (browserContextOptions?.RecordVideoDir != null)
        {
            if (!string.IsNullOrEmpty(uploadDir))
            {
                browserContextOptions.RecordVideoDir = uploadDir;
            }
        }

        return(browserContextOptions);
    }
示例#7
0
 public static Task <Result <IBrowserContext, Error> > NewContext(this IBrowser browser, BrowserNewContextOptions browserNewContextOptions) =>
 browser.NewContextAsync(browserNewContextOptions)
 .Map2(
     faulted =>
     Error <IBrowserContext, Error>($"Could not open new browser context because of {faulted.InnerException}"),
     succeeded =>
     Ok <IBrowserContext, Error>(succeeded));
示例#8
0
 public BrowserNewContextOptions GetContextOptions(string browser, string contextName, BrowserNewContextOptions options) =>
 Combine(GetContextOptions(browser, contextName), options);