构建Python+Selenium2自动化测试环境

  初步计划经过Python做为脚本语言,Selenium做为web端的测试工具,目前主要是基于web端来构建的。本节主要记录简单搭建Python+Selenium测试环境的过程,具体以下:python

  基础环境:windows 7 64bitlinux

  一、构建python开发环境,版本为当前最新版本python2.7.5web

  在python官方网站选择下载最新windows安装包:python-2.7.5.amd64.msi,注意这里选择64bit的。安装完以后,须要在系统的环境变量path中加入C:\Python27,而后能够在命令行,看到以下:chrome

    

  备注:以上表示,python安装成功,且path配置也ok!windows

  二、SetupTools和pip工具安装,这两个工具都是属于python的第三方工具包软件,有点相似于linux下的安装包软件,不过pip比SetupTools功能更强大。浏览器

  SetupTools官方解释:Download, build, install, upgrade, and uninstall Python packages -- easily!python2.7

  在python的官方网站上能够找到SetupTools的下载,这里Windows只提供了32bit的下载,setuptools-0.6c11.win32-py2.7.exe,直接双击安装便可。编辑器

  pip官方解释:A tool for installing and managing Python packages.工具

  cmd进入命令行:easy_install pip 在线安装便可。测试

  备注:此处须要注意的是,当安装SetupTools以后,就能够在python安装目录下看到Script目录,以下图所示:

  

  这个目录生成以后,须要在系统环境变量的中加入 path:C:\Python27\Scripts,而后才能够在命令使用easy_install命令进行pip在线安装。

  三、安装Selenium

  这里由于须要将Python和Selenium进行组合,固然Selenium也提供了基于python的实现,因此就须要把Selenium的包安装到python库中去,以便于python开发时进行调用。

  在cmd进入命令行:pip install selenium  执行以后,将自动化搜寻最新的selenium版本下载并安装,以下图所示:

  

  以上显示,则代表在线安装selenium成功!

  四、Python+Selenium的Sample

  这里能够直接在python的编辑中编写以下程序,并保存hello_selenium.py  

复制代码
1 from selenium import webdriver 2 3 driver = webdriver.Firefox() 4 driver.get("http://www.so.com") 5 assert "360搜索".decode('utf-8') in driver.title 6 7 print driver.title 8 9 driver.close()
复制代码

  在python编辑器里面操做F5运行便可,看看是否成功调用Firefox浏览器。。。

  以上一个基础的Python+Selenium的自动化环境已经搭建完成。  

构建Python+Selenium2自动化测试环境完成以后,就须要测试支持python的selenium的版本是否都支持在不一样浏览器上运行,当前咱们分别在三个最通用的浏览器上经过脚原本测试。

  一、在IE浏览器上运行测试脚本,首先须要下载IEDriverServer.exe,放在IE浏览器的安装目录且同级目录下,脚本以下:

复制代码
import os from selenium import webdriver from selenium.webdriver.common.keys import Keys iedriver = "C:\Program Files\Internet Explorer\IEDriverServer.exe" os.environ["webdriver.ie.driver"] = iedriver driver = webdriver.Ie(iedriver) driver.get("http://www.python.org") assert "Python" in driver.title elem = driver.find_element_by_name("q") elem.send_keys("selenium") elem.send_keys(Keys.RETURN) assert "Google" in driver.title driver.close() driver.quit()
复制代码

  二、在Chrome浏览器上运行测试脚本,首先须要下载ChromeDriver.exe,放在Chrome浏览器的安装目录且同级目录下,脚本以下:

复制代码
import os from selenium import webdriver from selenium.webdriver.common.keys import Keys chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe" os.environ["webdriver.chrome.driver"] = chromedriver driver = webdriver.Chrome(chromedriver) driver.get("http://www.python.org") assert "Python" in driver.title elem = driver.find_element_by_name("q") elem.send_keys("selenium") elem.send_keys(Keys.RETURN) assert "Google" in driver.title driver.close() driver.quit()
复制代码

  三、在Firefox浏览器上运行测试脚本,具体以下:

复制代码
from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driver.get("http://www.python.org") assert "Python" in driver.title elem = driver.find_element_by_name("q") elem.send_keys("selenium") elem.send_keys(Keys.RETURN) assert "Google" in driver.title driver.close() driver.quit()
复制代码

  总结:经过以上三个不一样浏览器上的测试,说明selenium在python中的运用于其Java版本都是同样。因为Firefox是默认安装 路径,webdriver能够正常访问找到他,若是非系统默认安装路径,则须要跟IE和Chrome同样来设置driver路径。

相关文章
相关标签/搜索