pythonselenium兼容性_python+selenium实现跨浏览器兼容性测试

标签:

python

https://www.python.org/

python是一种脚本语言, 易学易用,可以助你快速实现业务逻辑,高效集成系统。

----- http://zh.wikipedia.org/zh-cn/Python:

它的语法简单,与其它大多数程序设计语言使用大括号不一样,它使用缩进来定义语句块。

Python经常被用于Web开发。比如,通过mod_wsgi模块,Apache可以运行用Python编写的Web程序。使用Python语言编写的Gunicor

n作为Web服务器,也能够运行Python语言编写的Web程序。Python定义了WSGI标准应用接口来协调Http服务器与基于Python的Web程序之间的沟通。一些Web框架,如Django、Pyramid、TurboGears、Tornado、web2py、Zope、Flask等,可以让程序员轻松地开发和管理复杂的Web程序。

download: https://www.python.org/downloads/release/python-279/

selenium python bindings

selenium python bindings 提供一套简单的API,通过这些API调用了Selenium WebDriver,可以实现功能测试和验收性测试用例。

可以支持对 Firefox 和 IE 和 chrome的调用。

官网介绍:

http://selenium-python.readthedocs.org/installation.html#introduction

windows安装

1、 下载安装python3.4 https://www.python.org/ftp/python/3.4.2/python-3.4.2.msi

2、使用cmd.exe执行如下命令,安装selenium python bindings

C:\Python34\Scripts\pip.exe install selenium

3、下载IEdriver 和 chromedriver,并放到python安装目录下 (C:\Python34)

https://github.com/fanqingsong/daydayup/blob/master/CSoftware/webdriver/IEDriverServer.exe

https://github.com/fanqingsong/daydayup/blob/master/CSoftware/webdriver/chromedriver.exe

firefox不需要驱动

使用方法待研究驱动:

https://github.com/dineshkummarc/operadriver

https://github.com/mfazekas/safaridriver

4、可以运行python的测试脚本了,例如:

C:\Python34\python.exe C:\my_selenium_script.py

官网安装介绍:

http://selenium-python.readthedocs.org/installation.html#downloading-python-bindings-for-selenium

测试脚本

下面脚本实现, 打开baidu网页,搜索一个关键字, 依次执行三个浏览器(firefox ie chrome)。

1 #import os2 importtime3 from selenium importwebdriver4 from selenium.webdriver.common.keys importKeys5

6 #test case

7 deftestbrowser(driver):8 driver.get("http://www.baidu.com")9 driver.find_element_by_id("kw").click()10 driver.find_element_by_id("kw").clear()11 driver.find_element_by_id("kw").send_keys("vx")12 driver.find_element_by_id("su").click()13 driver.implicitly_wait(30)14 time.sleep(3)15 driver.close()16 driver.quit()17 returnNone18

19 driverfirefox =webdriver.Firefox()20 testbrowser(driverfirefox)21

22 driverie =webdriver.Ie()23 testbrowser(driverie)24

25 driverchrome =webdriver.Chrome()26 testbrowser(driverchrome)

测试用例testbrowser代码可以使用 selenium IDE导出的脚本中截取主体部分:

#-*- coding: utf-8 -*-

from selenium importwebdriverfrom selenium.webdriver.common.by importByfrom selenium.webdriver.common.keys importKeysfrom selenium.webdriver.support.ui importSelectfrom selenium.common.exceptions importNoSuchElementExceptionfrom selenium.common.exceptions importNoAlertPresentExceptionimportunittest, time, reclassBaidupython(unittest.TestCase):defsetUp(self):

self.driver=webdriver.Firefox()

self.driver.implicitly_wait(30)

self.base_url= "http://www.baidu.com/"self.verificationErrors=[]

self.accept_next_alert=Truedeftest_baidupython(self):

driver= self.driver

driver.get(self.base_url + "/")

driver.find_element_by_id("kw").click()

driver.find_element_by_id("kw").clear()

driver.find_element_by_id("kw").send_keys("vx")

driver.find_element_by_id("su").click()defis_element_present(self, how, what):try: self.driver.find_element(by=how, value=what)except NoSuchElementException, e: returnFalsereturnTruedefis_alert_present(self):try: self.driver.switch_to_alert()except NoAlertPresentException, e: returnFalsereturnTruedefclose_alert_and_get_its_text(self):try:

alert=self.driver.switch_to_alert()

alert_text=alert.textifself.accept_next_alert:

alert.accept()else:

alert.dismiss()returnalert_textfinally: self.accept_next_alert =TruedeftearDown(self):

self.driver.quit()

self.assertEqual([], self.verificationErrors)if __name__ == "__main__":

unittest.main()

也可以自己写脚本,使用selenium driver的提供的api:

http://selenium-python.readthedocs.org/locating-elements.html

python教程:

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013747381369301852037f35874be2b85aa318aad57bda000

标签:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值