Frame design of Python+selenium

Source: Internet
Author: User

First, automated testing framework

1. What is an automated testing framework

Simply put, the automated test framework consists of a number of standards, protocols, and rules that provide the environment in which the script runs. Automated testing frameworks provide a lot of convenience for users to do things efficiently, such as structured development scripts, multiple ways, platform execution scripts, and good reports to track script execution results.

The framework has some advantages:

1) Code Reuse

2) Maximum Coverage

3) Very low cost maintenance

4) Minimal manual intervention

5) Simple Report output

2. Common Test Framework Classification

Many selenium automated test developers have QTP over there, so their early design frameworks can be broadly divided into the following categories:

1) module-based test framework

2) Structure testing framework based on libraries (library)

3) Data-driven test framework, much like QTP

4) keyword-driven test framework, also QTP.

5) Hybrid Test box, 3 4 and integrated

6) Behavior-driven Development testing framework

Here we do not introduce these frameworks, literally can imagine some of these framework background and components, this article just have a basic understanding of the framework.

3. Framework Basic Components

Let's consider the following framework components:

1) requires profile management

2) Separation of business logic code and test scripts

3) Report and log file output

4) Packaging of custom libraries

5) Manage and Execute script mode

6) Introduction of third-party plugins

7) Continuous Integration

Explain:

We need a configuration file to control some, environment information, switch, config file can be Txt/xml/yaml/properties/ini, general. Properties are used more in Java, this is the Python series, I might choose INI file.

The business logic code and the test script are detached, unlike when we first learned selenium, the code and the script are shown in a class file. We didn't use code refactoring, multiplexing. After separating the code and use-case files, it is clearer to develop scripts for many people to facilitate debugging.

Report and log file output, how much you performed case,case results, which all need to be reported to show, the general use of third-party plug-ins to achieve this function, a lot of report format is HTML, simple, clear style. Log output is also important, if an error occurs, script execution fails, and the log quickly locates the problem location.

User-defined library, this is very well understood, many of our functions need to be repeated, so we write a common method, placed under the toolkit, each time convenient to call, such as the browser engine class and basepage.py encapsulation.

The way in which scripts are managed and executed, such as in Python, the Unit test framework unittest usage is very high.

Third-party plug-ins, sometimes, some of our features, need to use third-party plug-ins, can be better implemented, such as AutoIt, to achieve file upload and download. There is also the use of third-party report plugins to generate test reports based on HTML format.

Continuous integration, git,svn,ant,maven,jenkins, we will integrate this into Jenkins to achieve continuous integration, one-click execution of test scripts.

Based on the above features, I'm going to describe briefly how a simple automated testing framework might contain components.

A brief introduction to the framework here, some people think of the framework is too complex, the framework is nothing more than a collection of software, to achieve a specific purpose. Here we draw the framework, is a simple automated test framework, do not laugh, this is really a simple but also contains the necessary components of the automated test framework design example, if you learn this design ideas and ideas, then, You have reached the level of the second phase of automated testing: the ability to easily design automated test frameworks and maintain frameworks.

Ii. Brief introduction of unittest Unit Test framework

UnitTest is a unit test framework that is a unit test framework for Python programming.

UnitTest supports test automation, sharing initialization and closing exit codes in test cases, and in UnitTest, the smallest unit is test, which is a testing case. To understand the UnitTest unit testing framework, here are a few important concepts to understand first.
Test Firmware(Test fixture)
A test firmware consists of two parts, the preparation part before executing the test code, and the sweep code after the test is finished. These two sections are generally represented by the function setup () and teardown (). Here, for example, to test Baidu search Selenium this scenario, our test firmware can write this, setUp () to open the browser, browser maximization, and open the Baidu homepage and other script code, in teardown () write the end of the search, exit and close the browser code.
Test Cases(Test case)
The smallest unit managed in UnitTest is a test case, a test case, including a test firmware, and a function or method that specifically tests the business. In a test case, the test firmware may not be written, but there is at least one function that begins with test. UnitTest will automatically identify the function at the beginning of the test code, if you write a function is not the beginning of test, UnitTest will not execute the script inside the function, this must be remembered, all the test function to the beginning, remember that is lowercase oh.
test Suite(Test suite)
Very simply, a collection of many test cases, called a test suite, where a test suite can manage multiple test cases at will. If the test case is likened to a single student, the test suite is like a class concept.
Test Actuator(Test Runner)
Test Runner is a build that executes the load test case, executes the use case, and provides the test output. Test runner can load tests case or testing suite to perform the task.

For example, let's practice the use of test fixture and test case to learn the simple usage of unittest:
1. Create a new testbaidu.py file
2. Import the UnitTest module
3. The current test class inherits UnitTest. TestCase, equivalent to the current use of unittest to create a test case, the test case can be directly identified by UnitTest.
4. Write Setup (), mainly open the browser and open the site
5. Write a test_search () use case to write the search code
6. Write Teardown (), mainly browser exit operation

The relevant script code is as follows:

#Coding=utf-8Import TimeImportUnitTest fromSeleniumImportWebdriverclassBaidusearch (unittest. TestCase):defsetUp (self):"""the code for the test firmware setup () is primarily a prerequisite for testing: return:"""Self.driver=Webdriver. Chrome () Self.driver.maximize_window () self.driver.implicitly_wait (8) Self.driver.get ("https://www.baidu.com")        defTearDown (self):"""after the end of the test, this is basically closing the browser: return:"""self.driver.quit ()defTest_baidu_search (self):"""It is important to test the logic code into a method at the beginning of testing. : Return:"""self.driver.find_element_by_id ('kw'). Send_keys ('Selenium') Time.sleep (1)          Try:              assert 'Selenium' inchSelf.driver.titlePrint('Test Pass.')          exceptException as E:Print('Test Fail.', Format (e))if __name__=='__main__': Unittest.main ()

Explain:

At the end of the Unittest.main (), add this is supported in CMD, inside, CD to the directory where this script file resides, and then Python The script name. PY execution, if not add this paragraph, is unable to execute CMD inside run script, in Pycharm, do not add the last paragraph, also can pass, right click Run "UnitTest xxx", to achieve the effect of execution.

third, what is Pom

Pom, Chinese alphabet meaning, the Page object model, Pom is a very popular automated test model in recent years, or thought, POM is not a framework, is a problem-solving idea. The purpose of using POM is to resolve the UI changes frequently in the front end, resulting in increased cost of test automation script maintenance. , the image describes the benefits of POM.

From the see, take the pom design ideas and do not take the difference, the left side of the test Code and page elements are written in a class file, if you need to change the page, then you have to modify the page element positioning, so to modify the test code in this class, this looks and confusion. On the right, after taking the Pom, the main difference is to separate the page elements from the business logic and test scripts into two different classes of files. ClassA only writes page element positioning, and the encapsulation of business logic code operations, CLASSB only writes test scripts, does not care how element is positioned, writes only calls ClassA code to cover different test scenarios. If the front-end page changes, you only need to modify the ClassA element positioning, without having to modify the test script code in the CLASSB.

Pom mainly has the following advantages:

1. Detach the Web UI object repository from the test script, separating the business code from the test script.

2. Each page corresponds to a page class, and the elements of the page are written to this page class.

3. The page class mainly includes the element positioning of the page, and the method encapsulated by the business operation code associated with those elements.

4. Code reuse, which reduces the amount of test script code.

5. Clear hierarchy, while supporting the development of multiple writing automation scripts, such as how many pages each person writes, does not affect others.

6. It is recommended that both the page class and the business logic approach give a meaningful name to make it easy for others to quickly write scripts and maintain scripts.

Iv. in-Frame encapsulation log class and browser engine class

 

Frame design of Python+selenium

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.