Selenium + Python automated test environment setup

Source: Internet
Author: User
Tags seleniumhq

 

Selenium is a Web automated testing tool. Many students who learn the automation of learning functions have begun to prefer selenium because it has many advantages over qtp:

* Free, and no longer have to worry about cracking qtp

* Small, it is only a package for different languages, and qtp needs to download and install one multi-GProgram.

* This is also the most important point. Whether you are familiar with C, Java, Ruby, Python, or C # before, you can perform automated testing through selenium, while qtp only supports vbs.

* Supports multiple platforms: Windows, Linux, and Mac. Supports multiple browsers: IE, FF, Safari, opera, and chrome.

* Supports execution of distributed test cases and distributes test cases to different test machines. This is equivalent to the distribution machine function.

 

With regard to the combination of basic selenium knowledge and Java platform, I have previously written a series of "cainiao learning automated testing" and recently learned python, so I 'd like to try how to build selenium on the python platform; okay.ArticleIt is very easy. Here we will build a step to sort out and share.

 

Build platform windows

The preparation tool is as follows:

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

Download Python

Http://python.org/getit/

Download setuptools [Python Basic Package tool]

Http://pypi.python.org/pypi/setuptools

Download Pip [Python installation package management tool]

Https://pypi.python.org/pypi/pip

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

Because all versions are updated, select 2.7.xx for pyhton and the corresponding version for setuptools. Pip is available in the same way in windows.

 

Installation steps:

 

1. Python installation. This is not an explanation. Run and install the EXE file. Now that you are using python, I believe you are familiar with python. The installation directory is c: \ python27.

 

2. The installation of setuptools is also very simple. It is also an EXE file. By default, the python installation path will be found and installed under the c: \ python27 \ Lib \ Site-packages directory.

 

3. Install Pip. By default, I decompress it to the C: \ pip-1.3.1 directory.

 

4. Open the command prompt (start --- cmd press Enter) to enter the c: \ pip-1.3.1 directory and enter:

C: \ pip-1.3.1> Python setup. py install

(If you are prompted that python is not an internal or external command! Don't worry, configure the environment variable)

Modify my computer> Properties> advanced> environment variables> path in system variables:

Variable name: Path

Variable value:; C: \ python27

 

5. Switch to the C: \ python27 \ scripts directory and enter:

C: \ python27 \ scripts> easy_install Pip

 

6. Install selenium, (: https://pypi.python.org/pypi/selenium)

If it is in the network status, you can directly run the following command in c: \ python27 \ scripts:

C: \ python27 \ scripts> Pip install-u Selenium

If you are not connected to the Internet (this is generally not possible), download selenium 2.33.0 (the latest version)

Decompress the package and place the entire directory in the C: \ python27 \ Lib \ Site-packages directory.

 

7. Download and install (http://www.java.com/zh_CN/download/chrome.jsp? Locale = zh_cn) What? You have never completed java. Please refer to other documents! This is not difficult.

 

8. Download selenium server (https://code.google.com/p/selenium/) found in the list on the left side of the page

Selenium-server-standalone-XXX.jar

Yes! Download and decompress the package;

Start with the command Java-jar selenium-server-standalone-xxx.jar in the selenium-server-standalone-xxx.jar directory (if not open, check whether the port is occupied: netstat-Aon | findstr 4444 ).

 

Congratulations ~! You have already done your preliminary work. The above steps are indeed cumbersome, but not difficult. But we have already completed them. Let's try the results below! Take the python website as an example:

 

 From Selenium Import  WebDriver  From Selenium. Common. ExceptionsImport  Nosuchelementexception  From Selenium. WebDriver. Common. Keys Import  Keys  Import  Timebrowser = WebDriver. Firefox () #  Get local session of Firefox Browser. Get ( "  Http://www.yahoo.com  " ) #  Load page Assert   "  Yahoo!  "   In  Browser. titleelem = 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 () 

 

(If an error occurs during running:

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 .'

Change Internet Options of IE-> Security, and deselect or hide all enable protection modes in Internet, local Internet, trusted sites, and Restricted Sites .)

 

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

A good document for selenium + Python

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

 

========================================== Browser (ie chrome) run the script ====

 

Install chrome driver

Here is the chrome driver.

1. Download the package and you will get a chromedriver.exe file (I open it and run the prompt started no prot 9515. Why? Is port 9515 occupied? Later, I realized that I needed to put this guy in the chrome installation directory... \ google \ chrome \ Application \, set the PATH environment variable, and set the chrome installation directory (My: C: \ Program Files \ google \ chrome \ Application ), then run the command:

#Coding = UTF-8FromSeleniumImportWebDriverDriver=WebDriver. Chrome ()Driver. Get ('Http://radar.kuaibo.com')PrintDriver. titledriver. Quit ()

An error was reported:

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

If my chrome version is not later than 27.0.1453.0, update it to the latest version.

 

Install ie driver

In the new version of Webdriver, only ie driver is installed for testing.

Here, you can download the corresponding Driver Based on the operating system version of your machine.

I haven't tried it yet. It should be similar to Chrome's installation method.

 

Remember to configureIEProtection Mode

If you want to useWebDriverStartIEThen you need to configureIE.

SetIEThe protection modes in can be selected or checked out.

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.