Web Automation Framework Based on Selenium+Python

  test

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.

1.png

Next, introduce the logic units of each module:

1. Use Case Management Module

 用例管理模块包括新增、修改、删除等操作单元,这些单元又会涉及到用例书写模式,测试数据库的管理、可复用库等。

2. Automatic controller

   控制器是自动化用例执行的组织模块,主要是负责以什么方法执行我们的测试用例

3. Report Generation Module

   主要负责执行用例后的生成报告,一般以HTML格式居多,信息主要是用例执行情况。另外还可以配置发送邮件功能。

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

   1)页面管理

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.

   BaseClass作为父类只包含driver成员变量,用来标识Selenium中的WebDriver,以便在子类中定位页面元素。LoginClass和PageClass作为子类,可以提供页面元素的定位和操作方法。比如登录页面。

2.png

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

3.png

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.

4.png

2)公共库模块

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

Common.py

5.png

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

6.png

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.

   3)测试用例仓库

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.

7.png

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.

8.png

用例前没有“#“标记的测试用例脚本会被执行,而有”#“标记的则会被忽略,这样可以很方便的控制测试集的执行,当然也可以创建不同的文件来执行不同的测试集。 

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