Today, we will explain how to use unittest framework to realize UI automation in python. We won't repeat how to use webdriver API to operate web pages today. For missed partners, please stamp the link below~~
1.unittest unit test framework
-
Why use a unit test framework?
The use of unit test framework can standardize the preparation of test cases, facilitate the management and maintenance of test cases, and provide data for test reports.
2. Concepts in unittest unit test framework
-
test case
A test case is an independent test unit. It checks the response when specific data is entered. unittest provides a base class: TestCase, which is used to create new test cases.
-
test suite
test suite is a series of test cases, or test suites, or both. It is used to archive tests that need to be performed together.
-
test runner
test runner is a component used to execute and output test results. The runner may use a graphical interface, a text interface, or return a specific value to represent the result of running the test.
3. Preliminary preparation
Download webdriver and select the corresponding browser driver to download (this demonstration is based on Google driver)
-
chromedriver.exe: https://npm.taobao.org/mirrors/chromedriver/
-
Download the full history version of Firefox: http://ftp.mozilla.org/pub/firefox/releases/
-
geckodriver.exe:
https://github.com/mozilla/geckodriver/releases
-
Full drive download link:
https://www.selenium.dev/downloads/
After downloading the driver, put the driver package in Python Exe under the same level directory
4. Example
Analysis of operation results:
#Import Webdriver,UI Automation mainly depends on Webdriver realization from selenium import webdriver#Import time module import time#import unittest unit test module #Define the test case set of Baidu web login class BaiduLogin(unittest.TestCase): #definition Setup,Used to initialize the test firmware. The meaning of this setting is to open the browser and set before executing each test case url by https://www.baidu.com def setUp(self): self.url = 'https://www.baidu.com/' self.browser = webdriver.Chrome() # defines teardown, which is used to destroy test firmware. The meaning of this setting is to close the browser after executing each test case # so the execution order of the current script is: ##setup -- > test_ 001_ query-->teardown-->setup-->test_ 002_ login-->teardown def tearDown(self): self.browser.quit() def test_001_query(self): #Create a file named test_query_001 Test cases, open Baidu home page in the browser self.browser.get(self.url) #Maximize window self.browser.maximize_window() #Enter the keyword "123" in the search box self.browser.find_element_by_id('kw').send_keys('123') #Click the "Baidu click" button self.browser.find_element_by_id('su').click() #print("query was successful") #Wait for 3S to view the returned result of the page time sleep(3) def test_002_login(self): #Create a file named test_login_002 Open Baidu home page in the browser self.browser.get(self.url) #Maximize window self.browser.maximize_window() #Click the "login" button self.browser.find_element_by_id('s-top-loginbtn').click() #Wait 2 S time.sleep(2) #Switch login mode to "user name login" self.browser.find_element_by_id("TANGRAM__PSP_11__footerULoginBtn").click() #Clear the previously entered content in the user name input box self.browser.find_element_by_name("userName").clear() #Enter the user name "12345678910" self.browser.find_element_by_name("userName").send_keys("12345678910") #Enter the password "123456" self.browser.find_element_by_name("password").send_keys("123456") #Click the login button self.browser.find_element_by_id("TANGRAM__PSP_11__submit").click() #Manually drag the verification code. If the time is not enough, you can increase the waiting time time.sleep(10) #print("login completed") if __name__ == '__main__': unittest.main() There are two in the results“·",It means that both use cases are executed successfully; If the returned result contains“ E",It indicates that there is an error in the corresponding use case, and it is necessary to locate whether there is a problem in the script If the result returned is“ F",Indicates that the execution of the corresponding use case failed; If the result returned is“ S",Indicates that the use case skip is not executed.
If my blog is helpful to you and you like my blog content, please click "like", "comment" and "collect" for three times!
Finally, basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, advanced test Python programming, Web automation test, APP automation test, interface automation test, advanced continuous integration of test, test architecture development test framework, performance test, security test and other supporting learning resources [free].