Selenium 3 + BrowserMobProxy 2.1.4 模拟浏览器访问 (含趟坑)
背景
Selenium 是一個(gè)Web自動(dòng)化測試的組件,可基于WebDriver去控制彈出瀏覽器去做一系列Web點(diǎn)擊或行為測試(當(dāng)然也可以去做一些邪惡的事。。),減少重復(fù)人工網(wǎng)頁測試的開銷。BrowserMobProxy相當(dāng)于一層代理,它配合Selenium使用可以在Selenium控制瀏覽器訪問之前在代理層攔截做出一些記錄(har)、修改等。
一般來說,直接使用Selenium就足夠了,但是Selenium有個(gè)很致命的問題是不支持修改request的參數(shù),比如很重要的headers。headers其實(shí)是一些模擬測試時(shí)用來模擬不同的訪問和測試安全的重要元信息,從一些github issues看Selenium開發(fā)方似乎拒絕考慮加入headers修改功能,以自動(dòng)化測試組件不應(yīng)該讓用戶修改headers的理由應(yīng)付用戶,并讓大家使用BrowserMobProxy去模擬。如果webdriver也是可控的,這完全是可以做到的,可能會(huì)需要協(xié)調(diào)不同瀏覽器的webdriver開發(fā)者會(huì)有點(diǎn)麻煩;所以也不知道是開發(fā)方懶,還是和browsermobproxy的開發(fā)方有什么關(guān)系。。而彈出瀏覽器并能提供足夠的控制功能的框架目前暫沒見到其他能與selenium媲美的。(PhantomJS是在服務(wù)端提供了個(gè)解析,但并不會(huì)事實(shí)彈出瀏覽器模仿真正的瀏覽器行為,也就是說一些前端涉及鼠標(biāo)、懸停等事件的腳本并不能支持)
介紹一下Selenium3.x里幾個(gè)概念:
- WebDriver:可以理解為連接不同瀏覽器的驅(qū)動(dòng)程序,比如chrome和firefox的webdriver是不同的,如果selenium相關(guān)包沒有引入你的瀏覽器,就要考慮去尋找了。內(nèi)置支持的瀏覽器參見github。像IE這種需要windows相關(guān)組件支持可參考官網(wǎng)配置步驟。
- Selenium IDE:其實(shí)就是個(gè)可視化的測試案例創(chuàng)建管理的組件,一般我們用selenium可能是寫代碼,但部分沒那么復(fù)雜的測試功能可以通過該IDE去配置,也降低了QC的門檻。使用代碼去模擬測試的可忽略IDE的存在。
- Selenium Server:一般只會(huì)在遠(yuǎn)程測試的時(shí)候需要。比如公司有臺測試機(jī),你想要在上面測試但又不能在上面直接開發(fā),你就可以在測試機(jī)起SeleniumServer,本地測試代碼通過RemoteWebDriver的形式去連接它。大多數(shù)情況下本地測試可以忽略這個(gè)server的存在。
BrowserMobProxy會(huì)提供一個(gè)ProxyServer用于做轉(zhuǎn)發(fā)代理攔截,這個(gè)server可以是standalone部署支持遠(yuǎn)程,也可以embed進(jìn)代碼中。由于BrowserMob是Java開發(fā)的,因此JVM的可以支持真正的embedded,python等非JVM系的只能配置其執(zhí)行路徑通過子進(jìn)程的方式來偽裝embedded,這就是AutomatedTester/browsermob-proxy-py項(xiàng)目中需要配置 /path/to/browsermobproxy 的原因。
?
使用
Selenium WebDriver的具體使用請參考網(wǎng)上的教程示例和官方文檔,此處不贅述。
BrowserMobProxy官方文檔里有段 use with selenium 的代碼示例其實(shí)就是 embedded browsermob ?+ local selenium :
// start the proxyBrowserMobProxy proxy = new BrowserMobProxyServer();proxy.start(0);// set custom headersproxy.addHeaders(headers);// get the Selenium proxy objectProxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);// configure it as a desired capabilityDesiredCapabilities capabilities = new DesiredCapabilities();capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);// start the browser upWebDriver driver = new FirefoxDriver(capabilities);// open yahoo.comdriver.get("http://yahoo.com");需留意的是,Firefox的webdriver有坑,貌似Firefox>=52.0版本的需要用最新的Selenium和最新的Webdriver(3.3.0+)。本文主要以Java的代碼為示例,python用法也類似(需先 pip install browsermob-proxy)。
?
remote browsermob + local selenium
executeCMD(String.format("curl -X POST -d 'port=%d' http://localhost:8080/proxy", 55555));executeCMD(String.format("curl -X POST -H \"Content-disposition:json/application\" - H \"Content-type:json/application\" -d '%s' http://localhost:8080/proxy/" + (port1) + "/headers", headersJson));// get the Selenium proxy objectString PROXY = "localhost:8080";Proxy seleniumProxy = ClientUtil.createSeleniumProxy(new InetSocketAddress("localhost", 8080));seleniumProxy.setHttpProxy(PROXY).setSslProxy(PROXY);// configure it as a desired capabilityDesiredCapabilities capabilities = DesiredCapabilities.chrome();capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);// start the browser upWebDriver driver = new ChromeDriver(capabilities);
即browsermob在外部起,本地只能通過restful與其通信,包括先注冊端口和設(shè)置headers等,seleniumProxy也得配置到對應(yīng)的socket去。部署browsermob也很簡單,http://bmp.lightbody.net/ 下載部署版本或從github下載項(xiàng)目tag版本命令行執(zhí)行 mvn clean package -U 源碼安裝。源碼安裝的話在browsermob-dist/target/ 下可以找到bin結(jié)尾的目錄,里面就是可執(zhí)行文件(*nix和windows bat都有),copy到你需要的目錄即可。
?
embedded browsermob + remote selenium server
?
BrowserMobProxy proxyServer = new BrowserMobProxyServer(); proxyServer.addHeaders(headers); proxyServer.start(port); String PROXY = "localhost:" + port; Proxy seleniumProxy = ClientUtil.createSeleniumProxy(new InetSocketAddress("localhost", port)); seleniumProxy.setHttpProxy(PROXY).setSslProxy(PROXY);// configure it as a desired capability DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);// 默認(rèn)selenium server起在4444端口,可在capabilities中配置端口 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444"), capabilities);同樣需去Selenium官網(wǎng)下載standalone-server的jar包,下下來后java -jar xxx.jar 即可運(yùn)行,相關(guān)參數(shù)配置見文檔。須注意的是,3.x的selenium server需要jdk8支持,2.x只需要jdk1.7支持。
?
其實(shí)如同以上例子,可根據(jù)需求自由組合browsermob和selenium的使用。browsermob 2.x的ProxyServer換成了BrowserMobProxyServer,但其實(shí)接口大同小異,它的filter概念有點(diǎn)類似之前的interceptor概念,攔截修改了request后返回null就可以了(不需要按照方法聲明中的返回值真的構(gòu)造一個(gè)response),官網(wǎng)有filter的例子。
?
maven依賴:
<dependency><groupId>net.lightbody.bmp</groupId><artifactId>browsermob-core</artifactId><version>${browsermob.version}</version><exclusions><exclusion><artifactId>guava</artifactId><groupId>com.google.guava</groupId></exclusion></exclusions></dependency><dependency><groupId>net.lightbody.bmp</groupId><artifactId>browsermob-legacy</artifactId><version>${browsermob.version}</version><exclusions><exclusion><artifactId>guava</artifactId><groupId>com.google.guava</groupId></exclusion></exclusions></dependency><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>${selenium.version}</version></dependency>guava會(huì)有沖突,需exclude。selenium版本3.3.0, browsermob 2.1.4 。
?
修改headers支持
其實(shí)以上示例中的addHeaders函數(shù)就可以修改headers了,親測可用。但是需注意,你在瀏覽器看到的request headers不代表最終的headers,你最好用個(gè) php頁面 把server端真正的request打印出來才能看到效果。相關(guān)解釋。可以自己做個(gè)試驗(yàn)證實(shí),比如把Host或者一些重要字段隨便填寫,去訪問baidu等,你會(huì)發(fā)現(xiàn)訪問不了,改回去headers就可以了。
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lhfcws/p/6594038.html
總結(jié)
以上是生活随笔為你收集整理的Selenium 3 + BrowserMobProxy 2.1.4 模拟浏览器访问 (含趟坑)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# OracleParameter 传
- 下一篇: fragment error