Selenium + Python的自动化框架搭建


selenium是一个web的自动化测试工具,和其它的自动化工具相比来说其最主要的特色是跨平台、跨浏览器。
支持windows、linux、MAC,支持ie、ff、safari、opera、chrome等。
此外还有一个特色是支持分布式测试用例的执行,可以把测试用例分布到不同的测试机器的执行,相当于分发机的功能。

关于selenium的原理、架构、使用等可以参考其官网的资料,这里记录如何搭建一个使用python的selenium测试用例开发环境。其实用python
来开发selenium的方法有2种:一是去selenium官网下载python版的selenium引擎;还有一个就是搭建robot自动化框架,而后安装robot的
selenium插件。

这里记录的是第一种搭建方式:
1、下载并安装setuptools的Windows版本【这个工具是python的基础包工具】
2、下载并安装pip工具【这个工具是python的安装包管理工具,类似于Ubuntu的aptget工具】
3、通过pip命令安装selenium工具
4、测试demo脚本

具体安装操作:
1、去这个地址http://pypi.python.org/pypi/setuptools下载setuptools【setuptools-0.6c11.win32-py2.6.exe】
2、直接安装其Windows版本的安装包,但需要对应的python版本支持
3、去这个地址http://pypi.python.org/pypi/pip下载pip【pip-1.0.2.tar.gz】
4、用winrar解压,命令行进入其目录输入命令:python setup.py install
5、直接使用pip安装selenium,命令为:pip install -U selenium
6、在命令行调用测试脚本【python demo.py】

如果测试成功会看到打开浏览器后进行google搜索。另外selenium分版本1和版本2,这里安装是版本2的selenium。
附:demo的脚本内容如下

  1. #!/usr/bin/python   
  2. # -*- coding: gb2312 -*-   
  3.   
  4. from selenium import webdriver  
  5. from selenium.common.exceptions import TimeoutException  
  6. from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0   
  7. import time  
  8.   
  9. # Create a new instance of the Firefox driver   
  10. driver = webdriver.Chrome()  
  11.   
  12. # go to the google home page   
  13. driver.get("http://www.google.com")  
  14.   
  15. # find the element that's name attribute is q (the google search box)   
  16. inputElement = driver.find_element_by_name("q")  
  17.   
  18. # type in the search   
  19. inputElement.send_keys("Cheese!")  
  20.   
  21. # submit the form. (although google automatically searches now without submitting)   
  22. inputElement.submit()  
  23.   
  24. # the page is ajaxy so the title is originally this:   
  25. print driver.title  
  26.   
  27. try:  
  28.     # we have to wait for the page to refresh, the last thing that seems to be updated is the title   
  29.     WebDriverWait(driver, 10).until(lambda driver : driver.title.lower().startswith("cheese!"))  
  30.   
  31.     # You should see "cheese! - Google Search"   
  32.     print driver.title  
  33.   
  34. finally:  
  35.     driver.quit()  
  36.   
  37. #==================================  

相关内容