What is Selenium?

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

  • Selenium IDE: A Firefox extension that records playback and exports recorded actions as test cases in a variety of languages such as Java, Python, and so on.

  • Selenium WebDriver: Provides apis required for Web automation, primarily for browser control, page element selection, and debugging. Different browsers require different Webdrivers.

  • Selenium Grid: Provides the ability to run Selenium tests on different browsers on different machines.

This article will introduce how to build a Web automation testing framework using Python and Selenium WebDriver library in detail.

Second, automated testing framework

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

Next, the logical unit of each module is introduced:

1. Use case management module

Use case management module includes operation units such as adding, modifying and deleting, which will involve use case writing mode, test database management, reusable library and so on.

2. Automatic controller

The controller is the organizational module that automates use case execution and is primarily responsible for how our test cases are executed.

3. Report generation module

Mainly responsible for generating reports after the implementation of use cases, generally in HTML format, information is mainly the implementation of use cases. You can also configure the email sending function.

4. Log module

It is mainly used to record the use case execution, so as to efficiently investigate the use case failure information and track the use case execution.

Design and implementation of automation framework

1. Demand analysis

First of all, our test object is a Web platform, and the framework designed based on this platform includes test case management, test execution controller, test report and test log generation.

2. Design and implementation

Page management

Assume that the test Web object is a typical single-page application, so we use the page pattern. The page pattern is the link between the page and the test case, abstracting each page into a separate class that provides the positioning and manipulation of page elements for the test case.

BaseClass, as a parent class, contains only driver member variables that identify webDrivers in Selenium in order to locate page elements in subclasses. LoginClass and PageClass are subclasses that provide methods for locating and manipulating page elements. Like the login page.

The login user name, password, automatic login next time, and login button need to be operated. The specific implementation code is as follows:

Page parent class baseclass.py

LoginClass inherits from BaseClass and performs the element location and operation implementation of the login. Username and password are located in the code, and the actions to set the username and password are added.

Common library module

The common library module is used to create test cases, including constants, public functions, logs, reports, and so on.

Common.py

The test case information class identifies test cases and includes execution cases and execution results, mainly including the following fields.

Logs record the steps of test cases and error Information. Different logs have different log levels, such as Information, Warning, Critical, and Debug. Because there are fewer log entries per test case, only the highest log printing level, the Debug level, is used in the test framework, which also prints information for all other log levels. The logging library in the Python standard library is referenced in the implementation to facilitate the control of logging output.

Test case repository

The use case repository is primarily used to organize automated test cases. Each TestCase is abstracted into a separate class that inherits from the unittest.testcase class. The UnitTest library in Python provides rich test framework support, including test case setUp and tearDown methods that can be overridden during use case implementation. Depending on page management and common library module implementation of page methods and common functions, each test case script will be written very clear and concise.

From this test case, we can see that

  • Setup defines some instantiation before the test case is executed
  • TearDown cleans up after the test and writes to the log file
  • Test steps, test data, and test checkpoints are clear and easy to change (such as usernames and passwords)
  • The Log level is Debug. Therefore, only the same Log method is required 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 use cases references the Subprocess in the Python standard library to execute noseTests’ shell commands to execute the use cases in a given set of test cases. The testcase set is a simple plain text file that is implemented using the. TXT file testcases.txt.

Test case scripts without a “#” mark are executed, while those with a “#” mark are ignored. This allows easy control over the execution of test sets, and you can also create different files to execute different test sets.

4. Modules to be improved

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

  • Some use cases can try to be data-driven.
  • Selenium’s By function is reencapsulated to locate elements more efficiently.
  • There is no persistent integration.

Five, the summary

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

Author: Wu Guanxiang

Source: Creditease Institute of Technology