Python + Selenium notes (3): Use unittest, seleniumunittest
# An error may occur when the network is slow.
(1) Preface
Selenium WebDriver is an API set for automated browser testing. It provides many features for automated interaction with browsers, and these APIs are mainly used to test Web programs. If Selenium WebDriver is used only, we cannot implement the pre-conditions and post-Conditions for executing the test, compare the expected results with the actual results, check the program status, and generate the test report, create data-driven testing and other functions.
(2) unittest unit testing framework
Unittest (PyUnit) is inspired by JUnit, which is widely used in Java program development. Unittest enables us to create test cases, test kits, and test fixtures.
(3) xUnite testing framework
Arrange: a prerequisite used to initialize the test, including initializing the objects, configurations, and dependencies to be tested.
Act: used to perform function operations.
Assert: used to verify whether the actual results are consistent with the expected results.
(4) TestCase class
Create a single test or a group of tests by inheriting the TestCase class and adding a test method for each test in the test class.
(5) Use unittest
(1) define a subclass that inherits from the TestCase class.
(2) define the setUp () method in the class.
(3) Compile the test method
(4) code cleanup (define the tearDown () method in the class)
(5) run the test
Note:
1,A test case is from setUp ()You can use this method to execute initialization tasks before each test starts. The initialization preparation can be as follows: for example, creating a browser instance and accessing a URLLoad Test Data and open log files. This method has no parameters and does not return any values. When a setUp ()Method: The testing executor first executes the method before each test method is executed.
2,With setUp ()You can write some tests to verify the functions of the program we want to test. The test method is named as test.This Naming Convention notifies test runnerWhich method represents the test method.
3,Similar to setUp ()Method is called before each test method, TestCaseClass will also call tearDown () after the test execution is complete ()To clear all the initialization values. Once the test is executed ()The value defined in the method is no longer needed, so the best practice is to clear the setUp ()The value of method initialization.
(6) create a directory data, create a file category.txt, and enter the following data
Programming Language, Java, C ++, PHP, Delphi, Python, Ruby, C language, Erlang, Go, Swift, Scala, R language, OpenGL, other languages
(7) test whether the classification information in the programming language of the blog homepage is consistent with the expected results.
ImportUnittest
FromSeleniumImportWebdriver
FromSelenium. webdriver. common. action_chainsImportActionChains
ImportTime
ClassSearchTest (unittest. TestCase ):
DefSetUp (self ):
Self. driver = webdriver. Firefox ()
Self. driver. implicitly_wait (10)
Self. driver. maximize_window ()
Self. driver. get ("Https://www.cnblogs.com /")
DefTest_search_by_category (self ):
#Reads the category.txt file and returns a dictionary.
WithOpen ('Data/category.txt', Encoding ='Utf-8')AsCategory_file:
Category_dict = dict ()
Category_data = category_file.readline (). strip (). split (',')
The_class = category_data.pop (0)
Category_dict [the_class] = category_data
#Locate the programming language in the home page website category
Self. seach_class = self. driver. find_element_by_xpath ('// Li/a [@ href = "/cate/2/"]')
#Hover the cursor over the "Programming Language"
ActionChains (self. driver). move_to_element (self. seach_class). perform ()
#Returns all small classes in the programming language in the form of a list
Seach_small = self. driver. find_elements_by_xpath ( '// Div [@ id = "cate_content_block_2"]/div [@ class = "cate_content_block"]/ul/li')
#Sleep for 3 seconds
Time. sleep (3)
Small_cate = []
ForSInSeach_small:
#Remove the last three characters (0) of the sub-class and add them to the small_cate list.
Small_cate.append (str (s. text) [:-3])
#Check whether the expression is true (check whether the small classes in the programming language are consistent with the expected results)
Self. assertTrue (small_cate = category_dict ["Programming Language"])
# Self. assertEqual (small_cate, category_dict ["Programming Language "])
DefTearDown (self ):
Self. driver. quit ()
#The following two sentences can be added to run the test through the command line. If this parameter is not added, running the test through IDE is not affected.
If_ Name _ ='_ Main __':
#Add the verbosity = 2 parameter to display the specific test method in the command line.
Unittest. main (verbosity = 2)
(8) The test results are as follows:
Run the following command: