selenium:结合httpwatch进行网页测试(Python版)
【概述】
Httpwatch一款强大的网页数据分析工具。它可以捕捉http/https数据,查看底层的数据,包括headers、cookies、cache等。同时,记录发送请求、接收请的时间。Anyway,agoodtoolforyou。
或许,你有一个需求,要在selenium进行页面功能测试的时候,你需要获取一些信息,如提交请求数据、接收请求数据、页面加载的时间等。selenium+httpwatch,将是一个不错的解决方案。本文主要介绍,将httpwatch附加到seleniumwebdriver的方法。
注意:
有关httpwatch的使用,请自行查看API文档:
http://apihelp.httpwatch.com/#Automation%20Overview.html
【selenium+httpwatch】
官方做法(JAVA版):
IWebDriverdriver=<CreateSeleniumDriverusingexistingprofile>;//Don'tusenewFirefoxDriver()
//SetauniqueinitialpagetitlesothatHttpWatchcanattachtoit
stringuniqueTitle=Guid.NewGuid().ToString();
IJavaScriptExecutorjs=driverasIJavaScriptExecutor;
js.ExecuteScript("document.title='"+uniqueTitle+"';");
//AttachHttpWatchtotheinstanceofthebrowsercreatedthroughSelenium
Pluginplugin=control.AttachByTitle(uniqueTitle);
IWebDriverdriver=<CreateSeleniumDriverusingexistingprofile>;//Don'tusenewFirefoxDriver()
//SetauniqueinitialpagetitlesothatHttpWatchcanattachtoit
stringuniqueTitle=Guid.NewGuid().ToString();
IJavaScriptExecutorjs=driverasIJavaScriptExecutor;
js.ExecuteScript("document.title='"+uniqueTitle+"';");
//AttachHttpWatchtotheinstanceofthebrowsercreatedthroughSelenium
Pluginplugin=control.AttachByTitle(uniqueTitle);
我就照模照样的写个Python版的
control=win32com.client.Dispatch('HttpWatch.Controller')
#driver=webdriver.Ie(executable_path="D:\\software\\IEDriverServer_Win32_2.28.0\\IEDriverServer.exe")
profile=webdriver.FirefoxProfile(r'C:\Users\X230\AppData\Roaming\Mozilla\Firefox\Profiles\68zb9g9a.default')
driver=webdriver.Firefox(firefox_profile=profile)
uniqueTitle=str(uuid.uuid4())
driver.get(url)
driver.execute_script('document.title="'+uniqueTitle+'";')
plugin=control.AttachByTitle(uniqueTitle)
plugin.Log.EnableFilter(False)
plugin.Record()
#plugin.GotoURL(url)
plugin.Stop()
control=win32com.client.Dispatch('HttpWatch.Controller')
#driver=webdriver.Ie(executable_path="D:\\software\\IEDriverServer_Win32_2.28.0\\IEDriverServer.exe")
profile=webdriver.FirefoxProfile(r'C:\Users\X230\AppData\Roaming\Mozilla\Firefox\Profiles\68zb9g9a.default')
driver=webdriver.Firefox(firefox_profile=profile)
uniqueTitle=str(uuid.uuid4())
driver.get(url)
driver.execute_script('document.title="'+uniqueTitle+'";')
plugin=control.AttachByTitle(uniqueTitle)
plugin.Log.EnableFilter(False)
plugin.Record()
#plugin.GotoURL(url)
plugin.Stop()
请留意官方版的第一句话(如下)。意思是,(如果使用Firefox)请不要使用默认的profile配置,需要使用Firefox本地profile配置。
IWebDriverdriver=<CreateSeleniumDriverusingexistingprofile>;//Don’tusenewFirefoxDriver()
seleniumFirefox默认配置文件是webdriver_prefs.json,默认不加载所有的插件(add-ons)。所以,如果你忽略第一句,将发生以下报错:
plugin=self._control.AttachByTitle(uniqueTitle)
File"<COMObjectHttpWatch.Controller>",line2,inAttachByTitle
pywintypes.com_error:(-2147352567,'\xb7\xa2\xc9\xfa\xd2\xe2\xcd\xe2\xa1\xa3',(0,u'HttpWatch.Controller',u"AttachByTitlefailedbecausenoIEorFirefoxpagewasfoundwiththetitle'd9f5f540-3227-4d8a-b928-daaf196e256b'.\r\n\r\nPleasecheckthatHttpWatchisinstalled,enabledandworkingcorrectlyinthetargetbrowserwindow.AlsoifyouareusingSeleniumandFirefoxmakesurethatyouareusingacopyofaprofilethathasHttpWatchenabledratherthanafreshprofilecreatedby'newFirefoxDriver()'.FormoreinformationpleaserefertotheSelenium/FirefoxsampledescribedintheHttpWatchAutomationhelp-AttachByTitlefailed",None,0,-2147467259),None)
plugin=self._control.AttachByTitle(uniqueTitle)
File"<COMObjectHttpWatch.Controller>",line2,inAttachByTitle
pywintypes.com_error:(-2147352567,'\xb7\xa2\xc9\xfa\xd2\xe2\xcd\xe2\xa1\xa3',(0,u'HttpWatch.Controller',u"AttachByTitlefailedbecausenoIEorFirefoxpagewasfoundwiththetitle'd9f5f540-3227-4d8a-b928-daaf196e256b'.\r\n\r\nPleasecheckthatHttpWatchisinstalled,enabledandworkingcorrectlyinthetargetbrowserwindow.AlsoifyouareusingSeleniumandFirefoxmakesurethatyouareusingacopyofaprofilethathasHttpWatchenabledratherthanafreshprofilecreatedby'newFirefoxDriver()'.FormoreinformationpleaserefertotheSelenium/FirefoxsampledescribedintheHttpWatchAutomationhelp-AttachByTitlefailed",None,0,-2147467259),None)
那Firefox本地profile文件在哪,请查阅Firefox官方帮助:
https://support.mozilla.org/en-US/kb/profiles-where-firefox-stores-user-data
然后,如下初始化driver即可:
driver=webdriver.Firefox(firefox_profile=profile)