Selenium的PageObject设计模式(2)

 

 

一、代码与数据分离的方式(一)

1.excel做数据源时表格设计

 

 

2.读取出excel文件内元素组装成数组形式返回

 1 conf = ConfigUtils()
 2 elements_path=conf.get_elements_url
 3 
 4 class ElementExcelData:
 5     def __init__(self, sheet_name, element_path=elements_path):
 6         self.element_path = element_path
 7         # 打开excel文件
 8         self.workbook = xlrd.open_workbook(self.element_path)
 9         # sheet页名
10         self.sheet = self.workbook.sheet_by_name(sheet_name)
11         # 获取行数
12         self.row_count = self.sheet.nrows
13 
14     def get_element_info(self):
15         elements_info={}
16         for i in range(1, self.row_count):
17             element_info = {}
18             element_info['element_name'] = self.sheet.cell_value(i, 1)
19             element_info['locator_type'] = self.sheet.cell_value(i, 3)
20             element_info['locator_value'] = self.sheet.cell_value(i, 4)
21             element_info['timeout'] = int(self.sheet.cell_value(i, 5))
22             elements_info[self.sheet.cell_value(i, 0)] = element_info
23         return elements_info
24 
25 
26 def get_page_info(sheet_name):
27     element_data = ElementExcelData(sheet_name)
28     elements = element_data.get_element_info()
29     return elements

3.读取excel为数据源

 1 # 读取excel为数据源
 2 elements = get_page_info('Login')
 3 
 4 class LoginPage(BasePage):
 5     def __init__(self,driver):
 6         super().__init__(driver)
 7 
 8         # 方式三:
 9         self.username_inputbox = elements['username_inputbox']
10         self.password_inputbox = elements['password_inputbox']
11         self.login_button = elements['login_button']

 

 

二、代码与数据分离的方式(二)

1.yaml文件做数据源时.yml设计方式

 

 2.读取出yaml文件内元素

 1 conf = ConfigUtils()
 2 elements_yaml_url=conf.get_elements_yaml_url
 3 
 4 class ElementYamlData:
 5     def __init__(self, sheet_name, element_path=elements_yaml_url):
 6         self.sheet_name =sheet_name
 7         # 打开yaml文件
 8         self.file = open(element_path, 'r', encoding='utf-8')
 9         # 读出yaml文件全部内容
10         self.yaml_content = self.file.read()
11 
12     def read_yaml(self):
13         yaml_list = yaml.load(self.yaml_content,Loader=yaml.FullLoader)
14         elements = yaml_list[self.sheet_name]
15         return elements

 

3.读取yaml文件为数据源

 1 # 读取yaml文件为数据源
 2 element_infos = ElementYamlData('LoginPage')
 3 elements=element_infos.read_yaml()
 4 
 5 class LoginPage(BasePage):
 6     def __init__(self,driver):
 7         super().__init__(driver)
 8 
 9         # 方式三:
10         self.username_inputbox = elements['username_inputbox']
11         self.password_inputbox = elements['password_inputbox']
12         self.login_button = elements['login_button']

 

posted @ 2020-05-03 21:11  咿~Summer  阅读(301)  评论(0编辑  收藏  举报