# -*- coding: UTF-8 -*- import time 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 import re import math from selenium.webdriver.common.keys import Keys class Language: def __init__(self, demo_fixture: MyWebDriver): self.workdir = workdir self.my_random = RandomName() self.driver = demo_fixture self.language_style = "cursor-pointer iconfont icon-Lang_{replaceLanguage}Select" self.language_abbreviation = {"English": "en","简体中文": "zh","Русский": "py"} @screenshot_on_failure def language_case(self, data: {}): self._switch_language(data) # 执行翻页操作 @screenshot_on_failure def _switch_language(self, target_lauguage = "English"): """ 除name的其它选项操作,共创建、修改逻辑调用 :param data: :return: """ current_laguage = "en" # 等待语言栏出现 is_display = self.driver.find_element(By.XPATH, common_language_button_posXpath).is_displayed() if is_display == False: print("未加载出当前语言栏,请确认定位是否更改") return 0 time.sleep(0.5) if target_lauguage == "Current Laguage" or target_lauguage == "": current_laguage = self.driver.find_element(By.XPATH, common_language_button_posXpath).get_attribute("class") if 'cursor-pointer iconfont icon-Lang_enSelect' == current_laguage: current_laguage = "en" elif 'cursor-pointer iconfont icon-Lang_zhSelect' == current_laguage: current_laguage = "zh" #elif 'iconfont cursorPointer icon-Lang_pySelect' == current_laguage: else: current_laguage = "py" return current_laguage # 获取目标语言,如果当前语言和目标语言一致则不切换,如果不一致则切换目标语言 current_laguage = self.driver.find_element(By.XPATH, common_language_button_posXpath).get_attribute("class") target_language_abbreviation = self.language_abbreviation[target_lauguage] target_language_class = self.language_style.format(replaceLanguage=target_language_abbreviation) if current_laguage == target_language_class: pass else: # 点击打开语言选择下拉列表 self.driver.find_element(By.XPATH, common_language_button_posXpath).click() # 点击目标语言 self.driver.find_element(By.XPATH, common_language_button_target_posXpath.format(replaceLanguage=target_lauguage)).click() time.sleep(0.3) # 选择后获取选择语言进行核对 current_laguage = self.driver.find_element(By.XPATH, common_language_button_posXpath).get_attribute("class") if current_laguage == target_language_class: pass else: raise ValueError( f" Failed to switch target language:{target_lauguage},current_laguage:{current_laguage}") return self.language_abbreviation[target_lauguage] def current_language(self): """ 获取当前语言:English/简体中文/Pусский :param data: :return: """ current_laguage = "English" # 等待语言栏出现 is_display = self.driver.find_element(By.XPATH, common_language_button_posXpath).is_displayed() if is_display == False: print("未加载出当前语言栏,请确认定位是否更改") return 0 time.sleep(0.5) current_laguage = self.driver.find_element(By.XPATH, common_language_button_posXpath).get_attribute("class") if 'iconfont cursorPointer icon-Lang_enSelect' == current_laguage: current_laguage = "English" elif 'iconfont cursorPointer icon-Lang_zhSelect' == current_laguage: current_laguage = "简体中文" #elif 'iconfont cursorPointer icon-Lang_pySelect' == current_laguage: else: current_laguage = "Русский" return current_laguage @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 @screenshot_on_failure def _modify(self, data: {}): """ 修改数据,使用方法 :param data: :return: """ if data["model"].strip().lower() == "create": # 如果是create参数用例,则不用执行修改方法 return 0 # 点击勾选第一行选择框 self.first_row_checkBox_element.click() # 变量来自查询函数 self.driver.find_element(By.ID, listPage_manageUsers_loginProfiles_editButton_posId).click() # 在Edit Login Profiles界面操作 if "->" in data["name"].strip(): #temp_value = self.my_random.random_name() self.random_name = self.my_random.random_name() # Profile Name self.driver.find_element(By.ID, loginProfiles_input_Name_posId).clear() self.driver.find_element(By.ID, loginProfiles_input_Name_posId).send_keys(self.random_name) # 输入名字 # 页面其它选项操作 self._operate_page(data, operation_type="modify") # 点击Ok按钮提交 self.driver.find_element(By.ID, loginProfiles_button_oK_posId).click() return self.random_name @screenshot_on_failure def _del(self): """ 默认删除查询到的第一个结果 :return: """ # 点击勾选第一行选择框 self.first_row_checkBox_element.click() # 变量来自查询函数 # 调用profiles的功能删除方法 self.login_profiles_po.delete(listPage_manageUsers_loginProfiles_delButton_posXpath, listPage_common_button_yes_posCss, listPage_common_button_no_posCss) #create = _create query = _query modify = _modify delete = _del