Web Automation Framework Based on Selenium+Python


First, what is Selenium?

Selenium is a browser-based automation tool that provides a cross-platform, cross-browser end-to-end web automation solution. Selenium mainly consists of three parts: Selenium IDE, Selenium WebDriver and Selenium Grid:

1. Selenium IDE: an extension of Firefox, which can record and play back, and can export recorded operations into test cases in multiple languages (such as java, python, etc.).

2. Selenium WebDriver: Provides API required for Web automation, mainly used for browser control, page element selection and debugging. Different browsers require different WebDriver.

3. selenium Grid: Provides the ability to run Selenium tests on different browsers of different machines

This paper uses Python and Selenium WebDriver library to build an automated testing framework.

II. Automated Test Framework

A typical automated testing framework generally includes a use case management module, an automated execution controller, a report generation module and a log module, which complement each other.


Next, introduce the logic units of each module:

1. Use Case Management Module


2. Automatic controller


3. Report Generation Module


4. log module


III. Design and Implementation of Automation Framework

1. Demand Analysis

First of all, our test object is a web platform. The framework designed based on this platform should include test case management, test execution controller, test report and test log generation.

2. Design and Implementation


Assuming that the test web object is a typical single-page application, we adopt page mode. Page mode

It is the link between a page and a test case. It abstracts each page into a separate class and provides the location and operation of page elements for the test case.



From the page, the elements to be operated are respectively login user name, password, next automatic login and login button. The specific implementation code is as follows:

Page parent BaseClass.py


LoginClass inherits from BaseClass and locates the login elements and implements the operation. The code locates username and password, and adds the operation of setting username and password.



The common library module serves to create test cases, mainly including constants, common functions, logs, reports, etc.



The test case information class is used to identify test cases and includes execution cases and execution result information, mainly including the following fields.


Logs are mainly used to record the execution steps of test cases and error messages generated. Different messages have different log levels, such as Information, Warning, Critical and Debug. Because each test case produces fewer log entries, only the highest level of log printing, namely Debug level, is used in the test framework, and this level will print all other log level information as well. In order to control log output more conveniently, logging class library in Python standard library is referenced in the concrete implementation.


Use case warehouse is mainly used to organize automated test cases. Each test case is abstracted into an independent class and is inherited from the unittest.TestCase class. The unittest library in Python provides rich test framework support, including setUp and tearDown methods of test cases, which can be rewritten in the process of realizing the use cases. Relying on the page method and public function implemented by the page management and public library module, the writing of each test case script will be very clear and concise.


From this test case, we can see that

Some instantiation work before executing test cases is defined in Setup.

TearDown cleaned up and wrote log files after performing the test.

Test steps, test data and test checkpoints are very clear and easy to modify (such as user name and password)

The Log level is only Debug, so only the same log method is needed to write the log.

3) Use case execution module

The execution module is mainly used to control the batch execution of test case scripts to form a test set. The execution of use cases refers to subprocess in Python standard library to execute shell commands of nosetes, thus executing the use cases in a given test case set. The test case set is a simple plain text file, and the. txt file testcases.txt is used in the implementation process.



Four, need to improve the module

For the existing test framework, it can already meet the automation requirements of web objects, but there are still some areas that can be improved, such as:

1) Some use cases can try to be data-driven

2) Encapsulate selenium’s By function twice to locate elements more efficiently

3) No continuous integration has been carried out

V. summary

The web automation framework based on Selenium is not only lightweight but also flexible. It can quickly develop automated test cases. Combined with the framework design of this article and some good practices, I hope it will be helpful for the design and implementation of future web automation framework.

Yixin Institute of Technology