Selenium automation test based on Python language

Source: Internet
Author: User
Tags tag name selenium grid

I. Introduction of Selenium

Selenium is a Web Automation application testing tool that also supports the automation of all Web management tasks. and open source is free and a lightweight framework for Web automation. It supports multiple browsers, cross-platform, cross-operating systems, supports scripting in multiple programming languages, and enables the execution of distributed test cases.

Selenium experienced three editions, SELENIUM1, Selenium2, Selenium3. It is not a single tool, but a combination of some class libraries, plugins, tools, and so on. The main tools:

Selenium IDE : Selenium IDE is a plug-in embedded in Firefox, mainly used for browser operation error recording and playback, and the recorded script can be converted to multiple languages, you can develop test scripts in the language of conversion. Beginner learners can start with the plugin to learn selenium automated testing.

Selenium Grid : automated auxiliary tools to speed up the functional testing of Web-app.

Selenium RC : Selenium Core, can support a variety of different programming languages to write automated test scripts, such as common Java and Python language to write Selenium automated test scripts.

Webdriver : in the Selenium 2.0 version, the new Webdriver was added. Webdriver is developed specifically for individual browsers, replacing JavaScript embedded in the Web application under test. Tight integration with the browser enables the creation of more advanced tests, avoiding the limitations caused by the JavaScript security model. In addition to the support from browser vendors, Webdriver can also use OS-level calls to simulate user input. In addition to supporting individual browsers, it also supports Android and iphone mobile app testing.

second, the Windows test environment to build1. Install Python

This article is mainly based on the Python language of automated testing, the first to install Python. Download the latest version of the python3.6 on the web. If Windows is 32-bit, select the x86 version to download the installation and, if it is a 64-bit operating system, select version 64. This article takes version 64 as an example. After the download is complete, double-click the installation package to install it, select Custom installation, and tick automatically add environment variable as shown in:

(1) The installation process is the same as installing the general software, you can see the installed Python directory in the Start menu after the installation is complete, as shown in the following:

(2) After the installation is complete, you can open your own idle writing Python program:

2. PIP Installation

Pip is a tool for installing and managing Python packages. Importing some Python non-built class libraries, modules, tools, etc. are installed with PIP. But now that the version of the Python installation package is integrated PIP, we do not need to install it separately. To check if Python has integrated the PIP, we can see if there are files such as Pip.exe in the installation directory, such as: if you have a direct use of the PIP command.

3, SELENIUMD installation

The above mentioned installation PIP is to facilitate the installation of selenium package. The PIP command can install the selenium package directly, but it is best to choose the lower version when installing selenium, the general choice Selenium2.0 above, Selenium3.0 the following version is more appropriate. To install a version of selenium, you need to specify a version number to install in the command, for example:

>>>pip Install selenium==2.53.0

4, jetbrains pycharm installation

JetBrains Pycharm is a compiler for the Python development environment. Pycharm has the functionality of a general IDE, such as debug code, syntax highlighting, project management, smart tips, unit testing, and more.

(1) First download the installation package, and then customize the installation, according to the General software installation.

(2) After installation, the first use needs to be configured, the general choice is the default.

(3) After setting up the new project will prompt to select the Python version, select the path we installed.

5. Browser-driven installation

Webdriver supports a variety of browsers, but the browser driver needs to be installed to properly invoke the browser to run. Depending on your system and browser to download different versions of the driver, the downloaded driver files need to be placed under the system environment variable path, under the browser installation file, such as Firefox browser driver,

Iii. Basic knowledge of selenium automation testing

1. Invoking the browser driver, accessing the address method

Driver = Webdriver. Firefox ()

Driver.get ("url")

2. Positioning element Method

Id Location: find_element_by_id () ID must be unique

Name positioning:find_element_by_name ()

Class positioning:find_element_by_class_name ()

Tag positioning:find_element_by_tag_name ()

Link positioning:find_element_by_link_text () specifically locate text links

Partial Link positioning:find_element_by_partial_link_text ()

Xpath Positioning:

(1) Absolute path positioning

Find_element_by_xpath ("Html/body/div/div[2]/div/div/div/from/span/input")

(2) Element attribute positioning: Find_element_by_xpath ("//input[@id = ' kw ']")

(3) combination of hierarchy and attribute: Find_element_by_xpath ("//form[@id = ' form ']/span/input")

(4) using the logical operator Find_element_by_xpath ("//input[@id = ' kw ' and @class = ' su '/span/input]")

CSS Positioning:

(1) Positioning by class attribute: Find_element_by_css_selector (". S_ipt")

(2) Locating by id attribute: Find_elememt_by_css_selector ("#kw")

(3) Positioning by tag name: Find_element_css_selector ("input")

1) Locating by parent-child relationship: Find_element_css_selector ("Span>input")

2) by property positioning: Find_element_by_css_selector ("[name= ' kw ']")

3) by combination positioning: Find_element_by_css_selector ("Form.fm>span>input.s_ipt")

by positioning elements:find_element (by.id, "kw")

3. Set Browser size

Driver.set_window_size (480, 800): Set browser size (480,800)

4. Submit Input Box Data

driver.find_element_by_id (' Txtuser '). Send_keys (' Huang Wenting '): Submit login account Huang Wenting

5. Mouse Operation

The Actionchains class provides a common method of mouse operation:

Perform () performs the behavior stored in all Actionchains

Context_click () Mouse right-click

Double_click () mouse double-click

Drag_and_drop () Mouse drag

Move_to_element () mouse hover

6. Sleep Method

Sleep (3): Set sleep for 3 seconds

7. Form Switch

Switch_to_frame () can be switched directly from the form's ID or the Name property by default.

8. Multi-Window switching

Current_window_handle: Gets the current window handle

Window_handles: Returns the handle of all windows to the current session

Switch_to_window (): Used to switch to the appropriate window

9. Handling Warning Boxes

Text: Gets the prompt information in the alert/confirm/prompt.

Accept: Click the Confirm button.

Dismiss: Click the Cancel button.

Iv. Simple Example

The following is a simple automated login test case:

#coding =utf-8

From selenium import Webdriver

Import UnitTest

Import time

From selenium.webdriver.firefox.firefox_binary import firefoxbinary

Class Southinfomanageplatform (UnitTest. TestCase):

def setUp (self):

Print (' Start execution of test cases ')

Self.driver = Webdriver. Firefox ()

Self.driver.get ("http://192.168.10.42:8787/")

def tearDown (self):

# Self.driver.quit ()

Print (' End of test case execution! ‘)

def test_scenario_001 (self):

"A simple test scenario"

Print (' User login: ')

self.driver.find_element_by_id (' Txtuser '). Clear () #取消默认账号

self.driver.find_element_by_id (' Txtuser '). Send_keys (' Huang Wenting ') #用户名

self.driver.find_element_by_id (' Txtuser '). Clear ()

self.driver.find_element_by_id (' Txtpwd '). Send_keys (' 123 ') #密码

Logincode=self.driver.get_cookies () [1]["value"]

Self.driver.find_element_by_xpath (".//*[@id = ' Txtcheckcode ']"). Send_keys (Logincode)

#time. Sleep (3)

Self.driver.find_element_by_name (' Ibtnlogin '). Click ()

Time.sleep (3)

Selenium automation test based on Python language

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.