在 Robot Framework/Selenium 中使用 BrowserMobProxy 将 URL 列入黑名单? [英] URL blacklisting with BrowserMobProxy in Robot Framework/Selenium?
问题描述
我正在使用带有 Selenium 库的 RobotFramework 为各种网站编写自动化测试用例.我遇到了一个问题,由于从第三方 URL 加载缓慢,某些页面需要永远加载,我想阻止它们以加快测试执行速度.
I'm using RobotFramework with Selenium library for writing automated test cases for various websites. I've encountered an issue where due to slow loading from third-party URLs, some pages take forever to load and I want to block them to speed up test execution.
但是,我坚持通过 Robot Framework 中的 BrowserMob 实现该解决方案.任何人都可以帮忙吗?
However, I am stuck on implementing that solution via BrowserMob in Robot Framework. Can anyone help?
到目前为止,我有这个代码:
So far, I have this code:
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 代码:
The goal was to translate this selenium/java code:
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列入黑名单的方式都不正确.
The code I have now, creates the server, the proxy and opens the chrome browser but fails to blacklist google.com and opens the page anyway. Tried multiple regexp expressions, all failed but I think that the error is made somewhere before that, either in the way of creating the proxy/server or the way to blacklist URL is not right.
有没有人在 RF 中成功实施过这个解决方案?有人可以告诉我我错过了什么吗?
Has anyone had success implementing this solution in RF? Can someone tell me what I'm missing?
谢谢
推荐答案
对于可能需要此功能的任何人,解决方案如下:
For anyone who might need this, the solution is below:
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 路由流量.Blacklist 行中的正则表达式意味着它将阻止所有包含google"的 URL 并返回状态 200(可以随意更改).
This will start the chrome browser maximized and route traffic via BMP. The regex in the Blacklist line means it will block all URL containing 'google' and return status 200 (this can be changed at your will).
这篇关于在 Robot Framework/Selenium 中使用 BrowserMobProxy 将 URL 列入黑名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!