Python + Selenium's web automation framework

First, what is Selenium?

Selenium is a browser-based automation tool, she provides a cross-platform, cross-browser web-to-end automation solutions. Selenium mainly includes three parts: Selenium IDE, Selenium WebDriver and Selenium Grid:

 1, Selenium IDE: a Firefox extension, it can play back the recording, and the recording operation can be (e.g., Java, Python, etc.) into the form of test cases derived languages.

 2, Selenium WebDriver: to provide the necessary automation Web API, mainly used browser control, select the page elements and debugging. Different browsers require different WebDriver.

 3, Selenium Grid: provides the ability to run selenium tests on different machines in different browsers

As used herein, in conjunction with Selenium WebDriver Python library to build test automation frameworks.

Second, the automated testing framework

A typical automated testing framework embodiment generally comprises management module, automatic execution controller, and a report generation module log modules that complement each other.

1.png

Next, introduce the logic unit of each module:

1, with the management module cases

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

2, automation controller

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

3, report generation module

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

4, log module

   主要用来记录用例执行情况,以便于高效的调查用例失败信息以及追踪用例执行情况。

Third, the design and automation framework

1, needs analysis

First, we test object is a web platform, based on this platform design framework to include test case management, test execution controller generation, test reports and test logs.

2, design and implementation

   1)页面管理

Hypothesis testing web object to a typical single-page application, so we use page mode. Page Mode

Is the link between the page and the test case, each page will be abstracted into a single class, to provide the positioning and operation of page elements to test.

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

2.png

From the page to see the elements required to operate respectively, login user name, password, and automatically log the next login button, specific codes are as follows:

Page parent BaseClass.py

3.png

LoginClass inherited from BaseClass, and element positioning login and operation implementation. And locating the code username password, and adds the user name and operator password.

4.png

2)公共库模块

公共库模块是为创建测试用例服务的,主要包括常量、公共函数、日志、报告等。

Common.py

5.png

测试用例信息类用来标识测试用例,并且包括执行用例、执行结果信息,主要包括以下字段。

6.png

日志主要用来记录测试用例执行步骤及产生的错误信息,不同的信息有不同的日志级别,比如Information,Warning,Critical和Debug。由于每个测试用例产生的日志条目比较少,所以在测试框架中只利用了最高级别的日志打印,即Debug级别,该级别也会将其他所有的日志级别的信息同样打印出来。在具体的实现中引用了Python标准库中的logging类库,以便更方便的控制日志输出。

   3)测试用例仓库

用例仓库主要用来组织自动化测试用例。每条测试用例都被抽象成一个独立的类,并且均继承自unittest.TestCase类。 Python中的unittest库提供了丰富的测试框架支持,包括测试用例的setUp和tearDown方法,在实现用例的过程中可以重写。依托页面管理和公共库模块实现的页面方法和公共函数,每一个测试用例脚本的书写都会非常清晰简洁。

7.png

从这个测试用例中,我们可以看到

Setup中定义了执行测试用例前的一些实例化工作

tearDown对执行完测试做了清理和写日志文件工作

测试步骤、测试数据和测试检查点非常清晰,易修改(比如用户名密码)

日志级别仅有Debug,所以写日志仅需用同一Log方法

3)用例执行模块

执行模块主要用来控制测试用例脚本的批量执行,形成一个测试集。用例的执行引用了Python标准库中的subprocess来执行nosetests的shell命令,从而执行给定测试用例集中的用例。测试用例集是一个简单的纯文本文件,实现过程中利用了.txt文件testcases.txt

8.png

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

四、需要改进的模块

对于现有实现的测试框架,已经可以满足web对象的自动化需求,但还是有些可以改进提高的地方,比如:

1)部分用例可以尝试数据驱动

2)二次封装selenium的By函数,以便更高效定位元素

3)没有进行持续化集成

五、总结

Selenium web-based automation framework implemented not only lightweight but also flexible and can quickly develop automated test cases, binding framework design Benpian and some good practices, we hope for the future design and implementation of web automation framework to help.

Guess you like

Origin www.cnblogs.com/6J2B2/p/12145648.html