Selenium+python+eclipse Web Automation Integrated environment configuration (with simple test procedures)

Source: Internet
Author: User



Recently, the company is to give us training, mainly web automation testing, and now the work is a daily test app, just getting started, but when I read someone else to write the bug, I do not feel able to find out how bad the bug.



The first two weeks of time has been set up in the environment of automated testing, because there is no time to work during the day to do exercises, only after work time to learn about their own, bitter force ah. Every day is more than 9 from the company back to the dormitory, but to say the truth, such a day is really substantial, anyway, such a good work environment must cherish, learn more things, earn more money to go home to honor Dad, he himself a person to carry our whole family, also really not easy. Well, the nonsense is not much to say, directly into the subject bar.






  1 JDK, Python environment variable configuration



Download JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html, directly double-click the installation, it is recommended to directly click "Next", select the default path installation. Once the JDK is installed, configure the JDK's environment variables.



Right-click My Computer >> Properties >> Advanced >> Environment variables



New system Variable: variable java_home value C:\Program files\java\jdk1.6.0_24, you have to choose this value according to your own installation path



variable classpath value.;   %java_home%\lib\dt.jar;%java_home%\lib\tools.jar remember there's a. Represents the current path



Edit variable path Add%java_home%\bin;%java_home%\jre\bin



After these steps, the JDK environment variable is configured successfully. How is the test successful? The answer is as follows: Typing java in the cmd command window, javac if prompt for help information indicates a successful configuration, or if the prompt command does not exist, indicates that the configuration was unsuccessful.



The following is a graphic introduction such as:













To configure the CLASSPATH variable, remember that there is a previous one.



Append the above java_home and classpath to Path






  2 Downloads Eclipse



Official website: http://www.eclipse.org/downloads/.



Once the download is complete, you can run it directly by double-clicking. During the first run, a welcome screen appears and prompts you to select the workspace storage path. This path can be set according to your own situation, it is recommended that the path does not contain Chinese characters, or future projects may be in the process of error.



  3 Download the Python installation.



Python2 and Python3 a bit different, I download here is Python2.7.2 this version. : https://www.python.org/downloads/Select 32bit or 64bit installation package according to the number of digits of your computer



After downloading the Python installation package, you can simply double-click to complete the installation, it is recommended to choose the default installation, directly click "Next". After you have installed Python, configure the environment variables for Python.



In fact, it's easier to configure Python's environment variables than to configure the JDK's environment variables, so we just need to append the Python installation path to the path system variable, such as:









  4 integrating Eclipse and Python together



In the Eclipse's menu bar, click Help--> Eclipse Marketplace ..., in the popup window, there will be a search box, enter Pydev in it, this is mainly used to search Pydev, after the search, click Install. After install, restart Eclipse, then in the Eclipse menu, select Windows-->preferenc-->pydev--->interpreters--->python Interpreter, such as:









After configuration is complete









  5 Downloads Selenium



Official website: http://seleniumhq.org/download/



The Selenium consists of three main parts: Selenium IDE, Selenium Server, Selenium Client Drivers. We need to download the three parts and install them.



The first download is the Selenium IDE. The Selenium IDE is a Firefox plugin that can record scripts while translating scripts into scripts of different scripting languages. Selenium IDE after downloading, open Firefox browser, and then drag the Selenium IDE this compressed package to Firefox browser, Firefox automatically prompts to install, choose the default installation.



Selenium server is a core part of Selenium, which is written in JavaScript, which allows the test script to run in a supported browser. The downloaded selenium server file is a jar package, the installation method is to open cmd, go to the Selenium server path, directly enter: Java-jar Selenium-server-2.41.0.jar can complete the installation.



Selenium Client Drivers because I'm using the Java language, the package I chose to download is the Java version,












  6 Installing SETUPTOOLS-0.6C11



Setuptools is a convenient tool for Python to install third-party libraries and supports online installation. Let's download the SETUPTOOLS-0.6C11 first: Http://pypi.python.org/pypi/setuptools/0.6c11. You download the installation yourself according to whether your computer is 32-bit or 64-bit.






  7 Installing PIP



PIP supports online installation. We open cmd, directly enter the CD c:\Python27\Scripts carriage return, and then enter Easy_install.exe pip, you can download the installation online.






  8 Installing third-party packages



In fact, the above two steps mainly for the cushion here, with the above two steps, we can easily install the third party package.



Third-party packages are:



Selenium



XLWT (write to version 97 of Excel)



XLRD (read in version 97 of Excel)



Xlutils (Modifying an existing version 97 Excel document)



MySQL (Mysql-python, support for querying and writing MySQL data)



OPENPYXL (. xls and. xlsx are supported, but may fail if the document contains some special formulas)



Yaml (Pyyaml)



The installation method is also simple:



Open cmd,



Input CD c:\Python27\Scripts



Pip Install XLWT,



And so on, the above listed 7 packages are installed to finish. If some packages cannot be installed online, you can go directly to the official website to download them and install them in cmd mode.






  9 installation and download of driver



The driver that need to be installed are Google and IE, respectively:



Chromdirver:http://chromedriver.storage.googleapis.com/index.html



Iedriver:http://selenium-release.storage.googleapis.com/index.html



They are primarily used to drive open Web pages. After downloading, put these 2 exe files in the Python installation directory, such as: C:\Python27.






10 It's time to witness the miracle.



After doing all of the above, we can write our first program. The main function of this program is: Open a Google browser, and then, in the input box input beyond (I prefer the band), complete the automation of the Web page.



The program source code is as follows:


#-*-coding: utf-8-*-This is mainly to set the coding method
‘‘ ‘
Created on 2014-08-09

@author: Administrator
‘‘ ‘
#! / usr / bin / python

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
import time

# Instantiate a driver class
driver = webdriver.Chrome () #

# Open Google Chrome
driver.get ("http://www.youku.com")

# Find the element by the attribute q (Google search box)
inputElement = driver.find_element_by_name ("q")

# Enter beyond in the search box
inputElement.send_keys ("beyond")

# Submit search information
inputElement.submit ()


print driver.title

try:
   
     WebDriverWait (driver, 10) .until (lambda driver: driver.title.lower (). Startswith ("beyond"))

   
     print driver.title

finally:
    pass

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





If you see Google Browser automatically open, then you have taken the first step of automation.









The first technical blog post finally finished, you can go to bed, tomorrow to continue to work .....






  World Goodnight ...


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.