[Automated Testing] Web automation framework based on Selenium + Python

1. 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, it can record and playback, and can export the recorded operations into test cases in multiple languages (such as java, python, etc.).

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

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

This article uses Python combined with the Selenium WebDriver library to build an automated testing framework.

2. Automated testing 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. These modules complement each other.

1.png

The following introduces the logical units of each module:

1. Use case management module

 The use case management module includes operation units such as adding, modifying, and deleting. These units will also involve use case writing mode, test database management, reusable libraries, etc.

2. Automation controller

 The controller is the organizational module for automated test case execution. It is mainly responsible for how to execute our test cases.

3. Report generation module

 Mainly responsible for generating reports after executing use cases, usually in HTML format. The information is mainly about the execution status of use cases. In addition, you can also configure the function of sending emails.

4. log module

 is mainly used to record the execution of use cases in order to efficiently investigate use case failure information and track the execution of use cases.
Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when self-study, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036 [password: csdn999]

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

 1) Page management

Assume that the test web object is a typical single-page application, so we use page mode. Page mode

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

 BaseClass, as the parent class, only contains the driver member variable, which is used to identify the WebDriver in Selenium to locate page elements in the subclass. LoginClass and PageClass, as subclasses, can provide positioning and operation methods for page elements. Such as the login page.

2.png

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

Page parent class BaseClass.py

3.png

LoginClass inherits from BaseClass and performs login element positioning and operation implementation. The username and password are located in the code, and the operation of setting the username and password is added.

4.png

2) Public library module

The public library module serves to create test cases and mainly includes constants, public functions, logs, reports, etc.

Common.py

5.png

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

6.png

Logs are mainly used to record test case execution steps and error messages generated. Different information has different log levels, such as Information, Warning, Critical and Debug. Since each test case generates relatively few log entries, only the highest level of log printing, the Debug level, is used in the test framework. This level will also print out information from all other log levels. In the specific implementation, the logging class library in the Python standard library is referenced to make it more convenient to control log output.

 3) Test case warehouse

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

7.png

From this test case, we can see

Setup defines some instantiation work before executing the test case

tearDown cleans up and writes log files after executing the test

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

The log level is only Debug, so you only need to use the same Log method to write logs.

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 the test case refers to the subprocess in the Python standard library to execute the shell command of nosetests, thereby executing the use cases in the 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.

8.png

Test case scripts without a "#" mark before the test case will be executed, while those with a "#" mark will be ignored. This makes it easy to control the execution of the test set. Of course, you can also create different files for execution. Different test sets.

4. Modules that need improvement

The currently implemented testing framework can already meet the automation needs 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) Secondary encapsulation of selenium’s By function to position elements more efficiently

3) No continuous integration

5. Summary

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

END Today’s sharing ends here, please like and follow to avoid getting lost~

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeWeb crawlerSelenium388106 people are learning the system