转:Selenium+Python的自动化框架搭建

上一篇 / 下一篇  2015-10-10 16:16:44

Selenium是一个web自动化测试工具,和其它的自动化工具相比来说其最主要的特色是跨平台、跨浏览器。
  支持windows、linux、MAC,支持ie、ff、safari、opera、chrome等。
  此外还有一个特色是支持分布式测试用例的执行,可以把测试用例分布到不同的测试机器的执行,相当于分发机的功能。
  关于selenium的原理、架构、使用等可以参考其官网的资料,这里记录如何搭建一个使用python的selenium测试用例开发环境。其实用python
  来开发selenium的方法有2种:一是去selenium官网下载python版的selenium引擎;还有一种没有操作过,这里就不列出来了,有兴趣的朋友可以自行查阅资料
  这里记录的是第一种搭建方式:
  1.安装python2.7x32或者x64版本
  2.安装pip工具
  3.直接使用pip安装selenium,命令为:pip install -U selenium
  如果测试成功会看到打开浏览器后进行google搜索。另外selenium分版本1和版本2,这里安装是版本2的selenium。派生到我的代码片
#-*-coding:utf-8-*-
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import time
# Create a new instance of the browser driver
driver = webdriver.Chrome()  ##可以替换为IE(), FireFox(),但前提是配置了各自浏览器的Driver,火狐在我自己使用中如果浏览器和驱动不兼容,浏览器在启动和退出是会报停止工作,此处注意
# go to the google home page
driver.get("http://www.google.com")
# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")
# type in the search
inputElement.send_keys("Cheese!")
# submit the form. (although google automatically searches now without submitting)
inputElement.submit()
# the page is ajaxy so the title is originally this:
print driver.title
try:
# we have to wait for the page to refresh, the last thing that seems to be updated is the title
WebDriverWait(driver, 10).until(lambda driver : driver.title.lower().startswith("cheese!"))
# You should see "cheese! - Google Search"
print driver.title
finally:
driver.quit()
#==================================

TAG:

 

评分:0

我来说两句

日历

« 2024-04-21  
 123456
78910111213
14151617181920
21222324252627
282930    

我的存档

数据统计

  • 访问量: 21483
  • 日志数: 8
  • 建立时间: 2015-10-08
  • 更新时间: 2015-10-13

RSS订阅

Open Toolbar