Пример #1
0
 public static Task <Result <IPage, Error> > NewPage(this IBrowserContext context) =>
 context.NewPageAsync()
 .Map2(
     faulted =>
     Error <IPage, Error>($"Could not open a new page in the browser context because of {faulted.InnerException}"),
     succeeded =>
     Ok <IPage, Error>(succeeded));
Пример #2
0
        protected override async Task <IEnumerable <IDocument> > ExecuteInputAsync(IDocument input, IExecutionContext context)
        {
            var url  = _app.Urls.FirstOrDefault(u => u.StartsWith("http://"));
            var page = await _context.NewPageAsync().ConfigureAwait(false);

            var title       = input.GetString("Title");
            var description = input.GetString("Description");
            var highlights  = input.GetList <string>("Highlights") ?? Array.Empty <string>();

            await page.GotoAsync($"{url}/?title={title}&desc={description}&highlights={string.Join("||", highlights)}");

            // This will not just wait for the  page to load over the network, but it'll also give
            // chrome a chance to complete rendering of the fonts while the wait timeout completes.
            await page.WaitForLoadStateAsync(LoadState.NetworkIdle).ConfigureAwait(false);

            var bytes = await page.ScreenshotAsync().ConfigureAwait(false);

            await page.CloseAsync().ConfigureAwait(false);

            var destination = input.Destination.InsertSuffix("-social").ChangeExtension("png");
            var doc         = context.CreateDocument(
                input.Source,
                destination,
                new MetadataItems {
                { "DocId", input.Id }
            },
                context.GetContentProvider(bytes));

            return(new[] { doc });
        }
Пример #3
0
        private async Task <IPage> NavigateToPage(IBrowserContext browser, string listeningUri)
        {
            var page = await browser.NewPageAsync();

            await page.GoToAsync(listeningUri, LifecycleEvent.Networkidle);

            return(page);
        }
Пример #4
0
    private static async Task <IPage> NavigateToPage(IBrowserContext browser, string listeningUri)
    {
        var page = await browser.NewPageAsync();

        await page.GotoAsync(listeningUri, new() { WaitUntil = WaitUntilState.NetworkIdle });

        return(page);
    }
Пример #5
0
        /// <inheritdoc cref="IAsyncLifetime.InitializeAsync"/>
        public override async Task InitializeAsync()
        {
            _harPath = Path.Combine(_tempDir.Path, "test.har");
            _context = await Browser.NewContextAsync(recordHarPath : _harPath, ignoreHTTPSErrors : true);

            _page = await _context.NewPageAsync();

            await base.InitializeAsync();
        }
Пример #6
0
        public async Task <IPage> GetPage()
        {
            playwright = await Playwright.CreateAsync();

            browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
            {
                //Headless = false,
                //SlowMo = 50,
            });

            context = await browser.NewContextAsync();

            return(await context.NewPageAsync());
        }
Пример #7
0
        public async Task createBrowser()
        {
            playwright = await Playwright.CreateAsync();

            BrowserTypeLaunchOptions typeLaunchOptions = new BrowserTypeLaunchOptions {
                Headless = false
            };

            browser = await playwright.Chromium.LaunchAsync(typeLaunchOptions);

            context = await browser.NewContextAsync();

            page = await context.NewPageAsync();

            _objectContainer.RegisterInstanceAs(page);
        }
Пример #8
0
        private async Task <(IPage, IBrowserContext, System.Func <Task <dynamic> >)> PageWithHAR()
        {
            var             tmp     = new TempDirectory();
            var             harPath = Path.Combine(tmp.Path, "har.json");
            IBrowserContext context = await Browser.NewContextAsync(new() { RecordHarPath = harPath, IgnoreHTTPSErrors = true });

            IPage page = await context.NewPageAsync();

            async Task <dynamic> getContent()
            {
                await context.CloseAsync();

                var content = await File.ReadAllTextAsync(harPath);

                tmp.Dispose();
                return(JsonSerializer.Deserialize <dynamic>(content));
            };

            return(page, context, getContent);
        }
Пример #9
0
        public async Task Setup()
        {
            // Arrange before every test
            var webAppUrl = TestContext.Parameters["webAppUrl"];

            _enableVideo       = TestContext.Parameters["enableVideo"] != null && bool.Parse(TestContext.Parameters["enableVideo"]);
            _enableScreenshots = TestContext.Parameters["enableScreenshot"] != null && bool.Parse(TestContext.Parameters["enableScreenshot"]);
            _testResults       = new List <TestResult>();

            var playwright = await Playwright.CreateAsync();

            var chromium = playwright.Chromium;

            //set options
            _browser = await chromium.LaunchAsync(new BrowserTypeLaunchOptions
            {
#if DEBUG
                Headless = false,
                Devtools = true,
                SlowMo   = 1000
#endif
            });

            _context = await _browser.NewContextAsync(new BrowserNewContextOptions
            {
                RecordVideoDir = "videos/",
                BaseURL        = webAppUrl
            });

            _page = await _context.NewPageAsync();

            if (_enableVideo)
            {
                //a video (when enabled) is always made
                var videoPath = await _page.Video.PathAsync();

                _testResults.Add(new TestResult {
                    Description = "videorecording", Path = videoPath
                });
            }
        }