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. 安装并配置python
  2. 通过pip命令安装selenium工具
  3. 下载并安装浏览器的WebDriver程序
  4. 测试demo脚本

具体安装操作:
  1. 去官网下载Python安装包https://www.python.org/
  2. 安装完成后在系统环境变量添加python安装根目录,及根目录下的scripts目录
  3. 直接使用pip安装selenium,命令为:pip install -U selenium
  4. 下载浏览器的驱动程序http://www.testqa.cn/download,并把exe文件存放到任意系统环境目录下,建议存放在python安装根目录或scripts目录
  5. 在命令行调用测试脚本【python demo.py】

如果测试成功会看到打开浏览器后进行google搜索。另外selenium分版本1和版本2,这里安装是版本2的selenium。
附:demo的脚本内容如下
#!/usr/bin/python
# -*- 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()


# 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()

======================================
FireFox的驱动已经在Selenium的安装包中了,无需额外安装
ie、chrome都需要下载并安装额外的exe驱动程序

======================================
  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上帝De助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值