Skip to content

Commit

Permalink
第六十六章_表格的封装--获取指定列的值的列表
Browse files Browse the repository at this point in the history
  • Loading branch information
aino980 committed Aug 15, 2024
1 parent de0c9d9 commit aeb1044
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 47 deletions.
File renamed without changes.
1 change: 0 additions & 1 deletion module/BaiduPage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from module import *
from module.BasePage import PageObject


class Baidu(PageObject):
Expand Down
30 changes: 4 additions & 26 deletions module/BasePage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os.path
from module import *
from utils.globalMap import GlobalMap
from module.table import Table


class PageObject:
Expand All @@ -11,6 +10,9 @@ def __init__(self, page: Page):
def navigate(self):
self.page.goto(self.url)

def table(self, 唯一文字, 表格序号=-1):
return Table(self.page, 唯一文字, 表格序号)

def click_button(self, button_name, timeout=30_000):
button_loc = self.page.locator("button")
for 单字符 in button_name:
Expand All @@ -25,30 +27,6 @@ def search(self, 搜索内容: str, placeholder=None):
self.page.wait_for_load_state("networkidle")


def 使用new_context登录并返回实例化的page(new_context, 用户别名):
from module.PageInstance import PageIns
global_map = GlobalMap()
被测环境 = global_map.get("env")
用户名 = MyData().userinfo(被测环境, 用户别名)["username"]
密码 = MyData().userinfo(被测环境, 用户别名)["password"]
with FileLock(get_path(f".temp/{被测环境}-{用户别名}.lock")):
if os.path.exists(get_path(f".temp/{被测环境}-{用户别名}.json")):
context: BrowserContext = new_context(storage_state=get_path(f".temp/{被测环境}-{用户别名}.json"))
page = context.new_page()
my_page = PageIns(page)
my_page.我的任务.navigate()
expect(my_page.登录页.用户名输入框.or_(my_page.登录页.通知铃铛)).to_be_visible()
if my_page.登录页.用户名输入框.count():
my_page.登录页.登录(用户名, 密码)
my_page.page.context.storage_state(path=get_path(f".temp/{被测环境}-{用户别名}.json"))
else:
context: BrowserContext = new_context()
page = context.new_page()
my_page = PageIns(page)
my_page.登录页.登录(用户名, 密码)
my_page.page.context.storage_state(path=get_path(f".temp/{被测环境}-{用户别名}.json"))
return my_page

# with f"这里使用filelock进行文件锁,锁的文件名字为{被测环境}-{用户别名}.lock":
# if f".temp/{被测环境}-{用户别名}.json存在,判断方法os.path.exists":
# context = new_context(storage_state=f".temp/{被测环境}-{用户别名}.json")
Expand Down
10 changes: 0 additions & 10 deletions module/PageInstance.py

This file was deleted.

44 changes: 41 additions & 3 deletions module/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
import pytest
import time
from playwright.sync_api import Page, expect, BrowserContext
import os
import sys
import re
from playwright.sync_api import Page, expect, BrowserContext, Locator
from module.BasePage import PageObject
from utils.GetPath import get_path
from filelock import FileLock
from data_module.my_Data import MyData
from data_module.auth_Data import MyData
from utils.globalMap import GlobalMap
from module.BaiduPage import Baidu
from module.登录页 import 登录页_类
from module.我的任务 import 我的任务_类
from module.项目集 import 项目集_类
from module.项目集 import 项目集_类


class PageIns:
def __init__(self, page: Page):
self.page = page
self.百度 = Baidu(self.page)
self.登录页 = 登录页_类(self.page)
self.我的任务 = 我的任务_类(self.page)
self.项目集 = 项目集_类(self.page)

@staticmethod
def 使用new_context登录并返回实例化的page(new_context, 用户别名):
global_map = GlobalMap()
被测环境 = global_map.get("env")
用户名 = MyData().userinfo(被测环境, 用户别名)["username"]
密码 = MyData().userinfo(被测环境, 用户别名)["password"]
with FileLock(get_path(f".temp/{被测环境}-{用户别名}.lock")):
if os.path.exists(get_path(f".temp/{被测环境}-{用户别名}.json")):
context: BrowserContext = new_context(storage_state=get_path(f".temp/{被测环境}-{用户别名}.json"))
page = context.new_page()
my_page = PageIns(page)
my_page.我的任务.navigate()
expect(my_page.登录页.用户名输入框.or_(my_page.登录页.通知铃铛)).to_be_visible()
if my_page.登录页.用户名输入框.count():
my_page.登录页.登录(用户名, 密码)
my_page.page.context.storage_state(path=get_path(f".temp/{被测环境}-{用户别名}.json"))
else:
context: BrowserContext = new_context()
page = context.new_page()
my_page = PageIns(page)
my_page.登录页.登录(用户名, 密码)
my_page.page.context.storage_state(path=get_path(f".temp/{被测环境}-{用户别名}.json"))
return my_page
56 changes: 56 additions & 0 deletions module/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import random

from module import *


class Table:
def __init__(self, page: Page, 唯一文字: str, 表格序号: int = -1):
self.page = page
self.page.wait_for_load_state("networkidle")
self.table_div = self.page.locator(".ant-table-wrapper").filter(has_text=唯一文字).nth(表格序号)
self.table_header_tr = self.table_div.locator("//thead/tr")

def get_header_index(self, 表头文字: str) -> int:
return self.table_header_tr.locator("th").all_text_contents().index(表头文字)

def get_row_locator(self, 行元素定位: Locator) -> Locator:
return self.table_div.locator("tr").filter(has=行元素定位)

def get_cell(self, 表头文字or列序号: str | int, 行元素定位or行序号or行文字: Locator | int | str) -> Locator:
if isinstance(表头文字or列序号, str):
列序号 = self.get_header_index(表头文字or列序号)
else:
列序号 = 表头文字or列序号

if isinstance(行元素定位or行序号or行文字, Locator):
行定位 = self.get_row_locator(行元素定位or行序号or行文字)
elif isinstance(行元素定位or行序号or行文字, str):
行定位 = self.table_div.locator("tr").filter(has_text=行元素定位or行序号or行文字)
else:
行定位 = self.table_div.locator("tbody").locator('//tr[not(@aria-hidden="true")]').nth(行元素定位or行序号or行文字)

return 行定位.locator("td").nth(列序号)

def get_row_dict(self, 行元素定位or行序号: Locator | int = "random") -> dict:
if isinstance(行元素定位or行序号, int):
tr = self.table_div.locator("tbody").locator("tr").locator("visible=true").nth(行元素定位or行序号)
elif isinstance(行元素定位or行序号, Locator):
tr = self.table_div.locator("tr").filter(has=行元素定位or行序号)
else:
all_tr = self.table_div.locator("tbody").locator("tr").locator("visible=ture").all()
tr = random.choice(all_tr)

td_text_list = tr.locator("td").all_text_contents()
header_text_list = self.table_header_tr.locator("th").all_text_contents()
row_dict = dict(zip(header_text_list, td_text_list))
return row_dict

def get_col_list(self, 表头文字: str) -> list:
index = self.get_header_index(表头文字)
all_tr = self.table_div.locator("tbody").locator("tr").locator("visible=true").all()
col_list = []
for tr in all_tr:
col_list.append(tr.locator("td").nth(index).text_content())
return col_list


1 change: 0 additions & 1 deletion module/我的任务.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from module import *
from module.BasePage import PageObject


class 我的任务_类(PageObject):
Expand Down
1 change: 0 additions & 1 deletion module/登录页.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from module import *
from module.BasePage import PageObject


class 登录页_类(PageObject):
Expand Down
6 changes: 5 additions & 1 deletion module/项目集.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from module import *
from module.BasePage import PageObject


class 项目集_类(PageObject):
Expand All @@ -12,6 +11,10 @@ def __init__(self, page):
self.运维操作 = self.page.get_by_text("运维操作")
self.暂无数据 = self.page.get_by_text("暂无数据")

@property
def 主表格(self):
return self.table("项目集名称")

def 创建项目集(self, 项目集名称="自动化创建项目集", 是否需要纳秒时间戳=True):
self.navigate()
self.click_button("新建")
Expand All @@ -38,3 +41,4 @@ def 删除项目集(self, 项目集名称):




4 changes: 2 additions & 2 deletions testcases/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from playwright.sync_api import Page, expect, Browser, BrowserContext
from module.PageInstance import PageIns
from module import PageIns
import pytest
from filelock import FileLock
from utils.GetPath import get_path
from utils.globalMap import GlobalMap
from module.BasePage import 使用new_context登录并返回实例化的page
from module import PageIns
4 changes: 2 additions & 2 deletions testcases/new_context_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@


def test_new_context(new_context):
my_page_测试员 = 使用new_context登录并返回实例化的page(new_context, "测试员")
my_page_项目经理 = 使用new_context登录并返回实例化的page(new_context, "项目经理")
my_page_测试员 = PageIns.使用new_context登录并返回实例化的page(new_context, "测试员")
my_page_项目经理 = PageIns.使用new_context登录并返回实例化的page(new_context, "项目经理")
my_page_测试员.page
11 changes: 11 additions & 0 deletions testcases/table_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from testcases import *


def test_new_context(new_context):
my_page_测试员 = PageIns.使用new_context登录并返回实例化的page(new_context, "测试员")
my_page_测试员.项目集.navigate()
# index = my_page_测试员.项目集.主表格.get_header_index("开始时间")
# loc = my_page_测试员.项目集.主表格.get_row_locator(my_page_测试员.page.get_by_text("table_test"))
# my_page_测试员.项目集.主表格.get_cell(1, 10).text_content()
# my_page_测试员.项目集.主表格.get_row_dict()
my_page_测试员.项目集.主表格.get_col_list()

0 comments on commit aeb1044

Please sign in to comment.