Selenium + python automated test environment setup

Selenium is also a tool for web application testing. Selenium tests run directly in the browser, just like a real user. Supported browsers include IE, Mozilla Firefox, Mozilla Suite, etc. The main functions of this tool include: Test compatibility with browsers - test your application to see if it works well on different browsers and operating systems. Test system functionality - Create regression tests to verify software functionality and user requirements. Supports automatic recording of actions and automatic generation. Net, Java, Perl and other test scripts in different languages. Selenium is an acceptance testing tool written by ThoughtWorks specifically for web applications.

 

Selenium is an automated testing tool for the web. Many students who learn functional automation begin to prefer selenium because it has many advantages compared to QTP:

* Free, no need to worry about cracking QTP

* Small, it is just a package for different languages, and QTP needs to download and install more than 1 G program.

* This is also the most important point, whether you are more familiar with C, java, ruby, python, or all C# before, you can complete automated testing through selenium, and QTP only supports VBS 

* Support multiple platforms: windows, linux, MAC, support multiple browsers: ie, ff, safari, opera, chrome

* Supports the execution of distributed test cases, which can distribute test cases to different test machines for execution, which is equivalent to the function of the distributor.

 

Regarding the combination of the basic knowledge of selenium and the java platform, I wrote a series of "Rookie Learning Automated Testing" before, and I recently learned python, so I want to try how to build selenium on the python platform; fortunately, the article on this method is very easy, in This will build the steps to organize and share.

 

Build platform windows 

The preparation tools are as follows:

-------------------------------------------------------------

download python

https://www.python.org/

-------------------------------------------------------------

If you are new to python, you must depend on pyhton2.x if you don't have a package to use, then please choose python3.5 without hesitation!

 

 

Windows installation steps:

 

  1. Download python installation.

  https://www.python.org/downloads/release/python-351/

 

Select the corresponding version according to your own operating system 32/64 bit.

I don't need to describe the installation process. My installation directory is: C:\Python35

 

 2. Enter cmd (windows command prompt) and enter the "python" command.

(If it prompts that python is not an internal or external command! Don't worry, go and configure the environment variables)

Modify the PATH in My Computer -> Properties -> Advanced -> Environment Variables -> System Variables to:

variable name: PATH

Variable value: ;C:\Python35;C:\Python35\Scripts; 

 

3. Install selenium

3.1. Install via pip

C:\Users\fnngj>python3 -m pip install selenium 

3.2, install through the download package

Or download the selenium package directly:

https://pypi.python.org/pypi/selenium

Unzip, cmd enters the directory:

C:\selenium\selenium2.53.5> python3 setup.py install

 

 

===============================================

 

 

Installation method under ubuntu:

 

1. Installation: setuptools

root@fnngj-H24X:~# apt-get install python-setuptools

2. Install pip

root @ fnngj-H24X: / home / fnngj / python # tar -zxvf pip-1.4.1.tar.gz

root@fnngj-H24X:/home/fnngj/python# cd pip-1.4.1/

root@fnngj-H24X:/home/fnngj/python# python setup.py install

3、安装selenium

root@fnngj-H24X:/home/fnngj/python/pip-1.4.1# pip install -U selenium

 

恭喜~! 你前期工作已经做了,上面的步骤确实有些繁琐,但是并不难,不过我们已经完成成了,下面体验一下成果吧! 拿python网站上的例子:

 

复制代码
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo!" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()
复制代码

 

(运行过程中如果出现错误: 

WebDriverException: Message: u'Unexpected error launching Internet Explorer.

 Protected Mode settings are not the same for all zones. Enable Protected Mo

de must be set to the same value (enabled or disabled) for all zones.' 

更改IE的internet选项->安全,将Internet/本地Internet/受信任的站定/受限制的站点中的启用保护模式全部去 掉勾,或者全部勾上。)

 

-----------------------------------------

selenium + python的一份不错文档

http://selenium.googlecode.com/git/docs/api/py/index.html

 

===========================如果想通过其它浏览器(IE Chrome)运行脚本=================================

 

安装Chrome driver

chrome driver的下载地址在这里

  1. 下载解压,你会得到一个chromedriver.exe文件(我点开,运行提示started no prot 9515 ,这是干嘛的?端口9515被占了?中间折腾了半天),后来才知道需要把这家伙放到chrome的安装目录下...\Google\Chrome\Application\ ,然后设置path环境变量,把chrome的安装目录(我的:C:\Program Files\Google\Chrome\Application),然后再调用运行:

复制代码
# coding = utf-8

from selenium import webdriver

driver = webdriver.Chrome()

driver.get('http://radar.kuaibo.com')

print driver.title

driver.quit()
复制代码

又报了个错:

Chrome version must be >= 27.0.1453.0\n  (Driver info: chromedriver=2.0,platform=Windows NT 5.1 SP3 x86)

说我chrome的版本没有大于27.0.1453.0 ,这个好办,更新到最新版本即可。

 

安装IE driver

在新版本的webdriver中,只有安装了ie driver使用ie进行测试工作。

ie driver的下载地址在这里,记得根据自己机器的操作系统版本来下载相应的driver。

暂时还没尝试,应该和chrome的安装方式类似。

 

记得配置IE的保护模式

如果要使用webdriver启动IE的话,那么就需要配置IE的保护模式了。

IE里的保护模式都选上或都勾掉就可以了。

 

 

 乙醇的安装方式:

 http://easonhan007.github.io/python/2013/05/07/setup-env/

5分钟安装好selenium webdriver + python 环境:

http://v.youku.com/v_show/id_XNjQ1MDI5Nzc2.html?qq-pf-to=pcqq.group

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326388807&siteId=291194637