diff --git a/data_module/__init__.py b/data_module/__init__.py new file mode 100644 index 0000000..fe034e7 --- /dev/null +++ b/data_module/__init__.py @@ -0,0 +1,15 @@ +import time + + +class As_dict: + def as_dict(self): + return_dict = self.__dict__ + temp_dict = self.__dict__.copy() + for key, value in temp_dict.items(): + if value == "": + return_dict.pop(key) + continue + if "时间戳" in value: + value = str(value).replace("时间戳", str(time.time_ns())) + return_dict.update({key: value}) + return return_dict \ No newline at end of file diff --git a/data_module/项目集数据类模块.py b/data_module/项目集数据类模块.py new file mode 100644 index 0000000..991ebfb --- /dev/null +++ b/data_module/项目集数据类模块.py @@ -0,0 +1,10 @@ +from data_module import * +from dataclasses import dataclass + + +@dataclass +class 项目集数据类_新建项目集(As_dict): + 项目集名称: str = "自动化创建的项目集_时间戳" + 项目集周期: str = "" + 父项目集: str = "" + 子项目集: str = "" \ No newline at end of file diff --git a/module/BasePage.py b/module/BasePage.py index 92b4137..20dd608 100644 --- a/module/BasePage.py +++ b/module/BasePage.py @@ -1,11 +1,13 @@ from module import * from module.table import Table +from module.locators import Locators class PageObject: def __init__(self, page: Page): self.page = page self.url = "" + self.locators = Locators(self.page) def navigate(self): self.page.goto(self.url) @@ -26,27 +28,37 @@ class PageObject: self.page.locator(".ant-input-affix-wrapper input").fill(搜索内容) self.page.wait_for_load_state("networkidle") + def 表单_文本框填写(self, 表单项名称: str, 需要填写的文本: str, 表单最上层定位: Locator = None, timeout: float = None): + if 表单最上层定位: + 表单最上层定位.locator(self.locators.表单项中包含操作元素的最上级div(表单项名称)).locator("input,textarea").locator("visible=true").last.fill(需要填写的文本, timeout=timeout) + else: + self.locators.表单项中包含操作元素的最上级div(表单项名称).locator("input,textarea").locator("visible=true").last.fill(需要填写的文本, timeout=timeout) - # with f"这里使用filelock进行文件锁,锁的文件名字为{被测环境}-{用户别名}.lock": - # if f".temp/{被测环境}-{用户别名}.json存在,判断方法os.path.exists": - # context = new_context(storage_state=f".temp/{被测环境}-{用户别名}.json") - # page = context.new_page - # my_page = "PageIns实例化page,获得所有页面的操控方法" - # # 需要新增一个首页的封装 - # my_page.首页.navigate() - # 登录成功 = "判断下是成功登录上了,还是登录失败" - # if not 登录成功: - # my_page.登录页.登录(用户名, 密码) - # f"把storage_state保存为.temp/{被测环境}-{用户别名}.json" - # else: - # context = new_context() - # page = context.new_page - # my_page = "PageIns实例化page,获得所有页面的操控方法" - # my_page.登录页.登录(用户名, 密码) - # f"把storage_state保存为.temp/{被测环境}-{用户别名}.json" - # return my_page - - - + def 表单_下拉框选择(self, 表单项名称: str, 需要选择的项: str, 表单最上层定位: Locator = None, timeout: float = None): + if 表单最上层定位: + 表单最上层定位.locator(self.locators.表单项中包含操作元素的最上级div(表单项名称)).locator("visible=true").click(timeout=timeout) + if 表单最上层定位.locator(self.locators.表单项中包含操作元素的最上级div(表单项名称)).locator('//input[@type="search"]').count(): + 表单最上层定位.locator(self.locators.表单项中包含操作元素的最上级div(表单项名称)).locator('//input[@type="search"]').fill(需要选择的项, timeout=timeout) + self.page.locator(".ant-select-dropdown").locator("visible=true").get_by_text(需要选择的项).click(timeout=timeout) + else: + self.locators.表单项中包含操作元素的最上级div(表单项名称).locator("visible=true").click(timeout=timeout) + if self.locators.表单项中包含操作元素的最上级div(表单项名称).locator('//input[@type="search"]').count(): + self.locators.表单项中包含操作元素的最上级div(表单项名称).locator('//input[@type="search"]').fill(需要选择的项, timeout=timeout) + self.page.locator(".ant-select-dropdown").locator("visible=true").get_by_text(需要选择的项).click(timeout=timeout) + expect(self.page.locator(".ant-select-dropdown")).to_be_hidden(timeout=timeout) + def 表单_radio选择(self, 表单项名称: str, 需要选择的项: str, 表单最上层定位: Locator = None, timeout: float = None): + if 表单最上层定位: + 表单最上层定位.locator(self.locators.表单项中包含操作元素的最上级div(表单项名称)).locator("label").locator("visible=true").filter(has_text=需要选择的项).locator("input").check(timeout=timeout) + else: + self.locators.表单项中包含操作元素的最上级div(表单项名称).locator("label").locator("visible=true").filter(has_text=需要选择的项).locator("input").check(timeout=timeout) + def 表单_switch开关(self, 表单项名称: str, 开关状态: str, 表单最上层定位: Locator = None, timeout: float = None): + if "开" in 开关状态 or "是" in 开关状态: + 开关状态bool = True + else: + 开关状态bool = False + if 表单最上层定位: + 表单最上层定位.locator(self.locators.表单项中包含操作元素的最上级div(表单项名称)).get_by_role("switch").set_checked(开关状态bool, timeout=timeout) + else: + self.locators.表单项中包含操作元素的最上级div(表单项名称).get_by_role("switch").set_checked(开关状态bool, timeout=timeout) \ No newline at end of file diff --git a/module/__init__.py b/module/__init__.py index 337ce3d..0badf79 100644 --- a/module/__init__.py +++ b/module/__init__.py @@ -3,6 +3,7 @@ import time import os import sys import re +import random from playwright.sync_api import Page, expect, BrowserContext, Locator from module.BasePage import PageObject from utils.GetPath import get_path diff --git a/module/locators.py b/module/locators.py new file mode 100644 index 0000000..f09f7f0 --- /dev/null +++ b/module/locators.py @@ -0,0 +1,21 @@ +from module import * + + +class Locators: + def __init__(self, page: Page): + self.page = page + + def button_按钮(self, text, nth=-1) -> Locator: + button = self.page.locator("button") + for word in text: + button = button.filter(has_text=word) + return button.locator('visible=true').nth(nth) + + def below_元素下方紧邻的元素(self, 要找的元素类型="*") -> Locator: + return self.page.locator(f"xpath=/following::{要找的元素类型}[position()=1]") + + def 表单项中包含操作元素的最上级div(self, 字段名: str) -> Locator: + 最终上级元素locator = self.page.locator("label").locator("visible=true").filter(has=self.page.get_by_text(字段名)).locator(self.below_元素下方紧邻的元素()) + return 最终上级元素locator + + diff --git a/module/table.py b/module/table.py index 6164816..9a242f1 100644 --- a/module/table.py +++ b/module/table.py @@ -1,5 +1,3 @@ -import random - from module import * diff --git a/testcases/项目集的新建.py b/testcases/项目集的新建.py index 948c0f5..f4dfba5 100644 --- a/testcases/项目集的新建.py +++ b/testcases/项目集的新建.py @@ -2,12 +2,12 @@ from testcases import * def test_项目集的新建(new_context, 删除项目集): - my_page_测试员 = 使用new_context登录并返回实例化的page(new_context, "测试员") + my_page_测试员 = PageIns.使用new_context登录并返回实例化的page(new_context, "测试员") my_page_测试员.项目集.创建项目集() @pytest.fixture def 删除项目集(new_context): yield - my_page_测试员 = 使用new_context登录并返回实例化的page(new_context, "测试员") + my_page_测试员 = PageIns.使用new_context登录并返回实例化的page(new_context, "测试员") my_page_测试员.项目集.删除项目集("自动化创建项目集")