多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

Selenium+Python的自动化测试框架搭建

更新时间:2022-04-27 09:00:58 作者:多测师 浏览:167

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

Selenium+Python的自动化测试框架搭建

  关于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()

  #==================================

  以上内容为大家介绍了Selenium+Python的自动化测试框架搭建,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部