Python + Selenium notes (18): continuous integration of jenkins and seleniumjenkins
(1)Install xmlrunner
This module is used in the test code when Jenkins is used for testing.
Pip install xmlrunner
(2)Install jenkins
(1) download jekins
Https://jenkins.io/download/
Download is suitable for you. I download the long-term stable version.
(2) decompress the package, run the jenkins. msi file, and continue the next step.
(3) log on to http: // localhost: 8080
(4) follow the prompts to enter the password before proceeding to the next step.
(6) The first one here should be the commonly used plug-ins installed by default for jenkins community, and the second one should be the plug-ins to be installed. If you are not familiar with it, select the first one.
(7) after the plug-in is successfully installed, as shown in. (If some plug-ins fail to be installed in this step, download the plug-in again. If the plug-in cannot be installed, restart the Jenkins Service (restart the Windows Service). Then, log on again and skip this step, after the agent fails to be installed, install it in Jenkins plug-in management)
(8) the installation is complete.
(3)Create a task
(1) Click create on the homepage ].
(2) fill in the relevant information and click OK ].
(3) set the automatic build time for the build trigger. The following is automatic execution at every night on workday, so that the test results can be seen at work the next morning.
There are five parameters: Can I click Next? View the instruction document. * indicates any time.
First: minute (0-59)
Second: hour (0-23)
Third: Day (1-31)
Fourth: Month (1-12)
Fifth: day of the week (0-7, 0 and 7 both represent Sunday)
(4) Add the build step to the build part. Here, select the Windows Batch Processing Command.
Copy F: \ Python \ firstselenium \ smoketest \ *. py
Python smoketests. py
(5) After the build, add the operation steps, select Publish JUnit test result report, and enter the test report/*. xml in the test report field. (The test report is the output parameter value configured in the test suite. See the following smoketests. in this way, Jenkins reads the test results from this folder every time it runs the test.
(6) After the configuration is complete, click Save ].
(7) Click "build now", and the blue color indicates success.
(8) Click the latest test results to view the latest test results. (You can also click the time under the specific Build History to view the test result of the specific Build, and click the blue spherical icon to view the console output)
The following code mainly refers to this sentence in the test suite. Other users can simply write one or several test cases.
Xmlrunner. XMLTestRunner (verbosity = 2, output = 'test Report'). run (smoke_tests)
(4)Smoketests. py
1 import unittest 2 from searchtest import SearchTest 3 from homepagetest import HomePageTest 4 from xmlrunner import xmlrunner 5 # obtain all the test methods in the SearchTest class and HomePageTest Class 6 search_test = unittest. testLoader (). loadTestsFromTestCase (SearchTest) 7 home_page_test = unittest. testLoader (). loadTestsFromTestCase (HomePageTest) 8 # create a test suite that includes SearchTest and HomePageTest 9 smoke_tests = unittest. testSuite ([home_page_test, search_test]) 10 # Run test suite 11 # unittest. textTestRunner (verbosity = 2 ). run (smoke_tests) 12 xmlrunner. XMLTestRunner (verbosity = 2, output = 'test Report '). run (smoke_tests)
(5)Searchtest. py
1 import sys 2 import unittest 3 from selenium import webdriver 4 from selenium. webdriver. common. action_chains import ActionChains 5 from selenium. webdriver. support. ui import WebDriverWait 6 class SearchTest (unittest. testCase): 7 @ classmethod 8 def setUpClass (cls): 9 cls. driver = webdriver. chrome () 10 # cls. driver. implicitly_wait (20) 11 cls. driver. maximize_window () 12 cls. driver. get ("https://www.cnblogs.com/") 13 14 def test_search_by_category (self): 15 category_list = ['java', 'c ++ ', 'php', 'delphi ', 'python', 'ruby', \ 16 'C language ', 'erlang', 'Go ', 'SWIFT', 'scala ', 'r language', 'til ', 'Other language '] 17 # locate the programming language 18 search_class = self in the home page website category. driver. find_element_by_xpath ('// li/a [@ href = "/cate/2/"]') 19 # hover the cursor over 20 ActionChains (self. driver ). move_to_element (search_class ). perform () 21 WebDriverWait (self. driver, 20 ). until (lambda l: len (l. find_elements_by_xpath (\ 22 '// div [@ id = "cate_content_block_2"]/div [@ class = "cate_content_block"]/ul/li') = 14) 23 # return all small classes in the programming language in the form of a list 24 search_small = self. driver. find_elements_by_xpath (\ 25' // div [@ id = "cate_content_block_2"]/div [@ class = "cate_content_block"]/ul/li ') 26 small_cate = [] 27 for s in search_small: 28 # remove the last (0) of the small class and add it to the small_cate list. 29 small = str (s. text ). split (') 30 small_cate.append (small [0]) 31 # Check whether the expression is true (here, check whether the small classes in the programming language are consistent with the expected results) 32 self. assertTrue (small_cate = category_list) 33 self. assertEqual (small_cate, category_list) 34 35 def test_search_by_look (self): 36 seach_class = self. driver. find_element_by_xpath ('// li/a [@ href = "/cate/2/"]') 37 # locate the small class Python38 seach_small = self in the programming language. driver. find_element_by_xpath ('// li/a [@ href = "/cate/python/"]') 39 ActionChains (self. driver ). move_to_element (seach_class ). click (seach_small ). perform () 40 # Check whether the webpage title is Python-website classification-blog 41 self. assertEqual (self. driver. title, "Python-website classification-blog Garden") 42 43 @ classmethod44 def tearDownClass (cls): 45 cls. driver. quit () 46 # Add the following two sentences to run the test through the command line, if no value is added, the Test 47 if _ name _ = '_ main _': 51 # Add the verbosity = 2 parameter, the command line shows the specific test method 52 unittest. main (verbosity = 2)
(6)Homepagetest. py
1 import sys 2 import unittest 3 from selenium import webdriver 4 from selenium. common. exceptions import NoSuchElementException 5 from selenium. webdriver. common. by import By 6 class HomePageTest (unittest. testCase): 7 @ classmethod 8 def setUpClass (cls): 9 cls. driver = webdriver. chrome () 10 cls. driver. implicitly_wait (10) 11 cls. driver. maximize_window () 12 cls. driver. get ("https://www.cnblogs.com/") 13 14 def test_search_field (self): 15 # check whether there is a search box on the blog homepage, is_element_present () is the custom Method 16 self. assertTrue (self. is_element_present (. ID, "zzk_q") 17 def test_search_btn (self): 18 # check if there is any search button on the blog homepage 19 self. assertTrue (self. is_element_present (. CLASS_NAME, "search_btn") 20 21 # Check whether the menu bar information on the blog homepage is as expected 22 def test_menu (self): 23 menu_data = ['garden https://home.cnblogs.com /', 'news https://news.cnblogs.com/', 24 'blog ask https://q.cnblogs.com/', 'Flash https://ing.cnblogs.com/', 'Group https://group.cnblogs.com/', 'favorites comment' recruitment https://job.cnblogs.com /', 'class https://edu.cnblogs.com/',27' for more information: http://zzk.cnblogs.com/'%28 # Return to the menu bar information of the blog garden homepage in the form of a list 29 self. check_menu = self. driver. find_elements_by_xpath ('// div [@ id = "nav_menu"]/A') 30 the_menu = [] 31 for c in self. check_menu: 32 # Add the menu name and URL of the blog homepage to the list the_menu33 the_menu.append (c. text + c. get_attribute ('href ') 34 # Check whether the two lists are consistent (check whether the menu name and URL of the blog homepage are consistent with expected) 35 self. assertListEqual (the_menu, menu_data) 36 37 # Find the element and return True; otherwise, return False38 def is_element_present (self, how, what): 39 try: 40 self. driver. find_element (by = how, value = what) 41 blocks t NoSuchElementException as e: 42 return False43 return True44 45 @ classmethod46 def tearDownClass (cls): 47 cls. driver. quit () 48 49 # Add the following two sentences to run the test through the command line, if this parameter is not added, the test 50 if _ name _ = '_ main _': 54 # Add the verbosity = 2 parameter, the command line shows the specific test method 55 unittest. main (verbosity = 2)