Implementation of Python+selenium+unittest GUI automation framework

Source: Internet
Author: User
Tags python mysql


Frame Design Illustration:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/80/F7/wKioL1dFjXuDuDEzAAhEjEalRlk754.jpg "title=" Python +selenium+unittest Automation framework illustration. jpg "alt=" wkiol1dfjxududezaahejealrlk754.jpg "/>


Preparation before the framework is implemented:

The 1.Python version is installed under Windows 2.7.11.

2. The latest version of selenium under the command line (Editor version 2.53.0, compatible with the latest Firefox, IE, Chrome and other browser versions) PIP installation.

3. Version Version:mars.1 release (4.5.1) for Eclipse installation (Editor Python development Using tool), jdk1.7.

4.eclipse python Development Plug-in Pydev 4.5.1 (high version is not necessarily compatible with the current version of Eclipse and Java version, after the successful installation, Eclipse can not find the corresponding Pydev plug-in).

5.Python MySQL Database (editor using data for MySQL) module to install mysql-python-1.2.4b4.win32-py2.7 (Note the module version and the number of system bits, the same as the installation of Python).

6.Python Anti-compilation Module Uncompyle-master installation (may be used).

7.Python Executable Package Module py2exe-0.6.9.win32-py2.7 installation (may be used).


The feasibility of the framework implementation:

1. Easy to extend, similar to Python's modular installation.

2. Easy to maintain, the latest version upgrade for selenium (compatible with the latest version of the browser, etc.) requires only the command line execution: Pip Install-u Selenium can be implemented.

3. The realization of the language is easy to learn, for Python learning as long as not too much pursuit of tall (refactoring, complex polymorphism, etc.), basic fast time can be done.

4. Each browser is compatible with other browsers (ie, chrome, etc.) that are available in other editors ' articles.


The framework realizes the idea to explain:


The beginning is verbose:

the way of thinking before, wordy two sentences (the problem is not changed off). Small series of colleagues have a lot of Daniel, write the code is very cow fork (function, performance, readability, etc.), the design of the project framework of various excellent, but as many project developers, it is difficult to achieve the framework design of the implementation of the idea, according to the initial framework of the implementation of the project. In the final analysis, because the project personnel failed to achieve the same ideological consensus, that is, the framework designers have not been able to convey the framework design ideas to the project personnel, not to allow the project personnel to achieve unity of thought, Causes the subsequent project to be unable to carry out and the implementation of various difficulties (project personnel a think that according to the framework can achieve sofa production, project personnel B is that according to the framework can only design the bench, the ideological disunity). More of the framework of the design is not better understood. No matter what you do, the editor always thinks the idea is the first place.


The test case code implements the storage module:

Import UnitTest
From time import sleep
From common.driverelement import driverelement

Class Crmlogintest (UnitTest. TestCase):
def setUp (self):
Self.driverelement=driverelement ()
Self.driver=self.driverelement.webdriverinit (' Http://crm.360.cn/login ')
def tearDown (self):
Self.driver.quit ()
def testname (self):
Self.driverElement.webdriverLogin (Self.driver)
Sleep (4)

The code is simple and defines an implementation class Crmlogintestfor the system login test, which inherits several methods defined by the TestCase interface of the UnitTest module: setUp, TearDown, testname.

UnitTest Java-like testng are used for the respective language unit tests. It's just a different way of achieving it. Depending on how the test implements the class inheritance, Setup is used to prepare the test case before execution, including the database connection establishment, the use case needs the preparation of the data and so on. TestName method Set the use case specific implementation of some of the operations, the page user name, password input, click on the page "login" button, such as a series of manual testing operations. The Teardown method is used for end-of-use cases such as garbage collection after the completion of a use case, disconnection of a database connection, etc.

the execution order of the setup,tearDown,testname Three methods in the test implementation class is not executed in the order in which they appear, regardless of the order in which the three methods appear, priority is given to setup, In the execution of testname, testName1 ... Finally execute the teardown.

The above is the introduction of the test case implementation ideas, test case code implementation of the storage module implementation of the idea is that Only test case methods (crmresourcestest) and test case data Files (Login.json) and catalogs (used to indicate the different levels of test cases) are allowed under this module. The distinction between the test case method and the test case data file here is the "important implementation of data and script separation" in the automation implementation idea. Separation of data from scripts facilitates automation of use case maintenance (you can maintain the use case by maintaining the JSON file without modifying the test script). As automation becomes more and more, you will find it important to separate the data from the script.


The public logic code encapsulates the storage module:

The testname method in the above code only uses the Self.dr Iverelement.webdriverlogin method, that is, the implementation of system login. The actual manual test operation, the completion of the login page requires "Enter user name", "Enter Password", "click" Login "button" three steps to achieve system login operation. Here only the "one step" implementation of the system login, how to see the public logic code encapsulated storage module under the driverelement.py file, you can see the webdriverlogin method is as follows:

def Webdriverlogin (self,driver):
        Elementaction=elementactionutil ()
        elementaction.textinput (Driver, Elementname= ' Js_uname ', method= ' Class ', text=self.f.getglobalfileattrs (' Login.json ', ' username '), timeout=2) #用户名输入
        elementaction.textinput (Driver, elementname= ' js_upwd ', method= ' Class ', text= ' 123456 ') #密码输入
        Elementaction.singleclick (Driver, Elementname= ' btnsend ', method= ' Id ') #点击 login button
        print ' System login successfully! '

As can be seen in the above code, see "One Step", which is actually a three-step package, here for the above three steps to encapsulate, because this three steps are used in many use cases, after encapsulation, each test case directly invoke the "one-step" method to achieve system login, no need to write three steps in each use case, The code is beautiful and easy to read and easy to implement. This is the meaning of the "Public Logic Code Encapsulation module", which is stored in the common steps that are reused in the code. Of course, for the common steps of the package implementation of the test data, there are public data files stored, such as: Baseaction under the Login.json file, stored in the public logic code to use the data.


GUI page action Code storage module:

specifically see the module under the elementactionutil.py file singleclick method:

def singleclick (self,webdriver,elementname= ", method= ' Id ', timeout=2):
' Method Value:id,name,class,xpath;webdriver value Firefox,ie '
Element1=waitforelementpresentaction.waitforelementpresentaction (Webdriver, ElementName, method, timeout)
Element1.click ()

The public logic code encapsulates the Elementaction.singleclick step in the module, its implementation is the above code, the actual manual test mouse click operation, in the code implementation is not a low-level method can be achieved, often implemented may require n many operations. For WHO to execute, what to do, and so on. Especially when it comes to poor network conditions, manual testing knows how long it will take to perform the click operation, but the automation code has to find ways to complete the manual operation of Intelligent, its realization can be imagined.

Here "GUI page action code storage Module", only need to include elementactionutil.py Single file, the file definition of various page actions: Mouse click action, double mouse action, text input action, radio button selection action, check button selection action, Whether the page element is visible, whether the element control exists, and so on. A collection of various page actions that implements the manual implementation required for page automation.


The underlying code module:

the underlying code module defines the common logic The code encapsulates the core code used to store the various action steps in the module, including the execution time of the action, how to find the element control (similar to human eye recognition), what control to perform the recognition for, and so on, for example code such as:

def waitforelementpresentaction (webdriver,elementname= ", method= ' Id ', timeout=2):
' Method Value:id,name,class,xpath;webdriver value Firefox,ie,takes a Webdriver instance and timeout in seconds '
Element1=none
End_time = time.time () + timeout
While True:
Try
If method== ' Id ':
ELEMENT1=WEBDRIVER.FIND_ELEMENT_BY_ID (ElementName)
Elif method== ' Name ':
Element1=webdriver.find_element_by_name (ElementName)
Elif method== ' Class ':
Element1=webdriver.find_element_by_class_name (ElementName)
Elif method== ' Xpath ':
Element1=webdriver.find_element_by_xpath (ElementName)
If element1:
Return element1
Except Exception:
Pass
If Time.time () >end_time:
Assert False, ' nosuchelementexception ' +elementname+ ' with the method ' +method# here why so, please a reader friend to reply.


File Operation code Storage module:

the module holds how to implement the test case data file content read operation of the various methods, the method is implemented, but also defines the test data file storage format, data file different suffix form of reading and so on, are stored in the "File Operation code storage module."


Database Code Storage module:

The module defines various operation implementations for the database: Add, delete, change, check, build table, call stored procedure and so on database operation. The meaning of this module is that, in the process of manual testing, the implementation of automation code for the correctness verification operation of the background database into the table after the foreground page operation is completed, as well as the pre-test data prefabrication implementation before testing case execution, etc.


Log file Code storage module:

The log file code storage module defines how log file generation is implemented, runs multiple use cases for test case level implementations, runs tests on suite-level use cases, analyzes use case runs, and runs output logs through use cases to check for correctness of use case runs. The Code Editor is not implemented here, and subsequent time is allowed to be completed.


Insufficient frame and to be optimized:

1. How a single test method implements multiple test cases, that is, how to define a data file hierarchy, and so on.

2. How the log file defines the implementation.

3. Single use case test execution, multi-use case test Execution (suite test execution) test report output, etc.

4. Code-like implementation of the Fool.

。。。。。。。。

Unfinished, finished, and continued ..... Maybe

This article is from the "River a Drop" blog, please be sure to keep this source http://yidishui.blog.51cto.com/6297932/1783234

Python+selenium+unittest's GUI automation framework implementation

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.