Ejemplo n.º 1
0
        protected override async Task BeforeExecutionAsync(IExecutionContext context)
        {
            var builder = WebApplication.CreateBuilder();

            builder.Logging.ClearProviders();

            builder.Services
            .AddRazorPages()
            .WithRazorPagesRoot("/src/SocialCards/");

            _app = builder.Build();
            _app.MapRazorPages();
            _app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(
                    Path.Combine(builder.Environment.ContentRootPath, "src/SocialCards")),
                RequestPath = "/static"
            });

            await _app.StartAsync().ConfigureAwait(false);

            _playwright = await Playwright.CreateAsync().ConfigureAwait(false);

            _browser = await _playwright.Chromium.LaunchAsync().ConfigureAwait(false);

            _context = await _browser.NewContextAsync(new BrowserNewContextOptions {
                ViewportSize = new ViewportSize {
                    Width = 1200, Height = 618
                },
            }).ConfigureAwait(false);
        }
Ejemplo n.º 2
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));
Ejemplo n.º 3
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);
    }
Ejemplo n.º 4
0
 /// <inheritdoc cref="IPage"/>
 internal Page(IPageDelegate pageDelegate, IBrowserContext browserContext)
 {
     FrameManager   = new FrameManager(this);
     Delegate       = pageDelegate;
     BrowserContext = browserContext;
     Keyboard       = new Keyboard(Delegate.RawKeyboard);
     Mouse          = new Mouse(Delegate.RawMouse, Keyboard);
 }
Ejemplo n.º 5
0
        private async Task <IPage> NavigateToPage(IBrowserContext browser, string listeningUri)
        {
            var page = await browser.NewPageAsync();

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

            return(page);
        }
Ejemplo n.º 6
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();
        }
Ejemplo n.º 7
0
        public ChromiumPage(ChromiumSession client, ChromiumBrowser browser, IBrowserContext browserContext)
        {
            Client          = client;
            _browser        = browser;
            _browserContext = browserContext;
            _networkManager = new ChromiumNetworkManager(Client, this);
            RawKeyboard     = new ChromiumRawKeyboard(client);
            RawMouse        = new ChromiumRawMouse(client);
            Page            = new Page(this, browserContext);

            client.MessageReceived += Client_MessageReceived;
        }
Ejemplo n.º 8
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());
        }
Ejemplo n.º 9
0
        /// <inheritdoc cref="IPage"/>
        internal Page(IPageDelegate pageDelegate, IBrowserContext browserContext)
        {
            FrameManager   = new FrameManager(this);
            Delegate       = pageDelegate;
            BrowserContext = browserContext;
            Keyboard       = new Keyboard(Delegate.RawKeyboard);
            Mouse          = new Mouse(Delegate.RawMouse, Keyboard);

            PageState = new PageState {
                Viewport = browserContext.Options.Viewport
            };

            Screenshotter = new Screenshotter(this);
        }
Ejemplo n.º 10
0
        public FirefoxPage(FirefoxSession session, IBrowserContext context, Func <Task <Page> > openerResolver)
        {
            _session        = session;
            _context        = context;
            _openerResolver = openerResolver;

            RawKeyboard     = new FirefoxRawKeyboard(session);
            RawMouse        = new FirefoxRawMouse(session);
            Page            = new Page(this, _context);
            _networkManager = new FirefoxNetworkManager(session, Page);

            session.MessageReceived += OnMessageReceived;
            Page.FrameDetached      += RemoveContextsForFrame;
        }
Ejemplo n.º 11
0
        internal ChromiumTarget(
            TargetInfo targetInfo,
            ChromiumBrowser browser,
            Func <Task <ChromiumSession> > sessionFactory,
            IBrowserContext browserContext)
        {
            TargetInfo      = targetInfo;
            _browser        = browser;
            _sessionFactory = sessionFactory;
            BrowserContext  = browserContext;
            PageTask        = null;

            _ = _initializedTaskWrapper.Task.ContinueWith(
                async initializedTask =>
            {
                bool success = initializedTask.Result;
                if (!success)
                {
                    return;
                }

                var openerPageTask = Opener?.PageTask;
                if (openerPageTask == null || Type != TargetType.Page)
                {
                    return;
                }

                var openerPage = await openerPageTask.ConfigureAwait(false);
                if (!openerPage.HasPopupEventListeners)
                {
                    return;
                }

                var popupPage = await PageAsync().ConfigureAwait(false);
                openerPage.OnPopup(popupPage);
            },
                CancellationToken.None,
                TaskContinuationOptions.RunContinuationsAsynchronously,
                TaskScheduler.Default);

            CloseTaskWrapper = new TaskCompletionSource <bool>(TaskCreationOptions.RunContinuationsAsynchronously);
            IsInitialized    = Type != TargetType.Page || !string.IsNullOrEmpty(TargetInfo.Url);

            if (IsInitialized)
            {
                _initializedTaskWrapper.TrySetResult(true);
            }
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
        public FirefoxTarget(FirefoxConnection connection, FirefoxBrowser firefoxBrowser, IBrowserContext context, string targetId, TargetInfoType type, string url, string openerId)
        {
            _connection = connection;
            _browser    = firefoxBrowser;
            _targetId   = targetId;
            _openerId   = openerId;

            BrowserContext = context;
            Type           = type switch
            {
                TargetInfoType.Browser => TargetType.Browser,
                TargetInfoType.Page => TargetType.Page,
                _ => throw new ArgumentOutOfRangeException(nameof(type))
            };
            Url = url;
        }
Ejemplo n.º 14
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);
        }
        /// <inheritsdoc/>
        public async Task InitializeAsync()
        {
            Server.SetRoute("/download", context =>
            {
                context.Response.Headers["Content-Type"]        = "application/octet-stream";
                context.Response.Headers["Content-Disposition"] = "attachment; filename=file.txt";
                return(context.Response.WriteAsync("Hello world"));
            });

            _downloadsPath = new TempDirectory();
            _userDataDir   = new TempDirectory();

            var options = TestConstants.GetDefaultBrowserOptions().ToPersistentOptions();

            options.DownloadsPath   = _downloadsPath.Path;
            options.AcceptDownloads = true;
            _context = await Playwright[TestConstants.Product].LaunchPersistentContextAsync(_userDataDir.Path, options);
            _page    = _context.Pages[0];
            await _page.SetContentAsync($"<a href=\"{TestConstants.ServerUrl}/download\">download</a>");
        }
Ejemplo n.º 16
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
                });
            }
        }
Ejemplo n.º 17
0
 /// <inheritdoc cref="IBrowser.GetTargets"/>
 public IEnumerable <ITarget> GetTargets(IBrowserContext context = null) => null;
Ejemplo n.º 18
0
 public async Task ContextSetup()
 {
     Context = await NewContext(ContextOptions()).ConfigureAwait(false);
 }
        /// <inheritdoc cref="IAsyncLifetime.InitializeAsync"/>
        public override async Task InitializeAsync()
        {
            await base.InitializeAsync();

            Context = await Browser.NewContextAsync();
        }
Ejemplo n.º 20
0
 /// <inheritdoc cref="IBrowser.GetTargets(IBrowserContext)" />
 public IEnumerable <ITarget> GetTargets(IBrowserContext context = null)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 21
0
 internal void Attach(IBrowserContext context)
 {
     context.Page += AttachToPage;
 }
Ejemplo n.º 22
0
 public Crawler(SearchClient searchClient, IBrowser browser, IBrowserContext browserContext)
 {
     SearchClient   = searchClient;
     Browser        = browser;
     BrowserContext = browserContext;
 }
 public LandingPageObject(IBrowserContext browser, IConfiguration configuration) : base(configuration)
 {
     Context = browser;
 }
Ejemplo n.º 24
0
 /// <inheritdoc cref="IBrowser.GetTargets"/>
 public IEnumerable <ITarget> GetTargets(IBrowserContext context = null)
 => context == null?GetAllTargets() : GetAllTargets().Where(t => t.BrowserContext == context);
        /// <inheritdoc cref="IAsyncLifetime.InitializeAsync"/>
        public virtual async Task InitializeAsync()
        {
            Context = await Browser.NewContextAsync();

            Context.DefaultTimeout = TestConstants.DefaultPuppeteerTimeout;
        }
Ejemplo n.º 26
0
 /// <inheritdoc cref="IAsyncLifetime.InitializeAsync"/>
 public virtual async Task InitializeAsync()
 {
     Context = await Browser.NewContextAsync();
 }