Selenium + web-based automated testing framework for Python

First, what is Selenium?

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

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

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

  • Selenium Grid: provides the ability to run selenium tests on different machines in different browsers.

This article details how to use Python to build a library in conjunction with Selenium WebDriver web automated testing framework.

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.

17039196-e85c08d647501f70.png
image

Next comes the logic unit of each module:

1, with the management module cases

Use Case Management module includes add, modify, or delete operation units, which will involve the use case writing mode, the test database management, reusable libraries.

2, automation controller

The controller module is the automated organizational use cases executed, what method is mainly responsible for the implementation of our test cases.

3, report generation module

Mainly responsible for generating reports after execution of use cases, mostly general in HTML format, information is mainly the implementation of the use cases. It also can send e-mail configuration function.

4, log module

Primarily used for recording the implementation of the embodiment, in order to investigate the failure information for efficient tracking implementation examples and use cases.

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

Page Management

Hypothesis testing web object to a typical single-page application, so we use page mode. Is the link between the page mode and the test pages, each page will be abstracted into a single class, to provide the operation and positioning of page elements to test.

As BaseClass parent driver member comprises only variable that identifies Selenium in the WebDriver, page elements to be positioned in a subclass. LoginClass and PageClass as a subclass, and operation method can provide positioning of page elements. For example, the login page.

17039196-71e2cb29e5a7585e.png
image

From the page to see the elements need to operate are: login user name, password, and automatically log the next login button. Specific codes are as follows:

Page parent BaseClass.py

17039196-7a9b872fb0243989.png
image

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.

17039196-2f097d87b1186ed8.png
image

Public library module

Public library modules to create test cases for services, including constants, public functions, logs, reports, and so on.

Common.py

17039196-0ebea8fa1e18810b.png
image

Test to identify test class information, and includes cases execution, an execution result information, including the following fields.

17039196-13a7b15081b89f0f.png
image

Log mainly used to perform test recording step and the error information generation, we have different information of different levels of logging, such as Information, Warning, Critical and Debug. Because fewer log entries for each test case generation, so the test framework uses only the highest level of print logs that Debug level, which will also log all the other levels of information is also printed. Referenced Python standard library logging library In a particular implementation, in order to more easily control the log output.

Test Case warehouse

Use Case warehouse is mainly used to organize automated test cases. Each test case is abstracted into a separate class, and class inherit from unittest.TestCase. The Python unittest library provides a rich framework to support testing, including test cases setUp and tearDown methods, in the course of the use case implementations can be rewritten. Relying on page method and functions of public administration and public library modules page implementation, each test case script writing will be very clear and concise.

17039196-94297bb4e9cd4eb3.png
image

From this test case, we can see

  • Setup defines some examples of work before the implementation of test cases
  • tearDown for executing the test and do a clean-up work write log files
  • Test procedures, test data and test check point very clear, easy to modify (such as user name and password)
  • Just log level Debug, so just write the log using the same method Log

3, for example, execution module

Module is mainly used to control execution of a test case script batch execution, to form a test set. REFERENCE EXAMPLE execution of the Python standard library subprocess nosetests shell commands to execute to perform a given test set cases. Test suite is a simple text file, use the process to achieve a .txt file testcases.txt.

17039196-b8b053b263d92502.png
image

With the former cases there is no "#" mark test script will be executed, and the "#" mark will be ignored, so you can easily control the execution of the test set, of course, can create different files to perform different tests set.

Fourth, the need to improve the module

For existing testing framework to achieve, we have to meet the automation needs of web objects, but some can be improved to improve the place, such as:

  • EXAMPLE portion may attempt to use the data driver.
  • By encapsulating the secondary function of selenium, for more efficient targeting elements.
  • Integration is not continuous.

V. Summary

Help you design and implement future web automation framework web-based automated testing framework Selenium achieve not only lightweight but also flexible and can quickly develop automated test cases, binding framework design Benpian and some good practices, hope .

Author: Wu Guan Xiang

Source: CreditEase Institute of Technology

Further Reading: data sets: CreditEase agile data station construction practice | Share Record

Guess you like

Origin blog.csdn.net/weixin_34044273/article/details/90914277