我使用 RobotFramework 和 Selenium 库为各种网站编写自动化测试用例。我遇到了一个问题,由于第三方 URL 加载缓慢,某些页面需要永远加载,我想阻止它们以加快测试执行速度。
但是,我坚持通过 Robot Framework 中的 BrowserMob 实现该解决方案。有人可以帮忙吗?
到目前为止,我有这个代码:
Start Browser
## Init BrowserMob Proxy
${BMPATH} Join Path ${EXECDIR} browsermob-proxy-2.1.4 bin browsermob-proxy.bat
&{bmphost} Create Dictionary address=127.0.0.1 port=7070
Start Local Server ${BMPATH}
# Create dedicated proxy on BrowserMob Proxy
&{host} Create Dictionary address=127.0.0.1 port=7070
${BrowserMob_Proxy}= Create Proxy ${host}
#Blacklist URLS
Blacklist https://.*\\.google.com/.* 404
## Configure Webdriver to use BrowserMob Proxy
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${options} add_argument --proxy\=127.0.0.1:7070
#${proxy1}= Evaluate sys.modules['selenium.webdriver'].Proxy() sys, selenium.webdriver
#${proxy1.http_proxy}= Set Variable localhost:8888
Create WebDriver Chrome chrome_options=${options} #proxy=${BrowserMob_Proxy}
Go to https://www.google.com
目标是翻译这个 selenium/java 代码:
private WebDriver initializeDriver() throws Exception {
// Start the server and get the selenium proxy object
ProxyServer server = new ProxyServer(proxy_port); // package net.lightbody.bmp.proxy
server.start();
server.setCaptureHeaders(true);
// Blacklist google analytics
server.blacklistRequests("https?://.*\\.google-analytics\\.com/.*", 410);
// Or whitelist what you need
server.whitelistRequests("https?://*.*.yoursite.com/.*. https://*.*.someOtherYourSite.*".split(","), 200);
Proxy proxy = server.seleniumProxy(); // Proxy is package org.openqa.selenium.Proxy
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
// start the driver ;
Webdriver driver = new FirefoxDriver(capabilities);
return driver;
我现在拥有的代码创建了服务器、代理并打开了 Chrome 浏览器,但无法将 google.com 列入黑名单并无论如何打开该页面。尝试了多个正则表达式,都失败了,但我认为错误是在此之前的某个地方发生的,无论是创建代理/服务器的方式还是将 URL 列入黑名单的方式都不正确。
有人在射频领域成功实施过这个解决方案吗?有人能告诉我我错过了什么吗?
Thanks
对于任何可能需要这个的人,解决方案如下:
Start Browser
[Documentation] Start browser
Set Selenium Implicit Wait 10
${BMPATH} Join Path ${EXECDIR} browsermob-proxy-2.1.4 bin browsermob-proxy.bat
Start Local Server ${BMPATH}
## Create dedicated proxy on BrowserMob Proxy
&{host} Create Dictionary port=7070
${BrowserMob_Proxy}= Create Proxy ${host}
Blacklist .*\/\/.*google.* 200
## Configure Webdriver to use BrowserMob Proxy
${options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${options} add_argument --start-maximized
Call Method ${options} add_argument --proxy-server\=localhost:7070
Create WebDriver Chrome chrome_options=${options}
这将最大化 Chrome 浏览器并通过 BMP 路由流量。
黑名单行中的正则表达式意味着它将阻止所有包含“google”的 URL 并返回状态 200(这可以根据您的意愿进行更改)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)