[python] take the blog Garden Network as an example to realize the separation of program and data by using ini configuration file in learning automation test

Posted by johnkperks on Mon, 01 Nov 2021 03:57:47 +0100

preface

Where should the page positioning elements used in UI automation test be stored? What I want to say is that if you use the PO design pattern to design test cases, you can store the positioning elements in each page or separately in a directory. For new keys, different pages correspond to files with different names to store the positioning method. When the page changes, you only need to modify the positioning expression file corresponding to the corresponding page to facilitate project maintenance. The other is stored in the configuration file, which can also be configured for those who do not know how to automate testing. Of course, you can choose according to your own wishes. Now let me see how to store it in the configuration file? How to read and locate?

directory structure

Test address: https://www.cnblogs.com/

Test steps: 1. Open the web address 2. Enter python in the search box 3. Click the search button 4. Judge whether python is included in the page source code

File Description: Element.ini stores location elements. GetElement.py is used to read the configuration file and find page elements. CnblogsTest.py is used to write test cases

Example code

Element.ini

1 [cnblogs]
2 queryBox_id=id:zzk_search_input
3 queryBtn_id=id:zzk_search_button

GetElement.py

 1 # -*- coding:GBK -*-
 2 # Import test module
 3 import os
 4 import configparser
 5 from selenium import webdriver
 6 from selenium.webdriver.support.ui import WebDriverWait
 7 
 8 
 9 # Create get object class
10 class GetElement():
11     """
12     Get location information from configuration file
13     """
14     def __init__(self):
15         self.elementIni = os.path.dirname(os.path.abspath(__file__)) + r'\Element.ini'  #Profile path
16 
17     def getelement(self, driver, query_section, query_option):
18         try:
19             config = configparser.ConfigParser()
20             # Read configuration file into memory
21             config.read(self.elementIni)
22             locators = config.get(query_section,query_option).split(':') 
23             query_box = locators[0]  
24             query_btn = locators[1]
25             element = WebDriverWait(driver, 5).until(lambda x : x.find_element(query_box, query_btn))
26         except Exception as e:
27             raise e
28         else:
29             return element
30 
31 
32 if __name__ == "__main__":
33     ele = GetElement()
34     print(ele.elementIni)
35     driver = webdriver.Firefox()
36     driver.get("https://www.cnblogs.com/")
37     element = ele.getelement(driver, "cnblogs", "queryBox_id")
38     element.send_keys("python")

CnblogsTest.py

 1 import time
 2 import unittest
 3 from selenium import webdriver
 4 from GetElement import GetElement
 5 
 6 
 7 class Cnblogs(unittest.TestCase):
 8 
 9     def setUp(self):
10         self.obj = GetElement()
11         self.driver = webdriver.Firefox()
12         self.driver.get("https://www.cnblogs.com/")
13 
14     def test_cnblogs(self):
15         element_query = self.obj.getelement(self.driver, "cnblogs", "queryBox_id")
16         element_query.send_keys("python")
17         element_btn = self.obj.getelement(self.driver, "cnblogs", "queryBtn_id")
18         element_btn.click()
19         time.sleep(5)
20 
21     def tearDown(self):
22         self.driver.quit()
23 
24 
25 if __name__ == "__main__":
26     unittest.main(verbosity=2)

This example realizes the separation of program and data. First, obtain the positioning method and positioning expression of the page element to be operated in the front page of the blog park from the UI object library file WebElement.ini, then obtain the instance object of the page element in GetElement.py, and finally return it to the test case method for subsequent processing, The advantage of this is that even those who do not know the test can configure the use case. When the positioning expression changes, only modify the positioning in the configuration file without modifying the test related code. Of course, this is relatively speaking. If the page function operation steps change, you have to modify the code operation steps.

reference resources: https://www.cnblogs.com/linuxchao/p/linuxchao-selenium-programDataSeparate.html

Topics: Python Selenium Testing unittest