Selenium2+python Study Notes (i) Construction of automated test environment

Source: Internet
Author: User
Tags windows x86

Selenium2+python Automated Test environment construction

1. Preparatory work

1.1. Build platform: Windows

1.2. The preparation tools are as follows:

Download python:http://python.org/getit/

Download Setuptools "Python's Basic Package tool": Http://pypi.python.org/pypi/setuptools

Download Pip "Python's installation package management tool": HTTPS://PYPI.PYTHON.ORG/PYPI/PIP

Download Selenium:https://pypi.python.org/pypi/selenium (optional)

Download Browser driver: https://code.google.com/p/selenium/downloads/list (select Match chromedriver and Iedriver)

Because versions are updated, Python chooses 2.7.xx,setuptools to select the platform counterpart, Pip does not worry that tar.gz is available under Windows

My tools download situation (native environment Windows x86-64):

2. Installation Steps

2.1.python installation, Python-2.7.9amd64.msi run installation, I install directory C:\Python27

2.2.setuptools installation is also very simple, double-click ez_setup.py, the default will find the Python installation path, will be installed in the C:\Python27\Lib\site-packages directory

2.3. Install PIP, I unzipped in the C:\pip-6.1.1 directory by default

2.4. Configure Python environment variables

Modify My Computer, properties, advanced-environment variable--the path in the system variable is:

Variable name: PATH

Variable value:; C:\Python27

2.5. Open a command prompt (start---cmd carriage return) Enter the C:\pip-6.1.1 directory:

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

2.6. Switch to the C:\Python27\Scripts directory and enter:

C:\Python27\Scripts > Easy_install pip

2.7. Install selenium, if it is connected, you can enter the command installation directly under C:\Python27\Scripts:

C:\Python27\Scripts > Pip install-u Selenium

If there is no Internet connection (this is generally not possible), download Selenium 2.45.0 (currently the latest version) and unzip the entire directory into the C:\Python27\Lib\site-packages directory

2.8. Install the browser driver, due to the setting of the environment variables, can be said to download the driver directly extracted to the C:\PYTHON27 directory

3. Start the first script

If the author is the first to touch the Python language and the programming ability is weak, then I recommend using Python's own idle to write scripts. In order to better write python+webdriver scripts through the idle help, we need to know about idle first.

IDLE provides a fully functional code editor that allows you to write code in this editor, plus a python shell (Python interactive mode) where you can experiment with running code.

3.1. Open Python Shell

3.2. Enter the code and execute:

Effective use of idle:

TAB completion: Type some code first, and then press the TAB key. IDLE will provide some suggestions to help you complete this statement

Fallback code statement: Press Alt+p to fall back to the code statement you entered before idle, or press ALT + N to move to the next code statement.

4. First Automation Script

4.1. Open the Python Shell, enter from selenium import webdriver, import selenium related package, if no error after carriage return indicates that our selenium installation is successful.

4.2. Select the menu bar file--->new windows or open a new window by using the shortcut key CTRL + N. Enter the following code:

1 #coding = Utf-82  fromSeleniumImportWebdriver3 4Browser =Webdriver. Firefox ()5Browser.get ("http://www.baidu.com")6 7BROWSER.FIND_ELEMENT_BY_ID ("kw"). Send_keys ("Selenium")8BROWSER.FIND_ELEMENT_BY_ID ("su"). Click ()9 TenBrowser.quit ()

4.3. After the input completes the command for baidu.py saves, presses the F5 shortcut key to run the script, will see the script launches the Firefox browser to enter the Baidu page, enters "Selenium" to click the Search button, finally closes the browser the process.

4.4. Use IE and chrome to replace Firefox to run the above example.

Browser = Webdriver. Firefox ()
To be replaced by:
Browser = Webdriver. Ie ()
Or
Browser = Webdriver. Chrome ()

If the program can invoke the appropriate browser to run, it indicates that our browser driver installation was successful.

At this point, the Selenium2+python automated test environment is completed

Selenium2+python Learning Notes (i) building an automated test environment

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.