diff --git a/Aops_Auto_Test/Aops_Web_Auto_Test/page_element/conf_trace.yaml b/Aops_Auto_Test/Aops_Web_Auto_Test/page_element/conf_trace.yaml new file mode 100644 index 0000000000000000000000000000000000000000..6c87034b13c12aa436d12955f6286dfc4e584984 --- /dev/null +++ b/Aops_Auto_Test/Aops_Web_Auto_Test/page_element/conf_trace.yaml @@ -0,0 +1,17 @@ +conf_trace_menu: 'xpath==//span[text()="配置溯源"]/parent::div' +domain_magt_menu: 'xpath==//span[text()="业务域管理"]/parent::li' +configuration_log_menu: 'xpath==//span[text()="配置变更日志"]/parent::li' +add_domain_button: 'class==add-domain' +add_domain_page_title: 'xpath==//div[text()="创建业务域"]' +domain_name: 'id==form_item_domainName' +file_trace: 'xpath==//label[@title="文件追溯"]/parent::div/following::div[1]//button' +file_monitor: 'xpath==//label[@title="文件监控"]/parent::div/following::div[1]//button' +delete_domain: 'xpath==//td[text()="****"]/following-sibling::td//a[text()="删除"]' +add_host: 'xpath==//span[text()="添加主机"]/parent::button' +add_host_title: 'xpath==//div[text()="添加主机"]' +add_host_checkbox: 'xpath==//span[text()="****"]/parent::li' +add_host_domain: 'id==form_item_domainName' +add_host_all: 'xpath==//div[@class="ant-transfer-list"][1]//div[@class="ant-transfer-list-header"]//label' +host_checkbox: 'xpath==//div[@class="ant-transfer-list"][1]//span[text()="****"]' +right_icon: 'xpath==//span[@aria-label="right"]' +domain_detail_button: 'xpath==//td[text()="****"]/following-sibling::td[3]//a[text()="业务域详情"]' diff --git a/Aops_Auto_Test/Aops_Web_Auto_Test/page_object/conf_trace.py b/Aops_Auto_Test/Aops_Web_Auto_Test/page_object/conf_trace.py new file mode 100644 index 0000000000000000000000000000000000000000..a5c2e2fd314a0f973fac6d008c6676ad9a823055 --- /dev/null +++ b/Aops_Auto_Test/Aops_Web_Auto_Test/page_object/conf_trace.py @@ -0,0 +1,171 @@ +# -*-coding:utf-8-*- +from Aops_Web_Auto_Test.page_object.base_page import CommonPagingWebPage +from Aops_Web_Auto_Test.common.readelement import Element + +conf_trace_ele = Element('conf_trace') + + +class ConfTracePage(CommonPagingWebPage): + + def enter_domain_page(self): + """进入业务域菜单""" + expanded = self.get_element_attr(conf_trace_ele['conf_trace_menu'], 'aria-expanded') + if expanded == "false": + self.click_element(conf_trace_ele['conf_trace_menu']) + self.click_element(conf_trace_ele['domain_magt_menu']) + + def add_domain(self, cluster_name, domain_name, action='confirm'): + """ + 新建命令 + :param cluster_name: 需要添加域名的集群名称 + :param domain_name: 要添加的域名 + :param action: 操作确认或取消,默认为'confirm' + """ + self.click_element(conf_trace_ele['add_domain_button']) + self.find_element(conf_trace_ele['add_domain_page_title']) + self.select_cluster(cluster_name) + self.input_text(conf_trace_ele['domain_name'], domain_name) + self.file_trace() + self.file_monitor() + try: + if action == "confirm": + self.click_confirm_button() + elif action == "cancel": + self.click_cancel_button() + else: + raise ValueError("action参数必须是confirm或者cancel") + except Exception as e: + raise RuntimeError(f"处理按钮时发生错误:{e}") + + def delete_domain(self, domain_name, action='confirm'): + """ + 删除domain + :param domain_name: 要删除的域名 + :param action: 操作确认或取消,默认为'confirm' + """ + new_loc = self.replace_locator_text(conf_trace_ele['delete_domain'], domain_name) + self.click_element(new_loc) + try: + if action == "confirm": + self.click_confirm_button() + elif action == "cancel": + self.click_cancel_button() + else: + raise ValueError("action参数必须是confirm或者cancel") + except Exception as e: + raise RuntimeError(f"处理按钮时发生错误:{e}") + + def get_all_domain(self): + """ + 从业务域列表获取所有业务域名称 + """ + return self.get_column_data_all_pages(1)[1] + + def file_trace(self, action: bool = True) -> None: + """ + 文件跟踪开关状态 + + :param action: 控制开关状态,True=开启/False=关闭 + :param action: 操作确认或取消,默认为'confirm' + """ + if not isinstance(action, bool): + raise ValueError("action参数必须是布尔类型") + + def get_current_state() -> str: + return self.get_element_attr(conf_trace_ele['file_trace'], 'aria-checked') + + current_state = get_current_state() + target_state = 'true' if action else 'false' + + try: + if current_state != target_state: + self.click_element(conf_trace_ele['file_trace']) + if get_current_state() != target_state: + raise RuntimeError( + f"状态修正失败:预期 {target_state},当前 {get_current_state()}" + ) + except Exception as e: + raise RuntimeError( + f"文件跟踪开关操作失败(action={action}): {str(e)}" + ) from e + + def file_monitor(self, action: bool = True) -> None: + """ + 文件监控开关状态 + + :param action: 控制开关状态,True=开启/False=关闭 + :param action: 操作确认或取消,默认为'confirm' + """ + if not isinstance(action, bool): + raise ValueError("action参数必须是布尔类型") + + def get_current_state() -> str: + return self.get_element_attr(conf_trace_ele['file_monitor'], 'aria-checked') + + current_state = get_current_state() + target_state = 'true' if action else 'false' + + try: + if current_state != target_state: + self.click_element(conf_trace_ele['file_trace']) + if get_current_state() != target_state: + raise RuntimeError( + f"状态修正失败:预期 {target_state},当前 {get_current_state()}" + ) + except Exception as e: + raise RuntimeError( + f"文件监控开关操作失败(action={action}): {str(e)}" + ) from e + + def enter_domain_detail(self, domain): + """ + 进入业务域详情页面 + """ + new_loc = self.replace_locator_text(conf_trace_ele['domain_detail_button'], domain) + self.click_element(new_loc) + + def add_host(self, domain, hosts=None, select_all=False, action='confirm'): + """ + 给业务域添加主机 + + :param domain: 域名字符串,用于进入域名详细页面 + :param hosts: 主机列表,指定要添加的主机;若为None且select_all为False,则会抛出异常 + :param select_all: 布尔值,表示是否选择所有主机;True选择所有主机,False只选择指定的主机 + :param action: 操作动作,'confirm'表示确认添加,'cancel'表示取消操作 + """ + if not isinstance(domain, str): + raise TypeError("domain参数必须是字符串类型") + if hosts is not None and not isinstance(hosts, list): + raise TypeError("hosts参数必须是列表类型或None") + if action not in ['confirm', 'cancel']: + raise ValueError("action参数必须是'confirm'或'cancel'") + if hosts is None and not select_all: + raise ValueError("当select_all为False时,hosts参数不能为空") + try: + self.enter_domain_detail(domain) + self.click_element(conf_trace_ele['add_host']) + self.find_element(conf_trace_ele['add_host_title']) + + if select_all: + self.click_element(conf_trace_ele['add_host_all']) + else: + for host in hosts: + new_loc = self.replace_locator_text(conf_trace_ele['host_checkbox'], host) + self.click_element(new_loc) + self.click_element(conf_trace_ele['right_icon']) + + if action == "confirm": + self.click_confirm_button() + elif action == "cancel": + self.click_cancel_button() + + except KeyError as ke: + raise RuntimeError(f"配置文件中缺少关键定位器:{ke}") + except TypeError as te: + raise RuntimeError(f"类型错误:{te}") + except Exception as e: + raise RuntimeError(f"处理按钮时发生未知错误:{e}") + + + + diff --git a/Aops_Auto_Test/Aops_Web_Auto_Test/test_case/conf_trace/conftest.py b/Aops_Auto_Test/Aops_Web_Auto_Test/test_case/conf_trace/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..0499342d1c8f1bc7286a549046cdacebe0a75d63 --- /dev/null +++ b/Aops_Auto_Test/Aops_Web_Auto_Test/test_case/conf_trace/conftest.py @@ -0,0 +1,13 @@ +import pytest +from Aops_Web_Auto_Test.common.readconfig import ini +from Aops_Web_Auto_Test.page_object.user_magt import UserMagtPage + + +@pytest.fixture(scope='module', autouse=True) +def user_login(drivers): + """用户登录""" + user = UserMagtPage(drivers) + user.get_url(ini.url) + user.user_login(ini.user, ini.password) + yield + user.user_logout(ini.user) diff --git a/Aops_Auto_Test/Aops_Web_Auto_Test/test_case/conf_trace/test_domain.py b/Aops_Auto_Test/Aops_Web_Auto_Test/test_case/conf_trace/test_domain.py new file mode 100644 index 0000000000000000000000000000000000000000..d2f02144cf2d9de0a14f665fc99f2d5736628b77 --- /dev/null +++ b/Aops_Auto_Test/Aops_Web_Auto_Test/test_case/conf_trace/test_domain.py @@ -0,0 +1,85 @@ +# -*-coding:utf-8-*- +import string + +import pytest +from Aops_Web_Auto_Test.common import createtestdata +from Aops_Web_Auto_Test.page_object.conf_trace import ConfTracePage + + +@pytest.fixture(scope='class') +def domain(drivers) -> ConfTracePage: + return ConfTracePage(drivers) + + +class TestDomain: + + @pytest.fixture(scope="function", autouse=True) + def create_data(self, domain): + self.domain_name = None + yield + try: + domain.click_close_button() + except Exception as e: + print(f"关闭新增业务域时发生错误:{e}") + pass + + if self.domain_name: + try: + domain.delete_domain(self.domain_name) + except Exception as e: + print(f"删除domain: {self.domain_name}失败", e) + pass + + def test_add_domain_01_valid_data(self, drivers, domain): + """添加业务域-有效数据""" + domain.enter_domain_page() + domain_name = createtestdata.domain_name() + self.domain_name = domain_name + domain.add_domain('local-cluster', domain_name) + domain.click_refresh_button() + assert domain.search_in_column(domain_name) + + def test_add_domain_02_duplicate_data(self, drivers, domain): + """添加业务域-重复添加""" + domain.enter_domain_page() + domain_name = createtestdata.domain_name() + domain.add_domain('local-cluster', domain_name) + domain.click_refresh_button() + assert domain.search_in_column(domain_name) + self.domain_name = domain_name + domain.add_domain('local-cluster', domain_name) + assert domain.get_notice_text() == "失败" + + def test_add_domain_03_cancal(self, drivers, domain): + """添加业务域-取消添加""" + domain.enter_domain_page() + domain_name = createtestdata.domain_name() + domain.add_domain('local-cluster', domain_name, action='cancel') + domain.click_refresh_button() + assert not domain.search_in_column(domain_name) + + def test_add_domain_04_invalid_domain(self, drivers, domain): + """添加业务域-无效的domain""" + domain.enter_domain_page() + domain_name = createtestdata.domain_name(min_len=27, max_len=28) + domain.add_domain('local-cluster', domain_name) + assert domain.get_item_explain_error("业务域名称") == "名称长度不应超过26个字符" + domain.click_cancel_button() + domain.add_domain('local-cluster', "") + assert domain.get_item_explain_error("业务域名称") == "请输入业务域名称" + domain.click_cancel_button() + domain_name = createtestdata.domain_name(characters="".join(set(string.printable) - set(string.ascii_letters) - set(string.digits) - {'-', '_', '.'})) + domain.add_domain('local-cluster', domain_name) + assert domain.get_item_explain_error("业务域名称") == "名称只能输入大小写字母、下划线、中划线和小数点" + + def test_delete_domain_01(self, drivers, domain): + """删除业务域""" + domain.enter_domain_page() + domain_name = createtestdata.domain_name() + domain.add_domain('local-cluster', domain_name) + domain.click_refresh_button() + assert domain.search_in_column(domain_name) + domain.delete_domain(domain_name) + assert not domain.search_in_column(domain_name) + +