# -*- coding: UTF-8 -*- from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys from common.driver_common.mywebdriver import MyWebDriver from selenium.webdriver.common.by import By from config.workpath import workdir from page_element.admin_area_element_position import * from common.driver_common.screenshot import screenshot_on_failure from common.driver_common.random_name import RandomName from common.ui_common.admin_area.manage_users.manage_users_public_operations import ManageUsersPublicOperations import pytest_check import inspect import random from selenium.webdriver.common.keys import Keys class Pages: def __init__(self, demo_fixture: MyWebDriver): self.workdir = workdir self.my_random = RandomName() self.driver = demo_fixture # 调用profiles公共方法操作、删除、查询, self.pages_po = ManageUsersPublicOperations(self.driver) @screenshot_on_failure def pages_case(self, data: {}): # 打开对应菜单列表页 self._goto_subLoginProfiles() # 跳转页面 self._turn_page(data) # 执行翻页操作 @screenshot_on_failure def _goto_subLoginProfiles(self): # 菜单操作,登录成功后定位到Admin Area-Manage Users-Login Profiles列表页 self.driver.find_element(By.XPATH, mainPage_button_menu_posXpath).click() self.driver.find_element(By.CSS_SELECTOR, mainPage_button_adminArea_posCss).click() if "level1 level1-submenu el-submenu is-active is-opened clear-active" != self.driver.find_element(By.ID, mainPage_firstLevelMenu_manageUsersStyle_posId).get_attribute( "class"): self.driver.find_element(By.XPATH, mainPage_firstLevelMenu_manageUsers_posXpath).click() self.driver.find_element(By.ID, mainPage_secondLevelMenu_loginProfiles_posId).click() @screenshot_on_failure @screenshot_on_failure def _turn_page(self): """ 除name的其它选项操作,共创建、修改逻辑调用 :param data: :return: """ total_pages = self.pages_po._get_total_pages(self) # 如果只有1页,则往前和往后翻页均不可用 if total_pages == 1: is_display = self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format(replaceIndex=1)).is_displayed() if is_display == False: raise ValueError( f" The first page does not exist! is_display:{is_display}") else: first_row_style = self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format(replaceIndex=1)).get_attribute("class") prev_page_disabled = self.driver.find_element(By.XPATH, common_page_button_prev_posXpath).get_attribute("disabled") next_page_disabled = self.driver.find_element(By.XPATH,common_page_button_next_posXpath).get_attribute("disabled") if "number active" == first_row_style and None == prev_page_disabled and None == next_page_disabled: pass else: raise ValueError( f" When there is only one page, page turning and page status are incorrect.first_row_style:{first_row_style},prev_page_disabled:{prev_page_disabled},next_page_disabled:{next_page_disabled}") # 如果有多页,持续翻页,考虑过多页时,只随机翻几页 else: is_display = self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format( replaceIndex=1)).is_displayed() if is_display == False: raise ValueError( f" The first page does not exist! is_display:{is_display}") else: first_row_style = self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format(replaceIndex=1)).get_attribute("class") prev_page_disabled = self.driver.find_element(By.XPATH, common_page_button_prev_posXpath).get_attribute("disabled") next_page_disabled = self.driver.find_element(By.XPATH,common_page_button_next_posXpath).get_attribute("disabled") if "number active" == first_row_style and "true" == prev_page_disabled and None == next_page_disabled: pass else: raise ValueError( f" When there is only one page, page turning and page status are incorrect.firt_row_style:{first_row_style}") # 点击下一页 self.driver.find_element(By.XPATH, common_page_button_next_posXpath).click() first_row_style = self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format( replaceIndex=1)).get_attribute("class") second_row_style = self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format( replaceIndex=2)).get_attribute("class") if "number" == first_row_style and "number active" == second_row_style: pass else: raise ValueError( f" The page status is incorrect after turning the page for the first time.firt_row_style:{first_row_style},second_row_style:{second_row_style}") # 点击上一页 self.driver.find_element(By.XPATH, common_page_button_prev_posXpath).click() first_row_style = self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format( replaceIndex=1)).get_attribute("class") second_row_style = self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format( replaceIndex=2)).get_attribute("class") if "number active" == first_row_style and "number" == second_row_style: pass else: raise ValueError( f" The page status is incorrect after turning the page for the second time.firt_row_style:{first_row_style},second_row_style:{second_row_style}") # 循环点击下一页 for i in range(2,total_pages+1,1): self.driver.find_element(By.XPATH, common_page_button_next_posXpath).click() current_row_style = self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format( replaceIndex=i)).get_attribute("class") if "number active" == current_row_style: pass else: raise ValueError( f" The current page status is incorrect after turning the page.current_row:{i},current_row_style:{current_row_style}") # 循环点击上一页 for i in range(total_pages-1,0,-1): self.driver.find_element(By.XPATH, common_page_button_prev_posXpath).click() current_row_style = self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format( replaceIndex=i)).get_attribute( "class") if "number active" == current_row_style: pass else: raise ValueError( f" The current page status is incorrect after turning the page.current_row:{i},current_row_style:{current_row_style}") # 循环点击页码 for i in range(1, total_pages, 1): self.driver.find_element(By.XPATH, common_page_button_target_page_num_posXpath.format(replaceIndex=i)).click() current_row_style = self.driver.find_element(By.XPATH,common_page_button_target_page_num_posXpath.format(replaceIndex=i)).get_attribute("class") if "number active" == current_row_style: pass else: raise ValueError( f" The current page status is incorrect after turning the page.current_row:{i},current_row_style:{current_row_style}") # goto输入随机页码: random_page_num = random.randint(0,total_pages+100) # 清理输入框 self.driver.find_element(By.XPATH, common_page_goto_input_posXpath).clear() # 输入目标页面值 self.driver.find_element(By.XPATH, common_page_goto_input_posXpath).send_keys(random_page_num) # 输入回车翻页 self.driver.find_element(By.XPATH, common_page_goto_input_posXpath).send_keys(Keys.ENTER) current_row = common_page_button_target_page_num_posXpath.format(replaceIndex=1) if random_page_num == 0: current_row = common_page_button_target_page_num_posXpath.format(replaceIndex=1) elif random_page_num > total_pages: current_row = common_page_button_target_page_num_posXpath.format(replaceIndex=total_pages) else: current_row = common_page_button_target_page_num_posXpath.format(replaceIndex=random_page_num) current_row_style = self.driver.find_element(By.XPATH, current_row).get_attribute("class") if "number active" == current_row_style: pass else: raise ValueError( f" The current page status is incorrect after turning the page.current_row:{current_row},current_row_style:{current_row_style}") @screenshot_on_failure def _query(self, data: {}, require_assertion=0, **kwargs): """ 查询函数,根据输入的类型查询 :param require_assertion: =0默认情况不需要断言,=1为创建后使用的断言预期,=2为编辑修改后使用的断言预期 :param kwargs: 例如 :可以查询使用的关键字,ID=None,Name=None,Type=None("A") :return: """ # 打开该列表 self._goto_subLoginProfiles # first_row查询到的第一行内容。map_header_className为需要使用的到字典,用来提取第一行的结果。 first_row, map_header_className = \ self.login_profiles_po.query(mainPage_common_search_selectLabel_posId, listPage_common_dropDown_item_posXpath, listPage_common_input_itemContent_posXpath, mainPage_common_search_buttonSearch_posId, listPage_common_tableTbody_posXpath, listPage_common_tableHeader_posXpath, listPage_profileSearch_subProfiles_dropDown_typeItem_posXpath="", **kwargs) """ #点击search查询框 self.driver.find_element(By.ID, listPage_profileSearch_dnsRecords_selectLabel_posId).click() #循环输入查询内容 for key, value in kwargs.items(): dropdown_item_posXpath = listPage_profileSearch_dnsRecords_dropDown_item_posXpath.format(replaceName=key) self.driver.find_element(By.XPATH, dropdown_item_posXpath).click() #点击下拉item if key != "type": input_item_posXpath = listPage_profileSearch_dnsRecords_input_itemContent_posXpath.format(replaceName=key) self.driver.find_element(By.XPATH, input_item_posXpath).send_keys(value.split("->")[0]) #输入查询值 else: dropdown_typeItem_posXpath = listPage_profileSearch_dnsRecords_dropDown_typeItem_posXpath.format(replaceName=value.split("->")[0]) self.driver.find_element(By.XPATH, dropdown_typeItem_posXpath).click() #点击type的下拉item #点击查询按钮 element_search = self.driver.find_element(By.ID, listPage_profileSearch_dnsRecords_buttonSearch_posId) self.driver.execute_script("arguments[0].click()", element_search) #强制点击 element_tbody = self.driver.find_element(By.XPATH, listPage_profilTable_dnsRecords_tableTbody_posXpath) #找到所有的行,table的body element_header = self.driver.find_element(By.XPATH, listPage_profilTable_dnsRecords_tableHeader_posXpath) #查询table的header #获取table header的text、th.class headers = element_header.find_elements(By.XPATH, "th") #table header rows = element_tbody.find_elements(By.XPATH, "tr") #table body map_header_className = {} #表头名称和th.class的映射,为了从表body中去对应列的值{"ID":"33"} for th in headers: header_content = th.text.strip() classNames = th.get_attribute("class").split() for c_name in classNames: if "el-table" in c_name: tableBodyRowClassName = c_name if header_content and tableBodyRowClassName: map_header_className[header_content] = tableBodyRowClassName #从第一行查询的结果中,取出需要断言的值、name、Type、Values first_row = rows[0] """ # 第一行的勾选定位,给修改、删除使用 self.first_row_checkBox_element = first_row.find_element(By.XPATH, "//span[@class='el-checkbox__input']") # 第一行id self.first_row_ID_content = first_row.find_element(By.XPATH, "//div[@role='group']/label/span/input").get_attribute( "value") first_row_id_content = self.first_row_ID_content # 第一行Name,样式,根据样式获取Name内容//div[@class='bfId']/p/span first_row_Name_class = map_header_className["Name"] first_row_Name_content = first_row.find_element(By.XPATH, f"//td[contains(@class, '{first_row_Name_class}')]//div[2]//span").text.strip() first_row_Max_Login_Attempts_class = map_header_className["Max Login Attempts"] first_row_Max_Login_Attempts_content = first_row.find_element(By.XPATH, f"//td[contains(@class, '{first_row_Max_Login_Attempts_class}')]//span").text.strip() first_row_Lockout_Time_class = map_header_className["Lockout Time"] first_row_Lockout_Time_content = first_row.find_element(By.XPATH, f"//td[contains(@class, '{first_row_Lockout_Time_class}')]//span").text.strip() first_row_Only_Allow_Authorized_IPs_to_login_class = map_header_className["Only Allow Authorized IPs to login"] first_row_Only_Allow_Authorized_IPs_to_login_contents = first_row.find_elements(By.XPATH, f"//td[contains(@class, '{first_row_Only_Allow_Authorized_IPs_to_login_class}')]/div/div/div/div/div") first_row_Login_Allowed_Time_class = map_header_className["Login Allowed Time"] first_row_Login_Allowed_Time_contents = first_row.find_elements(By.XPATH, f"//td[contains(@class, '{first_row_Login_Allowed_Time_class}')]/div/div/div/div") first_row_Password_Strength_class = map_header_className["Password Strength"] first_row_Password_Strength_content = first_row.find_element(By.XPATH, f"//td[contains(@class, '{first_row_Password_Strength_class}')]//span").text.strip() first_row_Password_Change_Period_class = map_header_className["Password Change Period"] first_row_Password_Change_Period_content = first_row.find_element(By.XPATH, f"//td[contains(@class, '{first_row_Password_Change_Period_class}')]//span").text.strip() first_row_Enforce_2FA_class = map_header_className["Enforce 2FA"] first_row_Enforce_2FA_content = first_row.find_element(By.XPATH, f"//td[contains(@class, '{first_row_Enforce_2FA_class}')]//span").text.strip() authorized_IPs_list = [] for value in first_row_Only_Allow_Authorized_IPs_to_login_contents: authorized_IPs_list.append(value.text.strip()) print(first_row_id_content, first_row_Name_content, first_row_Max_Login_Attempts_content, first_row_Lockout_Time_content, authorized_IPs_list) """ pwd_strength_temp = "Fair" is_two_factor_temp = "Disable" current_language = "en" strength = "1" two_factor = "0" # raido_two if current_language == "en": pwd_strength_temp = "" # tup_pwd_strength_en[int(strength) - 1] is_two_factor_temp = "" # tup_disable_enable_en[int(two_factor)] elif current_language == "py": pwd_strength_temp = "" # tup_pwd_strength_py[int(strength) + 1] is_two_factor_temp = "" # tup_disable_enable_py[int(two_factor)] elif current_language == "zh": pwd_strength_temp = "" # tup_pwd_strength_zh[int(strength) + 1] is_two_factor_temp = "" # tup_disable_enable_zh[int(two_factor)] """ if require_assertion == 1: data_index = 0 # 新增数据索引 elif require_assertion == 2: data_index = -1 # 编辑数据索引 else: # 不适用断言 pass no_modify = "不修改" # 不同操作后的断言,创建后、修改后、无断言 print("self.random_name:"+self.random_name+";first_row_Name_content:"+first_row_Name_content) pytest_check.equal(self.random_name, first_row_Name_content, msg="所在行数{}".format(inspect.currentframe().f_lineno)) data_value: str = self._get_value_from_data(data, key="", value=data["login_addr_restriction"]["max_login_attmepts"], data_index=data_index) # 提取数据 pytest_check.equal(first_row_Max_Login_Attempts_content, data_value, msg="所在行数{}".format(inspect.currentframe().f_lineno)) data_value: str = self._get_value_from_data(data, key="", value=data["login_addr_restriction"]["lockout_minutes"], data_index=data_index) # 提取数据 pytest_check.equal(str(first_row_Lockout_Time_content.split(" ")[0]), data_value, msg="所在行数{}".format(inspect.currentframe().f_lineno)) for i in data["login_addr_restriction"]["authorized_ips"]: data_value: str = self._get_value_from_data(data, key="", value=i, data_index=data_index) # 提取数据 if data_value == "不修改": data_value: str = self._get_value_from_data(data, key="", value=i, data_index=0) # 提取数据 if data_value == "": continue if data_value == "删除": data_value: str = self._get_value_from_data(data, key="", value=i, data_index=0) # 提取数据 pytest_check.is_not_in(data_value, str(authorized_IPs_list), msg="所在行数{}".format(inspect.currentframe().f_lineno)) else: print("data_value:"+data_value+";authorized_IPs_list:"+str(authorized_IPs_list)) pytest_check.is_in(data_value, str(authorized_IPs_list), msg="所在行数{}".format(inspect.currentframe().f_lineno)) data_value: str = self._get_value_from_data(data, key="", value=data["authen_setting"]["pwd_strength"], data_index=data_index) # 提取数据 pytest_check.equal(first_row_Password_Strength_content, data_value, msg="所在行数{}".format(inspect.currentframe().f_lineno)) data_value: str = self._get_value_from_data(data, key="", value=data["authen_setting"]["pwd_change_period"], data_index=data_index) # 提取数据 pytest_check.equal(first_row_Password_Change_Period_content.split(" ")[0], data_value, msg="所在行数{}".format(inspect.currentframe().f_lineno)) data_value: str = self._get_value_from_data(data, key="", value=data["authen_setting"]["is_two_factor"], data_index=data_index) # 提取数据 pytest_check.equal(first_row_Enforce_2FA_content, data_value, msg="所在行数{}".format(inspect.currentframe().f_lineno)) # 强制点击清空查询按钮 element_clear = self.driver.find_element(By.ID, mainPage_common_search_buttonClear_posId) self.driver.execute_script("arguments[0].click()", element_clear) # 强制点击 return self.first_row_ID_content #create = _create query = _query