Python + Selenium notes (4): unittest Test Suite and seleniumunittest
(1)Test SuiteTest Suite
A test suite is a set of multiple tests or test cases. It is a set of tests created for the functions and modules of the tested program, the test cases in a test suite are executed together.
Using the TestSuites feature of unittest, you can combine different tests into a logical group, set up a unified test suite, and run the test using a command. This is implemented through the TestSuites, TestLoader, and TestRunn classes.
(2)Class-level setUp () and tearDown () Methods
Use the setUpClass () method, tearDownClass () method, and @ classmethod ID to share initialization data for each test method. For more information, see the following code.
(3)Searchtest. py
ImportUnittest
FromSeleniumImportWebdriver
FromSelenium. webdriver. common. action_chainsImportActionChainsImportTime
ClassSearchTest (unittest. TestCase ):
'''Use setUpClass () and @ classmethod to implement
All test methods share the class-Level Initialization data.
If this is not used, an instance ''' is created for each test method '''
@ Classmethod
DefSetUpClass (cls ):
Cls. driver = webdriver. Firefox ()
Cls. driver. implicitly_wait (10)
Cls. driver. maximize_window ()
Cls. 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. search_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. search_class). perform ()
#Returns all small classes in the programming language in the form of a list
Self. search_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 = []
ForSInSelf. search_small:
#Remove the last (0) of the sub-class and add it to the small_cate list.
Small = str (s. text). split ('(')
Small_cate.append (small [0])
#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 "])
DefTest_search_by_look (self ):
Self. seach_class = self. driver. find_element_by_xpath ('// Li/a [@ href = "/cate/2/"]')
#Locate small Python classes in programming languages
Self. seach_small = self. driver. find_element_by_xpath ('// Li/a [@ href = "/cate/python/"]')
ActionChains (self. driver). move_to_element (self. seach_class). perform ()
Self. seach_small.click ()
#Check whether the title of the opened webpage is Python-website classification-blog Garden
# Assert "Python-website classification-blog Park" in self. driver. title
Self. assertEqual (self. driver. title,"Python-Website category-blog Park")
'''Use the tearDownClass () and @ classmethod identifiers to implement
All test methods share the class-Level Initialization data '''
@ Classmethod
DefTearDownClass (cls ):
Cls. 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)
(4)Homepagetest. py
ImportUnittest
FromSeleniumImportWebdriver
FromSelenium. common. exceptionsImportNoSuchElementException
FromSelenium. webdriver. common.ImportBy
ClassHomePageTest (unittest. TestCase ):
'''Use setUpClass () and @ classmethod to implement
All test methods share the class-Level Initialization data.
If this is not used, an instance ''' is created for each test method '''
@ Classmethod
DefSetUpClass (cls ):
Cls. driver = webdriver. Firefox ()
Cls. driver. implicitly_wait (10)
Cls. driver. maximize_window ()
Cls. driver. get ("Https://www.cnblogs.com /")
DefTest_search_field (self ):
#Use by to check whether there is a search box on the blog homepage. is_element_present () is a custom method.
Self. assertTrue (self. is_element_present (By. ID,"Zzk_q"))
DefTest_search_btn (self ):
#Check whether the "Search" button is displayed on the homepage of the blog garden.
Self. assertTrue (self. is_element_present (By. CLASS_NAME,"Search_btn"))
DefTest_menu (self ):
#This method is used to check whether the menu bar information on the homepage is consistent with the expectation.
Reading menu.txt file data
WithOpen ('Data/menu.txt', Encoding ='Utf-8')AsMenu_file:
Menu_data = menu_file.readline (). strip (). split (',')
#Return the menu bar information of the blog homepage in the form of a list
Self. check_menu = self. driver. find_elements_by_xpath ('// Div [@ id = "nav_menu"]/')
The_menu = []
ForCInSelf. check_menu:
#Add the menu name and URL of the blog homepage to the the_menu list.
The_menu.append (c. text + c. get_attribute ('Href'))
#Check whether the two lists are consistent (check whether the menu name and URL of the blog homepage are consistent with expected)
Self. assertListEqual (the_menu, menu_data)
DefIs_element_present (self, how, what ):
#If the element is found, True is returned. Otherwise, False is returned.
Try:
Self. driver. find_element (by = how, value = what)
ExceptNoSuchElementExceptionAsE:
Return False
Return True
@ Classmethod
DefTearDownClass (cls ):
Cls. 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)
(5)Smoketests. py(Test suite)
ImportUnittest
FromSearchtestImportSearchTest
FromHomepagetestImportHomePageTest
#Obtain all the test methods in the SearchTest class and HomePageTest class.
Search_test = unittest. TestLoader (). loadTestsFromTestCase (SearchTest)
Home_page_test = unittest. TestLoader (). loadTestsFromTestCase (HomePageTest)
#Create a test suite that includes SearchTest and HomePageTest
Smoke_tests = unittest. TestSuite ([home_page_test, search_test])
#Run test suite
Unittest. TextTestRunner (verbosity = 2). run (smoke_tests)
(6)Other Instructions
#Another file has been described in the previous article
To use the complete code, create a file named menu.txt and enter the following data:
Garden/
(7)Run smoketests. py