public void Should_add_mediarange_as_accept_header_with_default_quality_when_accept_is_invoked_without_quality_parameter()
        {
            // Given
            var context = new BrowserContext(A.Fake<INancyEnvironment>());

            // When
            context.Accept("application/json");

            // Then
            ((IBrowserContextValues) context).Headers["accept"].First().ShouldEqual("application/json;q=1.0");
        }
        public void Should_remove_default_accept_header_when_accept_is_invoked_without_quality_parameter()
        {
            // Given
            var context = new BrowserContext();

            // When
            context.Accept("application/json");

            // Then
            ((IBrowserContextValues)context).Headers.Count.ShouldEqual(1);
        }
        public void Should_add_mediarange_with_supplied_quality_as_accept_header_with_default_quality_when_accept_is_invoked_with_quality_parameter()
        {
            // Given
            var context = new BrowserContext();

            // When
            context.Accept("application/json", 0.8m);

            // Then
            ((IBrowserContextValues)context).Headers["accept"].First().ShouldEqual("application/json;q=0.8");
        }
        public void Should_remove_default_accept_header_once_when_accept_is_invoked_with_quality_parameter()
        {
            // Given
            var context = new BrowserContext();

            // When
            context.Accept("application/json", 0.8m);
            context.Accept("*/*", 1.0m);
            context.Accept("application/xml", 0.5m);

            // Then
            ((IBrowserContextValues)context).Headers["accept"].Count().ShouldEqual(3);
        }
        public void Should_remove_default_accept_header_once_when_accept_is_invoked_without_quality_parameter()
        {
            // Given
            var context = new BrowserContext(A.Fake<INancyEnvironment>());

            // When
            context.Accept("application/json");
            context.Accept("*/*");
            context.Accept("application/xml");

            // Then
            ((IBrowserContextValues)context).Headers["accept"].Count().ShouldEqual(3);
        }
        public void Should_add_basic_authentication_credentials_to_the_headers_of_the_request()
        {
            // Given
            var context = new BrowserContext();

            // When
            context.BasicAuth("username", "password");

            // Then
            IBrowserContextValues values = context;

            var credentials        = string.Format("{0}:{1}", "username", "password");
            var encodedCredentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));

            Assert.Equal(1, values.Headers.Count);
            Assert.Equal("Basic " + encodedCredentials, values.Headers["Authorization"].First());
        }
Exemplo n.º 7
0
 public MainForm(BrowserContext browserContext)
 {
     InitializeComponent();
     _browserContext = browserContext;
     _browserContext.SendHandleMessage = () =>
     {
         if (InvokeRequired)
         {
             Invoke(new SendHandleMessageDelegate(_browserContext.SendHandleMessage));
         }
         else
         {
             Native.SendMessage(Handle, (int)Event.NclButtonDown, (int)Event.Caption, 0);
         }
     };
     Controls.Add(_browserContext);
 }
Exemplo n.º 8
0
        public async Task Should_add_basic_authentication_credentials_to_the_headers_of_the_request()
        {
            // Given
            var context = new BrowserContext(A.Fake <INancyEnvironment>());

            // When
            context.BasicAuth("username", "password");

            // Then
            IBrowserContextValues values = context;

            var credentials        = string.Format("{0}:{1}", "username", "password");
            var encodedCredentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));

            values.Headers["Authorization"].ShouldHaveCount(1);
            values.Headers["Authorization"].First().ShouldEqual("Basic " + encodedCredentials);
        }
Exemplo n.º 9
0
        public void Should_add_basic_authentication_credentials_to_the_headers_of_the_request()
        {
            // Given
            var context = new BrowserContext(A.Fake<INancyEnvironment>());

            // When
            context.BasicAuth("username", "password");

            // Then
            IBrowserContextValues values = context;

            var credentials = string.Format("{0}:{1}", "username", "password");
            var encodedCredentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));

            values.Headers["Authorization"].ShouldHaveCount(1);
            values.Headers["Authorization"].First().ShouldEqual("Basic " + encodedCredentials);
        }
Exemplo n.º 10
0
        public void TestarPesquisaNoGoogle(
            Browser browser, string texto)
        {
            var browserContext = new BrowserContext(browser, _configuration);



            using (var tela1 = new GooglePage(browserContext))
                using (var tela2 = new PesquisaGoogle(browserContext))
                {
                    tela1.CarregarPagina();
                    tela1.PreencheCampoDeTexto(texto);
                    tela1.SubmiterAPesquisa();

                    Assert.True(tela2.AcharBotao());
                }
        }
Exemplo n.º 11
0
        private async Task GetVkUserBrowser()
        {
            await userPageSemaphore.WaitAsync();

            try
            {
                _userBrowser = await GetNewBrowserAsync();

                _userBrowserCtx = await _userBrowser.CreateIncognitoBrowserContextAsync();

                _userPage = await GetNewPageAsync(_userBrowserCtx);
            }
            catch (Exception e)
            {
                Console.WriteLine("Can't get browser page and run for user " + ProfileLink + ": ", e);
            }
        }
Exemplo n.º 12
0
        protected void OnInitialized()
        {
            var switches = DotNetBrowserSwitches.Create(BrowserType);

            BrowserPreferences.SetChromiumSwitches(switches.ToArray());

            _path           = GetOrCreateContextParamsPath();
            _browserContext = new BrowserContext(new BrowserContextParams(_path));

            var browser = BrowserFactory.Create(_browserContext, BrowserType);

            browser.Preferences.AllowDisplayingInsecureContent = false;
            browser.Preferences.AllowRunningInsecureContent    = false;
            browser.Preferences.AllowScriptsToCloseWindows     = false;
            browser.Preferences.ApplicationCacheEnabled        = false;
            browser.Preferences.DatabasesEnabled          = false;
            browser.Preferences.LocalStorageEnabled       = false;
            browser.Preferences.PluginsEnabled            = false;
            browser.Preferences.TransparentBackground     = true;
            browser.Preferences.UnifiedTextcheckerEnabled = false;
            browser.Preferences.WebAudioEnabled           = false;
            browser.ZoomEnabled   = true;
            browser.DialogHandler = this;
            browser.LoadHandler   = this;
            browser.Context.NetworkService.ResourceHandler = this;

            browser.RenderGoneEvent      += Browser_RenderGoneEvent;
            browser.ScriptContextCreated += Browser_ScriptContextCreated;

            _browserView = new WPFBrowserView(browser);
            browser.ConsoleMessageEvent += Browser_ConsoleMessageEvent;

            _browserView.InputBindings.Add(new InputBinding(new BookmarkShortcut1Command(), new KeyChordGesture(ModifierKeys.Shift | ModifierKeys.Control, Key.OemQuestion, Key.D1)));
            _browserView.InputBindings.Add(new InputBinding(new BookmarkShortcut2Command(), new KeyChordGesture(ModifierKeys.Shift | ModifierKeys.Control, Key.OemQuestion, Key.D2)));
            _browserView.InputBindings.Add(new InputBinding(new BookmarkShortcut3Command(), new KeyChordGesture(ModifierKeys.Shift | ModifierKeys.Control, Key.OemQuestion, Key.D3)));
            _browserView.InputBindings.Add(new InputBinding(new BookmarkShortcut4Command(), new KeyChordGesture(ModifierKeys.Shift | ModifierKeys.Control, Key.OemQuestion, Key.D4)));
            _browserView.InputBindings.Add(new InputBinding(new BookmarkShortcut5Command(), new KeyChordGesture(ModifierKeys.Shift | ModifierKeys.Control, Key.OemQuestion, Key.D5)));
            _browserView.InputBindings.Add(new InputBinding(new BookmarkShortcut6Command(), new KeyChordGesture(ModifierKeys.Shift | ModifierKeys.Control, Key.OemQuestion, Key.D6)));
            _browserView.InputBindings.Add(new InputBinding(new BookmarkShortcut7Command(), new KeyChordGesture(ModifierKeys.Shift | ModifierKeys.Control, Key.OemQuestion, Key.D7)));
            _browserView.InputBindings.Add(new InputBinding(new BookmarkShortcut8Command(), new KeyChordGesture(ModifierKeys.Shift | ModifierKeys.Control, Key.OemQuestion, Key.D8)));
            _browserView.InputBindings.Add(new InputBinding(new BookmarkShortcut9Command(), new KeyChordGesture(ModifierKeys.Shift | ModifierKeys.Control, Key.OemQuestion, Key.D9)));

#if DEBUG
            Log.Debug(GetDevToolsUrl());
#endif
        }
Exemplo n.º 13
0
        public void VideoHasStarted()
        {
            BrowserContext.Retry(() =>
            {
                double currentTime;
                if (!double.TryParse(GetMethods.GetAttributeValue(_videoContent, BrowserContext, "currentTime"),
                                     out currentTime))
                {
                    throw new Exception();
                }

                if (currentTime > 0)
                {
                    return;
                }
                throw new Exception($"Video current time is {currentTime}, it hasn't started automatically");
            });
        }
Exemplo n.º 14
0
            public WindowMain()
            {
                BrowserContext browserContext = BrowserContext.DefaultContext;

                Browser browser = BrowserFactory.Create(browserContext);

                browser.RenderGoneEvent += delegate(object sender, RenderEventArgs e)
                {
                    // Restore Browser instance by loading the same URL
                    browser.LoadURL(e.Browser.URL);
                };

                browserView = new WPFBrowserView(browser);
                Content     = browserView;

                Width        = 1024;
                Height       = 768;
                this.Loaded += WindowMain_Loaded;
            }
Exemplo n.º 15
0
        private async Task GetCommunityDetailsAsync(BrowserContext browserContext, VkBrwCommunity community)
        {
            Page communityPage = await browserContext.NewPageAsync();

            ////groupPage.EvaluateFunctionAsync($"() => window.open('{groupUrl}')").GetAwaiter().GetResult();
            ////Target newWindowTarget = browser.WaitForTargetAsync(target => target.Url == "https://www.example.com/").Result;
            ////Page newPage = newWindowTarget.PageAsync().Result;

            try
            {
                await communityPage.GoToAsync(community.CommunityUrl);
            }
            catch (NavigationException)
            {
                return;
            }

            WaitForSelectorOptions waitSelectorOptions = new WaitForSelectorOptions {
                Timeout = 10000
            };

            ElementHandle communityLoadedElement = await communityPage.WaitForSelectorAsync("div#page_wrap.scroll_fix_wrap._page_wrap", waitSelectorOptions);

            if (communityLoadedElement != null)
            {
                ElementHandle communityBlockedElement = await communityPage.QuerySelectorAsync("div.groups_blocked");

                if (communityBlockedElement != null)
                {
                    community.Blocked = true;
                }
                else
                {
                    community.Type = await GetCommunityTypeAsync(communityPage);

                    community.CommunityId = await GetCommunityIdAsync(communityPage, community.Type);
                }
            }

            await communityPage.CloseAsync();
        }
Exemplo n.º 16
0
            public WindowMain()
            {
                BrowserContext browserContext = BrowserContext.DefaultContext;

                Browser browser = BrowserFactory.Create(browserContext);

                browser.DocumentLoadedInMainFrameEvent += delegate
                {
                    IWebStorage webStorage = browser.GetLocalWebStorage();
                    // Read and display the 'myKey' storage value.
                    Console.Out.WriteLine("The myKey value: " + webStorage["myKey"]);
                    // Modify the 'myKey' storage value.
                    webStorage.Set("myKey", "Hello from Local Storage");
                };

                browserView = new WPFBrowserView(browser);
                Content     = browserView;

                Width        = 1024;
                Height       = 768;
                this.Loaded += WindowMain_Loaded;
            }
Exemplo n.º 17
0
            public WindowMain()
            {
                BrowserContext browserContext = BrowserContext.DefaultContext;

                Browser browser = BrowserFactory.Create(browserContext);

                browserView = new WPFBrowserView(browser);
                Content     = browserView;

                Width        = 1024;
                Height       = 768;
                this.Loaded += WindowMain_Loaded;

                browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
                {
                    if (e.IsMainFrame)
                    {
                        // Get PID of Chromium render process associated with
                        // the current Browser instance
                        long pid = browser.RenderProcessInfo.Pid;
                    }
                };
            }
Exemplo n.º 18
0
        public void Should_add_cookies_to_the_request()
        {
            // Given
            var context = new BrowserContext();

            var cookies =
                new Dictionary <string, string>
            {
                { "CookieName", "CookieValue" },
                { "SomeCookieName", "SomeCookieValue" }
            };

            // When
            context.Cookie(cookies);

            // Then
            IBrowserContextValues values = context;

            var cookieString = cookies.Aggregate(string.Empty, (current, cookie) => current + string.Format("{0}={1};", HttpUtility.UrlEncode(cookie.Key), HttpUtility.UrlEncode(cookie.Value)));

            Assert.Equal(1, values.Headers["Cookie"].Count());
            Assert.Equal(cookieString, values.Headers["Cookie"].First());
        }
Exemplo n.º 19
0
        public void Should_add_cookies_to_the_request()
        {
            // Given
            var context = new BrowserContext();

            var cookies =
                new Dictionary<string, string>
                {
                    {"CookieName", "CookieValue"},
                    {"SomeCookieName", "SomeCookieValue"}
                };

            // When
            context.Cookie(cookies);

            // Then
            IBrowserContextValues values = context;

            var cookieString = cookies.Aggregate(string.Empty, (current, cookie) => current + string.Format("{0}={1};", HttpUtility.UrlEncode(cookie.Key), HttpUtility.UrlEncode(cookie.Value)));

            values.Headers["Cookie"].ShouldHaveCount(1);
            values.Headers["Cookie"].First().ShouldEqual(cookieString);
        }
Exemplo n.º 20
0
            public WindowMain()
            {
                BrowserContext browserContext = BrowserContext.DefaultContext;

                Browser browser = BrowserFactory.Create(browserContext);

                browser.FinishLoadingFrameEvent += delegate(object sender, FinishLoadingEventArgs e)
                {
                    if (e.IsMainFrame)
                    {
                        // Inserts "TeamDev DotNetBrowser" text into Google Search field.
                        browser.ExecuteCommand(EditorCommand.INSERT_TEXT, "TeamDev DotNetBrowser");
                        // Inserts a new line into Google Search field to simulate Enter.
                        browser.ExecuteCommand(EditorCommand.INSERT_NEW_LINE);
                    }
                };

                browserView = new WPFBrowserView(browser);
                Content     = browserView;

                Width        = 1024;
                Height       = 768;
                this.Loaded += WindowMain_Loaded;
            }
Exemplo n.º 21
0
 /// <summary>
 /// Adds a multipart/form-data encoded request body to the <see cref="Browser"/>, using the default boundary name.
 /// </summary>
 /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param>
 /// <param name="multipartFormData">The multipart/form-data encoded data that should be added.</param>
 public static void MultiPartFormData(this BrowserContext browserContext, BrowserContextMultipartFormData multipartFormData)
 {
     MultiPartFormData(browserContext, multipartFormData, BrowserContextMultipartFormData.DefaultBoundaryName);
 }
Exemplo n.º 22
0
        private async Task DiscoverSystemAsync()
        {
            GeoDetector geoDetector = new GeoDetector();

            CurrentCountry = await geoDetector.GetCountryAsync();

            switch (CurrentCountry)
            {
            case Countries.RU:
                Language = BrowsingLanguage.RU;
                break;

            case Countries.CN:
            case Countries.US:
            case Countries.DE:
            case Countries.FR:
            case Countries.IE:
            case Countries.GB:
            case Countries.SG:
                Language = BrowsingLanguage.EN;
                break;

            default:
                Language = BrowsingLanguage.RU;
                break;
            }

            await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);

            LaunchOptions browserOptions = new LaunchOptions
            {
                Headless = true, Args = new string[] { "--lang=" + Language.ToString().ToLowerInvariant() }
            };

            Browser browser = await Puppeteer.LaunchAsync(browserOptions);

            BrowserContext discoveryContext = await browser.CreateIncognitoBrowserContextAsync();

            Page page = await discoveryContext.NewPageAsync();

            await page.SetViewportAsync(new ViewPortOptions
            {
                Width  = 1024,
                Height = 768
            });

            await page.SetUserAgentAsync(UserAgent);

            if (Mobile)
            {
                await DiscoverServiceMobile(page);
            }
            else
            {
                await DiscoverServiceDesktop(page);
            }

            if (!page.IsClosed)
            {
                await page.CloseAsync();
            }

            await discoveryContext.CloseAsync();

            if (!browser.IsClosed)
            {
                await browser.CloseAsync();
            }
        }
Exemplo n.º 23
0
 public static BrowserContext AcceptJson(this BrowserContext context)
 {
     context.Accept("application/json");
     return(context);
 }
Exemplo n.º 24
0
        private RestfulTestServerRequest CreateRequest(HttpMethod method, string path, 
                Action<BrowserContext> browserContext)
        {
            var context =
                new BrowserContext();

            browserContext.Invoke(context);

            var contextValues =
                (IBrowserContextValues)context;

            if (String.IsNullOrEmpty(contextValues.UserHostAddress)) {
                throw new Exception("Need a valid host address");
            }

            path = path.TrimStart('/');
            var uriString = contextValues.Protocol + "://" + contextValues.UserHostAddress +
                "/" + path;

            Uri uri;

            if (!Uri.TryCreate(uriString, UriKind.Absolute,  out uri)) {
                throw new Exception(String.Format("Cannot create uri from {0}", uri));
            }

            BuildRequestBody(contextValues);

            var queryString = contextValues.QueryString.ToRestValueCollectionNoSplitOnCommas();
            var headers = contextValues.Headers.ToRestValueCollectionSplitOnCommas();
            headers["Origin"] = new [] {"http://" + path.TrimStart('/')};

            return new RestfulTestServerRequest{
                Url = uri,
                Cookies = contextValues.Cookies,
                HttpMethod = method,
                InputStream = contextValues.Body,
                RawUrl= path,
                Headers = headers,
                QueryString = queryString,
            };
        }
 protected void BuildRequest(BrowserContext context)
 {
     context.HttpRequest();
     context.Header("Accept", "application/json");
     context.Header("Content-Type", "application/json");
 }
        internal void CreateRemoteObject(ChannelOwnerType type, string guid, JsonElement?initializer)
        {
            IChannelOwner result = null;

            switch (type)
            {
            case ChannelOwnerType.BindingCall:
                result = new BindingCall(this, guid, initializer?.ToObject <BindingCallInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.Playwright:
#pragma warning disable CA2000 // Dispose objects before losing scope
                result = new Playwright(this, guid, initializer?.ToObject <PlaywrightInitializer>(_connection.GetDefaultJsonSerializerOptions()), _loggerFactory);
#pragma warning restore CA2000 // Dispose objects before losing scope
                break;

            case ChannelOwnerType.Browser:
                result = new Browser(this, guid, initializer?.ToObject <BrowserInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.BrowserType:
                result = new BrowserType(this, guid, initializer?.ToObject <BrowserTypeInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.Context:
                result = new BrowserContext(this, guid, initializer?.ToObject <BrowserContextInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.ConsoleMessage:
                result = new ConsoleMessage(this, guid, initializer?.ToObject <ConsoleMessageInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.Dialog:
                result = new Dialog(this, guid, initializer?.ToObject <DialogInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.Download:
                result = new Download(this, guid, initializer?.ToObject <DownloadInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.ElementHandle:
                result = new ElementHandle(this, guid, initializer?.ToObject <ElementHandleInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.Frame:
                result = new Frame(this, guid, initializer?.ToObject <FrameInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.JSHandle:
                result = new JSHandle(this, guid, initializer?.ToObject <JSHandleInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.Page:
                result = new Page(this, guid, initializer?.ToObject <PageInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.Request:
                result = new Request(this, guid, initializer?.ToObject <RequestInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.Response:
                result = new Response(this, guid, initializer?.ToObject <ResponseInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            case ChannelOwnerType.Route:
                result = new Route(this, guid, initializer?.ToObject <RouteInitializer>(_connection.GetDefaultJsonSerializerOptions()));
                break;

            default:
                Debug.Write("Missing type " + type);
                break;
            }

            _objects.TryAdd(guid, result);
            _connection.OnObjectCreated(guid, result);
        }
Exemplo n.º 27
0
 public CommonActions(BrowserContext browserContext)
 {
     _browserContext = browserContext;
 }
Exemplo n.º 28
0
 public static BrowserContext JsonBrowserContext(this BrowserContext context, object request)
 {
     context.Body(JsonConvert.SerializeObject(request));
     context.Header("Content-Type", "application/json");
     return(context);
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="urls"></param>
        /// <param name="path"></param>
        /// <param name="changeCountry">选择切换的国家</param>
        /// <param name="randomCity">随机切换城市</param>
        /// <param name="addText">添加页眉/页脚内容</param>
        /// <returns></returns>
        private async Task BulidPdfAsync(Uri[] urls, string path, string changeCountry, bool randomCity, string addText, bool saveHis, PaperFormat paperFormat)
        {
            try
            {
                await new BrowserFetcher(new BrowserFetcherOptions()
                {
                    Host = "http://cdn.npm.taobao.org/dist"
                }).DownloadAsync(BrowserFetcher.DefaultRevision);

                var option = new LaunchOptions
                {
                    UserDataDir     = $"{System.IO.Directory.GetCurrentDirectory()}/UserData",
                    Headless        = true,
                    DefaultViewport = null,
                    Args            = new[] {
                        "--no-sandbox",
                        "--disable-setuid-sandbox",
                        "--lang=en-US,en",
                        "--start-maximized",
                        //    "--disable-dev-shm-usage",
                        //   "--disable-extensions",
                        //  "--disable-gpu",
                        // "--disable-infobars",
                        //"--disable-local-storage",
                        // "--no-zygote",
                        // "--disable-bundled-ppapi-flash"
                        //$"--user-agent={_userAgent}"
                    },
                    IgnoreHTTPSErrors = true,
                };
                using (var browser = await Puppeteer.LaunchAsync(option))
                {
                    BrowserContext browserContext = browser.DefaultContext;
                    //if (saveHis)
                    //{
                    //    browserContext = await browser.CreateIncognitoBrowserContextAsync();

                    //}

                    var page = await browserContext.NewPageAsync();

                    await page.SetUserAgentAsync(_userAgent);

                    await page.SetViewportAsync(new ViewPortOptions
                    {
                        Width  = Screen.PrimaryScreen.WorkingArea.Width,
                        Height = Screen.PrimaryScreen.WorkingArea.Height
                    });


                    if (!changeCountry.Contains("请选择") && _CountryModels.Any())
                    {
                        OnShowTooltipInfo("初始化首页内容,开始切换国家");
                        //var zips = Properties.Resources.zip.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                        //var zip = zips.OrderBy(m => Guid.NewGuid()).First();

                        await page.GoToAsync("https://www.amazon.com/");

                        var pagetitle = await page.GetTitleAsync();

                        try
                        {
                            var docid = _CountryModels.Find(m => m.Text.Contains(changeCountry));
                            if (docid != null)
                            {
                                await page.EvaluateFunctionAsync(Properties.Resources.oChange2, docid.Id);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("访问网址受限,自动切换地区失败,\r\n请等待亚马逊解封或切换新ip后再尝试\r\n" + ex.Message, pagetitle);
                            return;
                        }
                        await Task.Delay(8000);
                    }
                    else if (randomCity)//没有选择国家的情况,选择地区切换地区
                    {
                        OnShowTooltipInfo("初始化首页内容,开始切换地区");


                        var zips = Properties.Resources.zip.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                        var zip  = zips.OrderBy(m => Guid.NewGuid()).First();
                        await page.GoToAsync("https://www.amazon.com/");

                        var pagetitle = await page.GetTitleAsync();

                        try
                        {
                            await InputMatchCaptch(page);

                            await page.EvaluateFunctionAsync(Properties.Resources.oChange, zip);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("访问网址受限,自动切换地区失败,\r\n请等待亚马逊解封或切换新ip后再尝试", pagetitle);
                            throw ex;
                        }
                        await Task.Delay(10000);
                    }


                    int index = 0;
                    int max   = urls.Length;

                    var prelist  = Properties.Resources.preload.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    var maxwidth = Screen.PrimaryScreen.WorkingArea.Width;
                    foreach (var item in urls)
                    {
                        index += 1;
                        var ht = $"<div style=\"top:0px;font-size:10px;margin-left:10px;width:{maxwidth}px;\" ><span style=\"margin-left: 20px;\">{item}</span><span style=\"margin-left: 25%;\"></span><span style=\"margin-left:25%;\">{addText}</span></div>";
                        var ft = $"<div style=\"top:0px;font-size:10px;margin-left: 10px;width:{maxwidth}px;\"><span style=\"margin-left: 20px;\">{item}</span><span style=\"margin-left: 25%;\"></span><span style=\"margin-left:25%;\">{addText}</span></div>";

                        try
                        {
                            OnShowTooltipInfo($"开始加载页面{item}");
                            // progressBar.PerformStep();


                            //搜索记录
                            if (saveHis)
                            {
                                var preurl = prelist.OrderBy(m => Guid.NewGuid()).Take(6).ToList();
                                for (int i = 0; i < preurl.Count; i++)
                                {
                                    await InputMatchCaptch(page);

                                    var url = preurl[i];
                                    OnShowTooltipInfo($"增加浏览记录 {(i + 1)}/{preurl.Count}");

                                    await page.GoToAsync(url);
                                }
                            }
                            OnShowTooltipInfo("打开目标页面 " + item.ToString());
                            //目标页面
                            await page.GoToAsync(item.ToString());

                            await page.EvaluateFunctionAsync <string>("()=>{try{document.querySelector('#productTitle').click(); document.querySelector('.nav-signin-tt.nav-flyout').remove();return 1;}catch(ex){return 0;}}");

                            if (saveHis)
                            {
                                OnShowTooltipInfo("展示浏览记录");

                                var result = await page.EvaluateFunctionAsync <string>("()=>{try{window.scrollBy(0,document.querySelector('.navFooterBackToTopText').getBoundingClientRect().top-600);return 1;}catch(ex){console.log(ex);return 0;}}");

                                await Task.Delay(3000);

                                result = await page.EvaluateFunctionAsync <string>(Properties.Resources.oRandom);

                                await Task.Delay(5000);
                            }
                            var file = Path.Combine(path, item.AbsolutePath.Replace("/dp/", "").Replace("/", "") + ".pdf");
                            OnShowTooltipInfo("开始生成PDF " + file);

                            if (option.Headless)
                            {
                                await page.PdfAsync(file, new PdfOptions()
                                {
                                    PrintBackground     = true,
                                    DisplayHeaderFooter = !string.IsNullOrEmpty(addText),
                                    HeaderTemplate      = ht,
                                    FooterTemplate      = ft,
                                    MarginOptions       = new PuppeteerSharp.Media.MarginOptions()
                                    {
                                        Top    = "40",
                                        Bottom = "40",
                                        Left   = "30",
                                        Right  = "30"
                                    },
                                    Format = paperFormat
                                });
                            }

                            OnSetProcessValue(index);
                        }
                        catch (Exception ex)
                        {
                            OnShowTooltipInfo(ex.Message + "操作失败 " + item);
                            continue;
                            // throw;
                        }
                    }

                    //if (saveHis)
                    //{
                    //    var ck = await page.GetCookiesAsync("https://www.amazon.com");
                    //    await page.DeleteCookieAsync(ck);
                    //}

                    OnShowTooltipInfo($"操作完成 {index}/{urls.Length}");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
                //throw;
            }
        }
Exemplo n.º 30
0
 public ContactUsSteps(BrowserContext browserContext)
 {
     _contactUs = new ContactUs(browserContext, PageUri.AboutHearingsPage);
 }
Exemplo n.º 31
0
 public MicrosoftLoginPage(BrowserContext browserContext)
 {
     _context = browserContext;
 }
Exemplo n.º 32
0
            public WindowMain()
            {
                BrowserContext browserContext = BrowserContext.DefaultContext;

                browser = BrowserFactory.Create(browserContext);

                var downloadHandler = new SampleDownloadHandler();

                downloadHandler.DownloadUpdated += delegate
                {
                    Application.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        CheckButton();
                    }));
                };
                browser.DownloadHandler = downloadHandler;

                layout = new Grid();
                ColumnDefinition gridCol1 = new ColumnDefinition();

                layout.ColumnDefinitions.Add(gridCol1);
                RowDefinition gridRow1 = new RowDefinition();

                gridRow1.Height = new GridLength(45);
                RowDefinition gridRow2 = new RowDefinition();

                gridRow2.Height = new GridLength(45);
                RowDefinition gridRow3 = new RowDefinition();

                gridRow3.Height = new GridLength(45);

                layout.RowDefinitions.Add(gridRow1);
                layout.RowDefinitions.Add(gridRow2);
                layout.RowDefinitions.Add(gridRow3);

                Content = layout;

                pauseButton         = new Button();
                pauseButton.Content = "Pause";
                pauseButton.Height  = 23;
                pauseButton.Click  += pauseButton_Click;
                Grid.SetRow(pauseButton, 0);
                Grid.SetColumn(pauseButton, 0);

                resumeButton         = new Button();
                resumeButton.Content = "Resume";
                resumeButton.Height  = 23;
                resumeButton.Click  += resumeButton_Click;
                Grid.SetRow(resumeButton, 1);
                Grid.SetColumn(resumeButton, 0);

                cancelButton         = new Button();
                cancelButton.Content = "Cancel";
                cancelButton.Height  = 23;
                cancelButton.Click  += cancelButton_Click;
                Grid.SetRow(cancelButton, 2);
                Grid.SetColumn(cancelButton, 0);


                layout.Children.Add(pauseButton);
                layout.Children.Add(resumeButton);
                layout.Children.Add(cancelButton);

                Width        = 400;
                Height       = 300;
                this.Loaded += WindowMain_Loaded;

                this.Closed += delegate
                {
                    browser.Dispose();
                };
            }
Exemplo n.º 33
0
 public static BrowserContext ApiAuthToken(this BrowserContext context, string token)
 {
     context.Header("Authorization", "Token " + token);
     return(context);
 }
Exemplo n.º 34
0
        public async Task Run([QueueTrigger("%JUMBO_QUEUE_NAME%", Connection = "JUMBO_CONNECTION_STRING")] string searchTerm, ILogger log)
        {
            const string merchantName = "Jumbo";
            var          page         = await BrowserContext.NewPageAsync().ConfigureAwait(false);

            await page.GotoAsync($"https://www.jumbo.com/zoeken?searchTerms={HttpUtility.UrlEncode(searchTerm)}", new PageGotoOptions { Timeout = 0 });

            var productElements = await page.QuerySelectorAllAsync("//*[@analytics-tag='product card']");

            foreach (var productElement in productElements)
            {
                try
                {
                    using Task <IElementHandle?> getProductTitle       = productElement.QuerySelectorAsync("css=[class='title-link']");
                    using Task <IElementHandle?> getPriceUnits         = productElement.QuerySelectorAsync("css=[class='whole']");
                    using Task <IElementHandle?> getPriceFraction      = productElement.QuerySelectorAsync("css=[class='fractional']");
                    using Task <IElementHandle?> getUnitSizeAndMeasure = productElement.QuerySelectorAsync("css=[class='price-per-unit']");
                    using Task <IElementHandle?> getImageSource        = productElement.QuerySelectorAsync("css=[class='image']");
                    using Task <IElementHandle?> getDetailsPageUrl     = productElement.QuerySelectorAsync("css=a:first-child");

                    await Task.WhenAll(getImageSource, getPriceFraction, getUnitSizeAndMeasure, getPriceUnits, getProductTitle, getDetailsPageUrl);

                    IElementHandle?productTitleHandle = await getProductTitle.ConfigureAwait(false);

                    IElementHandle?productPriceUnitsHandle = await getPriceUnits.ConfigureAwait(false);

                    IElementHandle?productPriceFractionHandle = await getPriceFraction.ConfigureAwait(false);

                    IElementHandle?unitSizeAndMeasureHandle = await getUnitSizeAndMeasure.ConfigureAwait(false);

                    IElementHandle?imageElementHandle = await getImageSource.ConfigureAwait(false);

                    string?id = await productElement.GetAttributeAsync("data-product-Id").ConfigureAwait(false);

                    string?title = productTitleHandle is not null ? await productTitleHandle.TextContentAsync().ConfigureAwait(false) : "";

                    string?detailsPageLink = productTitleHandle is not null ? await productTitleHandle.GetAttributeAsync("href").ConfigureAwait(false) : "";

                    string?priceUnits = productPriceUnitsHandle is not null ? await productPriceUnitsHandle.TextContentAsync().ConfigureAwait(false) : "";

                    string?priceFraction = productPriceFractionHandle is not null ? await productPriceFractionHandle.TextContentAsync().ConfigureAwait(false) : "";

                    string?unitSize      = "";
                    string?unitOfMeasure = "";
                    if (unitSizeAndMeasureHandle is not null)
                    {
                        var unitSizeAndMeasure = await unitSizeAndMeasureHandle.TextContentAsync().ConfigureAwait(false);

                        var parts = unitSizeAndMeasure is not null?unitSizeAndMeasure.Split('/') : new[] { "", "" };
                        unitSize      = RemoveSpecialCharacters(parts[0]);
                        unitOfMeasure = RemoveSpecialCharacters(parts[1]);
                    }
                    string?imageSource = await imageElementHandle.GetAttributeAsync("src").ConfigureAwait(false);

                    string?description = "";

                    if (detailsPageLink is not null)
                    {
                        await using var detailsContext = await Browser.NewContextAsync(new BrowserNewContextOptions { }).ConfigureAwait(false);

                        var detailsPage = await detailsContext.NewPageAsync().ConfigureAwait(false);

                        await detailsPage.GotoAsync($"https://www.jumbo.com{detailsPageLink}", new PageGotoOptions { Timeout = 0 });

                        var descriptionElement = await detailsPage.QuerySelectorAsync("css=[analytics-tag='product description collapsible']");

                        description = descriptionElement is not null ? await descriptionElement.TextContentAsync() ?? string.Empty : "";

                        await detailsContext.CloseAsync().ConfigureAwait(false);
                    }

                    var validatedProduct = Product.Create(id, title, description, merchantName, priceUnits, priceFraction, imageSource, unitSize, unitOfMeasure);

                    switch (validatedProduct)
                    {
                    case Valid <Product>(var product):
                        var result = IndexDocumentsAction.MergeOrUpload(product.ToIndexableProduct());
                        await SearchClient.IndexDocumentsAsync(IndexDocumentsBatch.Create(result));

                        break;

                    case Invalid <Product>(var errors):
                        // TODO : proper tracing
                        foreach (var error in errors)
                        {
                            Console.WriteLine(error);
                        }
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                }
            }

            await page.CloseAsync();
        }
Exemplo n.º 35
0
 private void DefaultBrowserContext(BrowserContext context)
 {
     context.HttpRequest();
 }
Exemplo n.º 36
0
 public static BrowserContext Authenticated(this BrowserContext context)
 {
     context.BasicAuth("admin", "servant");
     return(context);
 }
Exemplo n.º 37
0
 public static BrowserContext HttpsBrowserContext(this BrowserContext context)
 {
     context.HttpsRequest();
     return(context);
 }
Exemplo n.º 38
0
 public static BrowserContext QueryString(this BrowserContext context, string key, string value)
 {
     context.Query(key, value);
     return(context);
 }
Exemplo n.º 39
0
 public ContactUs(BrowserContext browserContext, string url) : base(browserContext, url)
 {
 }