设为首页 收藏本站
查看: 1376|回复: 0

[经验分享] selenium:结合httpwatch进行网页测试(Python版)

[复制链接]

尚未签到

发表于 2017-5-6 13:48:27 | 显示全部楼层 |阅读模式
【概述】

Httpwatch 一款强大的网页数据分析工具。它可以捕捉http/https数据,查看底层的数据,包括headers、cookies、cache等。同时,记录发送请求、接收请的时间。Anyway,a good tool for you。
或许,你有一个需求,要在selenium进行页面功能测试的时候,你需要获取一些信息,如提交请求数据、接收请求数据、页面加载的时间等。selenium + httpwatch,将是一个不错的解决方案。本文主要介绍,将httpwatch附加到selenium webdriver的方法。

注意:
有关httpwatch的使用,请自行查看API文档:
http://apihelp.httpwatch.com/#Automation%20Overview.html

【selenium + httpwatch】

官方做法(JAVA版):

IWebDriver driver = <Create Selenium Driver using existing profile>; // Don't use new FirefoxDriver()

// Set a unique initial page title so that HttpWatch can attach to it
string uniqueTitle = Guid.NewGuid().ToString();
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("document.title = '" + uniqueTitle + "';");

// Attach HttpWatch to the instance of the browser created through Selenium
Plugin plugin = control.AttachByTitle(uniqueTitle);
IWebDriver driver = <Create Selenium Driver using existing profile>; // Don't use new FirefoxDriver()

// Set a unique initial page title so that HttpWatch can attach to it
string uniqueTitle = Guid.NewGuid().ToString();
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("document.title = '" + uniqueTitle + "';");

// Attach HttpWatch to the instance of the browser created through Selenium
Plugin plugin = 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配置。

IWebDriver driver = < Create Selenium Driver using existing profile >; // Don’t use new FirefoxDriver()
selenium Firefox 默认配置文件是webdriver_prefs.json,默认不加载所有的插件(add-ons)。所以,如果你忽略第一句,将发生以下报错:

    plugin = self._control.AttachByTitle(uniqueTitle)
  File "<COMObject HttpWatch.Controller>", line 2, in AttachByTitle
pywintypes.com_error: (-2147352567, '\xb7\xa2\xc9\xfa\xd2\xe2\xcd\xe2\xa1\xa3', (0, u'HttpWatch.Controller', u"AttachByTitle failed because no IE or Firefox page was found with the title 'd9f5f540-3227-4d8a-b928-daaf196e256b'. \r\n\r\nPlease check that HttpWatch is installed, enabled and working correctly in the target browser window. Also if you are using Selenium and Firefox make sure that you are using a copy of a profile that has HttpWatch enabled rather than a fresh profile created by 'new FirefoxDriver()'. For more information please refer to the Selenium/Firefox sample described in the HttpWatch Automation help - AttachByTitle failed", None, 0, -2147467259), None)
plugin = self._control.AttachByTitle(uniqueTitle)
  File "<COMObject HttpWatch.Controller>", line 2, in AttachByTitle
pywintypes.com_error: (-2147352567, '\xb7\xa2\xc9\xfa\xd2\xe2\xcd\xe2\xa1\xa3', (0, u'HttpWatch.Controller', u"AttachByTitle failed because no IE or Firefox page was found with the title 'd9f5f540-3227-4d8a-b928-daaf196e256b'. \r\n\r\nPlease check that HttpWatch is installed, enabled and working correctly in the target browser window. Also if you are using Selenium and Firefox make sure that you are using a copy of a profile that has HttpWatch enabled rather than a fresh profile created by 'new FirefoxDriver()'. For more information please refer to the Selenium/Firefox sample described in the HttpWatch Automation help - AttachByTitle failed", 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)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-373888-1-1.html 上篇帖子: 零基础学python-18.9 序列映射函数:map 下篇帖子: 从C/C++程序员角度看Python的动态类型
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表