歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Selenium + Python的自動化框架搭建

Selenium + Python的自動化框架搭建

日期:2017/2/28 16:02:24   编辑:Linux教程

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. from selenium import webdriver
  4. from selenium.common.exceptions import TimeoutException
  5. from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
  6. import time
  7. # Create a new instance of the Firefox driver
  8. driver = webdriver.Chrome()
  9. # go to the google home page
  10. driver.get("http://www.google.com")
  11. # find the element that's name attribute is q (the google search box)
  12. inputElement = driver.find_element_by_name("q")
  13. # type in the search
  14. inputElement.send_keys("Cheese!")
  15. # submit the form. (although google automatically searches now without submitting)
  16. inputElement.submit()
  17. # the page is ajaxy so the title is originally this:
  18. print driver.title
  19. try:
  20. # we have to wait for the page to refresh, the last thing that seems to be updated is the title
  21. WebDriverWait(driver, 10).until(lambda driver : driver.title.lower().startswith("cheese!"))
  22. # You should see "cheese! - Google Search"
  23. print driver.title
  24. finally:
  25. driver.quit()
  26. #==================================
Copyright © Linux教程網 All Rights Reserved