mangoautomation 1.1.22__py3-none-any.whl → 1.1.24__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of mangoautomation might be problematic. Click here for more details.
- mangoautomation/enums/__init__.py +0 -1
- mangoautomation/exceptions/__init__.py +0 -2
- mangoautomation/exceptions/error_msg.py +3 -3
- mangoautomation/mangos/__init__.py +14 -0
- mangoautomation/mangos/__pycache__/__init__.cpython-310.pyc +0 -0
- mangoautomation/mangos/mangos.cp310-win_amd64.pyd +0 -0
- mangoautomation/mangos/mangos.cp312-win_amd64.pyd +0 -0
- mangoautomation/mangos/mangos.cpython-310-darwin.so +0 -0
- mangoautomation/mangos/mangos.cpython-310-x86_64-linux-gnu.so +0 -0
- mangoautomation/mangos/mangos.cpython-312-darwin.so +0 -0
- mangoautomation/mangos/mangos.cpython-312-x86_64-linux-gnu.so +0 -0
- mangoautomation/models/__init__.py +0 -1
- mangoautomation/models/_ui_model.py +1 -0
- mangoautomation/tools/__init__.py +0 -1
- mangoautomation/tools/_uiautodev.py +1 -1
- mangoautomation/uidrives/__init__.py +2 -3
- mangoautomation/uidrives/_async_element.py +74 -48
- mangoautomation/uidrives/_base_data.py +16 -4
- mangoautomation/uidrives/_driver_object.py +7 -40
- mangoautomation/uidrives/_sync_element.py +75 -46
- mangoautomation/uidrives/android/__init__.py +2 -16
- mangoautomation/uidrives/pc/__init__.py +1 -2
- mangoautomation/uidrives/pc/element.py +1 -2
- mangoautomation/uidrives/pc/input_device.py +1 -2
- mangoautomation/uidrives/web/__init__.py +5 -0
- mangoautomation/uidrives/web/{async_web/__init__.py → _async_web.py} +82 -81
- mangoautomation/uidrives/web/{sync_web/__init__.py → _sync_web.py} +80 -74
- mangoautomation-1.1.24.dist-info/METADATA +42 -0
- mangoautomation-1.1.24.dist-info/RECORD +48 -0
- {mangoautomation-1.1.22.dist-info → mangoautomation-1.1.24.dist-info}/WHEEL +1 -1
- tests/__init__.py +0 -10
- tests/demo1.py +94 -0
- tests/get_ope.py +12 -3
- tests/test_ai_element.py +36 -0
- tests/test_ui_web.py +7 -3
- mangoautomation/uidrives/android/_application.py +0 -70
- mangoautomation/uidrives/android/_assertion.py +0 -90
- mangoautomation/uidrives/android/_customization.py +0 -15
- mangoautomation/uidrives/android/_element.py +0 -169
- mangoautomation/uidrives/android/_equipment.py +0 -151
- mangoautomation/uidrives/android/_new_android.py +0 -54
- mangoautomation/uidrives/android/_page.py +0 -116
- mangoautomation/uidrives/web/async_web/_assertion.py +0 -303
- mangoautomation/uidrives/web/async_web/_browser.py +0 -112
- mangoautomation/uidrives/web/async_web/_customization.py +0 -14
- mangoautomation/uidrives/web/async_web/_element.py +0 -250
- mangoautomation/uidrives/web/async_web/_input_device.py +0 -82
- mangoautomation/uidrives/web/async_web/_new_browser.py +0 -153
- mangoautomation/uidrives/web/async_web/_page.py +0 -63
- mangoautomation/uidrives/web/sync_web/_assertion.py +0 -304
- mangoautomation/uidrives/web/sync_web/_browser.py +0 -112
- mangoautomation/uidrives/web/sync_web/_customization.py +0 -14
- mangoautomation/uidrives/web/sync_web/_element.py +0 -250
- mangoautomation/uidrives/web/sync_web/_input_device.py +0 -80
- mangoautomation/uidrives/web/sync_web/_new_browser.py +0 -153
- mangoautomation/uidrives/web/sync_web/_page.py +0 -61
- mangoautomation-1.1.22.dist-info/METADATA +0 -34
- mangoautomation-1.1.22.dist-info/RECORD +0 -59
- {mangoautomation-1.1.22.dist-info → mangoautomation-1.1.24.dist-info/licenses}/LICENSE +0 -0
- {mangoautomation-1.1.22.dist-info → mangoautomation-1.1.24.dist-info}/top_level.txt +0 -0
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# @Project: 芒果测试平台
|
|
3
|
-
# @Description: # @Time : 2023-04-25 22:33
|
|
4
|
-
# @Author : 毛鹏
|
|
5
|
-
import asyncio
|
|
6
|
-
import json
|
|
7
|
-
import os.path
|
|
8
|
-
import traceback
|
|
9
|
-
from urllib.parse import urlparse
|
|
10
|
-
|
|
11
|
-
from playwright._impl._errors import TimeoutError, Error, TargetClosedError
|
|
12
|
-
|
|
13
|
-
from mangotools.decorator import async_method_callback
|
|
14
|
-
from mangotools.models import MethodModel
|
|
15
|
-
from ..._base_data import BaseData
|
|
16
|
-
from ....exceptions import MangoAutomationError
|
|
17
|
-
from ....exceptions.error_msg import ERROR_MSG_0049, ERROR_MSG_0013, ERROR_MSG_0058, ERROR_MSG_0059, ERROR_MSG_0010
|
|
18
|
-
from ....tools import Meta
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class AsyncWebBrowser(metaclass=Meta):
|
|
22
|
-
"""浏览器操作"""
|
|
23
|
-
|
|
24
|
-
def __init__(self, base_data: BaseData):
|
|
25
|
-
self.base_data = base_data
|
|
26
|
-
|
|
27
|
-
@async_method_callback('web', '浏览器操作', 0, [
|
|
28
|
-
MethodModel(n='等待时间', f='_time', p='请输入等待时间', d=True)])
|
|
29
|
-
async def w_wait_for_timeout(self, _time: int):
|
|
30
|
-
"""强制等待"""
|
|
31
|
-
await asyncio.sleep(int(_time))
|
|
32
|
-
|
|
33
|
-
@async_method_callback('web', '浏览器操作', 1, [
|
|
34
|
-
MethodModel(n='url地址', f='url', p='请输入URL', d=True)])
|
|
35
|
-
async def w_goto(self, url: str):
|
|
36
|
-
"""打开URL"""
|
|
37
|
-
try:
|
|
38
|
-
result = urlparse(url)
|
|
39
|
-
if not all([result.scheme, result.netloc]):
|
|
40
|
-
raise MangoAutomationError(*ERROR_MSG_0049)
|
|
41
|
-
await self.base_data.page.goto(url, timeout=60000)
|
|
42
|
-
await asyncio.sleep(2)
|
|
43
|
-
except TimeoutError as error:
|
|
44
|
-
self.base_data.log.debug(f'打开URL失败-1,类型:{type(error)},失败详情:{error}')
|
|
45
|
-
raise MangoAutomationError(*ERROR_MSG_0013, value=(url,))
|
|
46
|
-
except TargetClosedError as error:
|
|
47
|
-
self.base_data.setup()
|
|
48
|
-
self.base_data.log.debug(f'打开URL失败-2,类型:{type(error)},失败详情:{error}')
|
|
49
|
-
raise MangoAutomationError(*ERROR_MSG_0010, value=(url,))
|
|
50
|
-
except Error as error:
|
|
51
|
-
self.base_data.log.debug(
|
|
52
|
-
f'打开URL失败-2,类型:{type(error)},失败详情:{error},失败明细:{traceback.format_exc()}')
|
|
53
|
-
raise MangoAutomationError(*ERROR_MSG_0058, value=(url,))
|
|
54
|
-
|
|
55
|
-
@async_method_callback('web', '浏览器操作', 2, [
|
|
56
|
-
MethodModel(n='存储路径', f='path', p='请输入截图名称', d=True)])
|
|
57
|
-
async def w_screenshot(self, path: str):
|
|
58
|
-
"""整个页面截图"""
|
|
59
|
-
try:
|
|
60
|
-
await self.base_data.page.screenshot(path=os.path.join(self.base_data.screenshot_path, path), full_page=True, timeout=10000)
|
|
61
|
-
except (TargetClosedError, TimeoutError) as error:
|
|
62
|
-
self.base_data.log.debug(
|
|
63
|
-
f'截图出现异常失败-1,类型:{type(error)},失败详情:{error},失败明细:{traceback.format_exc()}')
|
|
64
|
-
self.base_data.setup()
|
|
65
|
-
raise MangoAutomationError(*ERROR_MSG_0010)
|
|
66
|
-
except AttributeError as error:
|
|
67
|
-
self.base_data.log.debug(
|
|
68
|
-
f'截图出现异常失败-2,类型:{type(error)},失败详情:{error},失败明细:{traceback.format_exc()}')
|
|
69
|
-
self.base_data.setup()
|
|
70
|
-
raise MangoAutomationError(*ERROR_MSG_0010)
|
|
71
|
-
|
|
72
|
-
@async_method_callback('web', '浏览器操作', 3)
|
|
73
|
-
async def w_alert(self):
|
|
74
|
-
"""设置弹窗不予处理"""
|
|
75
|
-
self.base_data.page.on("dialog", lambda dialog: dialog.accept())
|
|
76
|
-
|
|
77
|
-
@async_method_callback('web', '浏览器操作', 4)
|
|
78
|
-
async def w_get_cookie(self):
|
|
79
|
-
"""获取cookie"""
|
|
80
|
-
with open(os.path.join(self.base_data.download_path, 'storage_state.json'), 'w') as file:
|
|
81
|
-
file.write(json.dumps(await self.base_data.context.storage_state()))
|
|
82
|
-
|
|
83
|
-
@async_method_callback('web', '浏览器操作', 5, [
|
|
84
|
-
MethodModel(n='获取cookie方法的值', f='storage_state', p='请输入获取cookie方法中获取的内容', d=True)])
|
|
85
|
-
async def w_set_cookie(self, storage_state: str):
|
|
86
|
-
"""设置cookie"""
|
|
87
|
-
if isinstance(storage_state, str):
|
|
88
|
-
storage_state = json.loads(storage_state)
|
|
89
|
-
else:
|
|
90
|
-
raise MangoAutomationError(*ERROR_MSG_0059)
|
|
91
|
-
await self.base_data.context.add_cookies(storage_state['cookies'])
|
|
92
|
-
for storage in storage_state['origins']:
|
|
93
|
-
local_storage = storage.get('localStorage', [])
|
|
94
|
-
session_storage = storage.get('sessionStorage', [])
|
|
95
|
-
for item in local_storage:
|
|
96
|
-
await self.base_data.context.add_init_script(
|
|
97
|
-
f"window.localStorage.setItem('{item['name']}', '{item['value']}');")
|
|
98
|
-
for item in session_storage:
|
|
99
|
-
await self.base_data.context.add_init_script(
|
|
100
|
-
f"window.sessionStorage.setItem('{item['name']}', '{item['value']}');")
|
|
101
|
-
await self.base_data.page.reload()
|
|
102
|
-
|
|
103
|
-
@async_method_callback('web', '浏览器操作', 6)
|
|
104
|
-
async def w_clear_cookies(self):
|
|
105
|
-
"""清除所有cookie"""
|
|
106
|
-
await self.base_data.context.clear_cookies()
|
|
107
|
-
|
|
108
|
-
@async_method_callback('web', '浏览器操作', 7)
|
|
109
|
-
async def w_clear_storage(self):
|
|
110
|
-
"""清除本地存储和会话存储"""
|
|
111
|
-
await self.base_data.page.evaluate("() => localStorage.clear()")
|
|
112
|
-
await self.base_data.page.evaluate("() => sessionStorage.clear()")
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# @Project: 芒果测试平台
|
|
3
|
-
# @Description: # @Time : 2023-04-26 22:22
|
|
4
|
-
# @Author : 毛鹏
|
|
5
|
-
|
|
6
|
-
from ..._base_data import BaseData
|
|
7
|
-
from ....tools import Meta
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class AsyncWebCustomization(metaclass=Meta):
|
|
11
|
-
"""定制开发"""
|
|
12
|
-
|
|
13
|
-
def __init__(self, base_data: BaseData):
|
|
14
|
-
self.base_data = base_data
|
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# @Project: 芒果测试平台
|
|
3
|
-
# @Description: # @Time : 2023-04-26 22:22
|
|
4
|
-
# @Author : 毛鹏
|
|
5
|
-
import asyncio
|
|
6
|
-
import os
|
|
7
|
-
|
|
8
|
-
import time
|
|
9
|
-
from playwright.async_api import Locator, Error, TimeoutError
|
|
10
|
-
|
|
11
|
-
from mangotools.decorator import async_method_callback
|
|
12
|
-
from mangotools.models import MethodModel
|
|
13
|
-
from ..._base_data import BaseData
|
|
14
|
-
from ....exceptions import MangoAutomationError
|
|
15
|
-
from ....exceptions.error_msg import ERROR_MSG_0024, ERROR_MSG_0056
|
|
16
|
-
from ....tools import Meta
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
class AsyncWebElement(metaclass=Meta):
|
|
20
|
-
"""元素操作"""
|
|
21
|
-
|
|
22
|
-
def __init__(self, base_data: BaseData):
|
|
23
|
-
self.base_data = base_data
|
|
24
|
-
|
|
25
|
-
@async_method_callback('web', '元素操作', 0, [MethodModel(f='locating')])
|
|
26
|
-
async def w_click(self, locating: Locator):
|
|
27
|
-
"""元素单击"""
|
|
28
|
-
if await locating.count() < 1:
|
|
29
|
-
raise TimeoutError('元素个数小于1,直接操作超时!')
|
|
30
|
-
await locating.click()
|
|
31
|
-
|
|
32
|
-
@async_method_callback('web', '元素操作', 1, [MethodModel(f='locating')])
|
|
33
|
-
async def w_dblclick(self, locating: Locator):
|
|
34
|
-
"""元素双击"""
|
|
35
|
-
if await locating.count() < 1:
|
|
36
|
-
raise TimeoutError('元素个数小于1,直接操作超时!')
|
|
37
|
-
await locating.dblclick()
|
|
38
|
-
|
|
39
|
-
@async_method_callback('web', '元素操作', 2, [MethodModel(f='locating')])
|
|
40
|
-
async def w_force_click(self, locating: Locator):
|
|
41
|
-
"""强制单击"""
|
|
42
|
-
await locating.evaluate('element => element.click()')
|
|
43
|
-
|
|
44
|
-
@async_method_callback('web', '元素操作', 3, [
|
|
45
|
-
MethodModel(f='locating'),
|
|
46
|
-
MethodModel(n='输入内容', f='input_value', p='请输入输入内容', d=True)])
|
|
47
|
-
async def w_input(self, locating: Locator, input_value: str):
|
|
48
|
-
"""元素输入"""
|
|
49
|
-
if await locating.count() < 1:
|
|
50
|
-
raise TimeoutError('元素个数小于1,直接操作超时!')
|
|
51
|
-
await locating.fill(str(input_value))
|
|
52
|
-
|
|
53
|
-
@async_method_callback('web', '元素操作', 4, [MethodModel(f='locating')])
|
|
54
|
-
async def w_hover(self, locating: Locator):
|
|
55
|
-
"""鼠标悬停"""
|
|
56
|
-
await locating.hover()
|
|
57
|
-
await asyncio.sleep(1)
|
|
58
|
-
|
|
59
|
-
@async_method_callback('web', '元素操作', 5, [
|
|
60
|
-
MethodModel(f='locating'),
|
|
61
|
-
MethodModel(n='缓存的key', f='set_cache_key', p='请输入获取元素文本后存储的key', d=True)])
|
|
62
|
-
async def w_get_text(self, locating: Locator, set_cache_key=None):
|
|
63
|
-
"""获取元素文本"""
|
|
64
|
-
if await locating.count() < 1:
|
|
65
|
-
raise TimeoutError('元素个数小于1,直接操作超时!')
|
|
66
|
-
methods = [
|
|
67
|
-
("inner_text", locating.inner_text),
|
|
68
|
-
("text_content", locating.text_content),
|
|
69
|
-
("input_value", locating.input_value),
|
|
70
|
-
("get_attribute", lambda: locating.get_attribute("value")),
|
|
71
|
-
("evaluate", lambda: locating.evaluate("el => el.value")),
|
|
72
|
-
]
|
|
73
|
-
for method_name, method in methods:
|
|
74
|
-
try:
|
|
75
|
-
value = await method()
|
|
76
|
-
if value is not None and str(value).strip() and set_cache_key:
|
|
77
|
-
self.base_data.test_data.set_cache(key=set_cache_key, value=value)
|
|
78
|
-
return value
|
|
79
|
-
elif value is not None and str(value).strip():
|
|
80
|
-
return value
|
|
81
|
-
except Exception:
|
|
82
|
-
continue
|
|
83
|
-
return None
|
|
84
|
-
|
|
85
|
-
@async_method_callback('web', '元素操作', 5, [
|
|
86
|
-
MethodModel(f='locating'),
|
|
87
|
-
MethodModel(n='输入文本', f='input_value', p='请输入输入内容', d=True)])
|
|
88
|
-
async def w_clear_input(self, locating: Locator, input_value: str):
|
|
89
|
-
"""元素清空再输入"""
|
|
90
|
-
if await locating.count() < 1:
|
|
91
|
-
raise TimeoutError('元素个数小于1,直接操作超时!')
|
|
92
|
-
await locating.clear()
|
|
93
|
-
await locating.fill(str(input_value))
|
|
94
|
-
|
|
95
|
-
@async_method_callback('web', '元素操作', 6, [MethodModel(f='locating')])
|
|
96
|
-
async def w_many_click(self, locating: Locator):
|
|
97
|
-
"""多元素循环单击"""
|
|
98
|
-
await asyncio.sleep(1)
|
|
99
|
-
elements = await locating.all()
|
|
100
|
-
for element in elements:
|
|
101
|
-
await element.click()
|
|
102
|
-
await asyncio.sleep(0.2)
|
|
103
|
-
|
|
104
|
-
@async_method_callback('web', '元素操作', 6, [
|
|
105
|
-
MethodModel(f='locating'),
|
|
106
|
-
MethodModel(n='文件名称', f='file_path', p='请输入文件路径,参照帮助文档', d=True)])
|
|
107
|
-
async def w_upload_files(self, locating: Locator, file_path: str | list):
|
|
108
|
-
"""拖拽文件上传"""
|
|
109
|
-
try:
|
|
110
|
-
if isinstance(file_path, str):
|
|
111
|
-
await locating.set_input_files(file_path, timeout=30000)
|
|
112
|
-
else:
|
|
113
|
-
for file in file_path:
|
|
114
|
-
await locating.set_input_files(file, timeout=30000)
|
|
115
|
-
except Error:
|
|
116
|
-
raise MangoAutomationError(*ERROR_MSG_0024)
|
|
117
|
-
|
|
118
|
-
@async_method_callback('web', '元素操作', 7, [
|
|
119
|
-
MethodModel(f='locating'),
|
|
120
|
-
MethodModel(n='文件名称', f='file_path', p='请输入文件路径,参照帮助文档', d=True)])
|
|
121
|
-
async def w_click_upload_files(self, locating: Locator, file_path: str | list):
|
|
122
|
-
"""点击并选择文件上传"""
|
|
123
|
-
async with self.base_data.page.expect_file_chooser(timeout=30000) as fc_info:
|
|
124
|
-
await locating.click()
|
|
125
|
-
file_chooser = await fc_info.value
|
|
126
|
-
await file_chooser.set_files(file_path)
|
|
127
|
-
|
|
128
|
-
@async_method_callback('web', '元素操作', 8, [
|
|
129
|
-
MethodModel(f='locating'),
|
|
130
|
-
MethodModel(n='存储key值', f='file_key', p='请输入文件存储路径的key,后续通过key获取文件保存的绝对路径', d=True)])
|
|
131
|
-
async def w_download(self, locating: Locator, file_key: str):
|
|
132
|
-
"""下载文件"""
|
|
133
|
-
async with self.base_data.page.expect_download(timeout=30000) as download_info:
|
|
134
|
-
await locating.click()
|
|
135
|
-
download = await download_info.value
|
|
136
|
-
file_name = download.suggested_filename
|
|
137
|
-
save_path = os.path.join(self.base_data.download_path, file_name)
|
|
138
|
-
await download.save_as(save_path)
|
|
139
|
-
self.base_data.test_data.set_cache(file_key, file_name)
|
|
140
|
-
|
|
141
|
-
@async_method_callback('web', '元素操作', 9, [
|
|
142
|
-
MethodModel(f='locating')])
|
|
143
|
-
async def w_element_wheel(self, locating: Locator):
|
|
144
|
-
"""滚动到元素位置"""
|
|
145
|
-
await locating.scroll_into_view_if_needed()
|
|
146
|
-
|
|
147
|
-
@async_method_callback('web', '元素操作', 9, [MethodModel(f='locating')])
|
|
148
|
-
async def w_right_click(self, locating: Locator):
|
|
149
|
-
"""元素右键点击"""
|
|
150
|
-
await locating.click(button='right')
|
|
151
|
-
|
|
152
|
-
@async_method_callback('web', '元素操作', 10, [
|
|
153
|
-
MethodModel(f='locating'),
|
|
154
|
-
MethodModel(n='点击时间', f='n', p='请输入循环点击的时间', d=True)])
|
|
155
|
-
async def w_time_click(self, locating: Locator, n: int):
|
|
156
|
-
"""循环点击N秒"""
|
|
157
|
-
try:
|
|
158
|
-
n = int(n)
|
|
159
|
-
except ValueError:
|
|
160
|
-
raise MangoAutomationError(*ERROR_MSG_0056)
|
|
161
|
-
s = time.time()
|
|
162
|
-
while True:
|
|
163
|
-
await locating.click()
|
|
164
|
-
if time.time() - s > n:
|
|
165
|
-
return
|
|
166
|
-
|
|
167
|
-
@async_method_callback('web', '元素操作', 11, [
|
|
168
|
-
MethodModel(f='locating'),
|
|
169
|
-
MethodModel(n='像素大小', f='n', p='请输入向上像素', d=True)])
|
|
170
|
-
async def w_drag_up_pixel(self, locating: Locator, n: int):
|
|
171
|
-
"""往上拖动N个像素"""
|
|
172
|
-
try:
|
|
173
|
-
n = int(n)
|
|
174
|
-
except ValueError:
|
|
175
|
-
raise MangoAutomationError(*ERROR_MSG_0056)
|
|
176
|
-
|
|
177
|
-
box = await locating.bounding_box()
|
|
178
|
-
|
|
179
|
-
if box:
|
|
180
|
-
await self.base_data.page.mouse.move(box['x'] + box['width'] / 2, box['y'] + box['height'] / 2)
|
|
181
|
-
await self.base_data.page.mouse.down()
|
|
182
|
-
await self.base_data.page.mouse.move(box['x'] + box['width'] / 2, box['y'] + box['height'] / 2 - n)
|
|
183
|
-
await self.base_data.page.mouse.up()
|
|
184
|
-
|
|
185
|
-
@async_method_callback('web', '元素操作', 12, [
|
|
186
|
-
MethodModel(f='locating'),
|
|
187
|
-
MethodModel(n='像素大小', f='n', p='请输入向下像素', d=True)])
|
|
188
|
-
async def w_drag_down_pixel(self, locating: Locator, n: int):
|
|
189
|
-
"""往下拖动N个像素"""
|
|
190
|
-
try:
|
|
191
|
-
n = int(n)
|
|
192
|
-
except ValueError:
|
|
193
|
-
raise MangoAutomationError(*ERROR_MSG_0056)
|
|
194
|
-
|
|
195
|
-
box = await locating.bounding_box()
|
|
196
|
-
|
|
197
|
-
if box:
|
|
198
|
-
await self.base_data.page.mouse.move(box['x'] + box['width'] / 2, box['y'] + box['height'] / 2)
|
|
199
|
-
await self.base_data.page.mouse.down()
|
|
200
|
-
await self.base_data.page.mouse.move(box['x'] + box['width'] / 2, box['y'] + box['height'] / 2 + n)
|
|
201
|
-
await self.base_data.page.mouse.up()
|
|
202
|
-
|
|
203
|
-
@async_method_callback('web', '元素操作', 13, [
|
|
204
|
-
MethodModel(f='locating'),
|
|
205
|
-
MethodModel(n='像素大小', f='n', p='请输入向左像素', d=True)])
|
|
206
|
-
async def w_drag_left_pixel(self, locating: Locator, n: int):
|
|
207
|
-
"""往左拖动N个像素"""
|
|
208
|
-
try:
|
|
209
|
-
n = int(n)
|
|
210
|
-
except ValueError:
|
|
211
|
-
raise MangoAutomationError(*ERROR_MSG_0056)
|
|
212
|
-
|
|
213
|
-
box = await locating.bounding_box()
|
|
214
|
-
|
|
215
|
-
if box:
|
|
216
|
-
await self.base_data.page.mouse.move(box['x'] + box['width'] / 2, box['y'] + box['height'] / 2)
|
|
217
|
-
await self.base_data.page.mouse.down()
|
|
218
|
-
await self.base_data.page.mouse.move(box['x'] + box['width'] / 2 - n, box['y'] + box['height'] / 2)
|
|
219
|
-
await self.base_data.page.mouse.up()
|
|
220
|
-
|
|
221
|
-
@async_method_callback('web', '元素操作', 14, [
|
|
222
|
-
MethodModel(f='locating'),
|
|
223
|
-
MethodModel(n='像素大小', f='n', p='请输入向右像素', d=True)])
|
|
224
|
-
async def w_drag_right_pixel(self, locating: Locator, n: int):
|
|
225
|
-
"""往右拖动N个像素"""
|
|
226
|
-
try:
|
|
227
|
-
n = int(n)
|
|
228
|
-
except ValueError:
|
|
229
|
-
raise MangoAutomationError(*ERROR_MSG_0056)
|
|
230
|
-
box = await locating.bounding_box()
|
|
231
|
-
|
|
232
|
-
if box:
|
|
233
|
-
await self.base_data.page.mouse.move(box['x'] + box['width'] / 2, box['y'] + box['height'] / 2)
|
|
234
|
-
await self.base_data.page.mouse.down()
|
|
235
|
-
await self.base_data.page.mouse.move(box['x'] + box['width'] / 2 + n, box['y'] + box['height'] / 2)
|
|
236
|
-
await self.base_data.page.mouse.up()
|
|
237
|
-
|
|
238
|
-
@async_method_callback('web', '元素操作', 15, [
|
|
239
|
-
MethodModel(f='locating'),
|
|
240
|
-
MethodModel(n='保存路径', f='path', p='请输入截图名称', d=True)])
|
|
241
|
-
async def w_ele_screenshot(self, locating: Locator, path: str):
|
|
242
|
-
"""元素截图"""
|
|
243
|
-
await locating.screenshot(path=os.path.join(self.base_data.download_path, path))
|
|
244
|
-
|
|
245
|
-
@async_method_callback('web', '元素操作', 20, [
|
|
246
|
-
MethodModel(f='locating1'),
|
|
247
|
-
MethodModel(f='locating2')])
|
|
248
|
-
async def w_drag_to(self, locating1: Locator, locating2: Locator):
|
|
249
|
-
"""拖动A元素到达B"""
|
|
250
|
-
await locating1.drag_to(locating2)
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# @Project: 芒果测试平台
|
|
3
|
-
# @Description: # @Time : 2023-04-29 12:11
|
|
4
|
-
# @Author : 毛鹏
|
|
5
|
-
|
|
6
|
-
from mangotools.decorator import async_method_callback
|
|
7
|
-
from mangotools.models import MethodModel
|
|
8
|
-
from ....tools import Meta
|
|
9
|
-
from ....uidrives._base_data import BaseData
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class AsyncWebDeviceInput(metaclass=Meta):
|
|
13
|
-
"""输入设备"""
|
|
14
|
-
|
|
15
|
-
def __init__(self, base_data: BaseData):
|
|
16
|
-
self.base_data = base_data
|
|
17
|
-
|
|
18
|
-
@async_method_callback('web', '输入设备', 0, [
|
|
19
|
-
MethodModel(n='按键值', f='keyboard', p='请输入键盘名称,首字母大写', d=True)])
|
|
20
|
-
async def w_keys(self, keyboard: str):
|
|
21
|
-
"""模拟按下指定的键"""
|
|
22
|
-
await self.base_data.page.keyboard.press(str(keyboard))
|
|
23
|
-
|
|
24
|
-
@async_method_callback('web', '输入设备', 1, [
|
|
25
|
-
MethodModel(n='滚动像素', f='y', p='请输入向上滚动像素', d=True)])
|
|
26
|
-
async def w_wheel(self, y):
|
|
27
|
-
"""鼠标上下滚动像素,负数代表向上"""
|
|
28
|
-
await self.base_data.page.mouse.wheel(0, int(y))
|
|
29
|
-
|
|
30
|
-
@async_method_callback('web', '输入设备', 2, [
|
|
31
|
-
MethodModel(n='x坐标', f='x', p='请输入点击的x轴', d=True),
|
|
32
|
-
MethodModel(n='y坐标', f='y', p='请输入点击的y轴', d=True)])
|
|
33
|
-
async def w_mouse_click(self, x: float, y: float):
|
|
34
|
-
"""鼠标点击坐标"""
|
|
35
|
-
await self.base_data.page.mouse.click(float(x), float(y))
|
|
36
|
-
|
|
37
|
-
@async_method_callback('web', '输入设备', 3)
|
|
38
|
-
async def w_mouse_center(self):
|
|
39
|
-
"""鼠标移动到中间"""
|
|
40
|
-
|
|
41
|
-
viewport_size = await self.base_data.page.evaluate('''() => {
|
|
42
|
-
return {
|
|
43
|
-
width: window.innerWidth,
|
|
44
|
-
height: window.innerHeight
|
|
45
|
-
}
|
|
46
|
-
}''')
|
|
47
|
-
center_x = viewport_size['width'] / 2
|
|
48
|
-
center_y = viewport_size['height'] / 2
|
|
49
|
-
await self.base_data.page.mouse.move(center_x, center_y)
|
|
50
|
-
|
|
51
|
-
@async_method_callback('web', '输入设备', 4)
|
|
52
|
-
async def w_mouse_center(self):
|
|
53
|
-
"""鼠标移动到中间并点击"""
|
|
54
|
-
|
|
55
|
-
viewport_size = await self.base_data.page.evaluate('''() => {
|
|
56
|
-
return {
|
|
57
|
-
width: window.innerWidth,
|
|
58
|
-
height: window.innerHeight
|
|
59
|
-
}
|
|
60
|
-
}''')
|
|
61
|
-
center_x = viewport_size['width'] / 2
|
|
62
|
-
center_y = viewport_size['height'] / 2
|
|
63
|
-
await self.base_data.page.mouse.click(center_x, center_y)
|
|
64
|
-
|
|
65
|
-
@async_method_callback('web', '输入设备', 5, [
|
|
66
|
-
MethodModel(n='输入文本', f='text', p='请输入键盘输入的内容', d=True)])
|
|
67
|
-
async def w_keyboard_type_text(self, text: str):
|
|
68
|
-
"""模拟人工输入文字"""
|
|
69
|
-
await self.base_data.page.keyboard.type(str(text))
|
|
70
|
-
|
|
71
|
-
@async_method_callback('web', '输入设备', 6, [
|
|
72
|
-
MethodModel(n='输入文本', f='text', p='请输入键盘输入的内容', d=True)])
|
|
73
|
-
async def w_keyboard_insert_text(self, text: str):
|
|
74
|
-
"""直接输入文字"""
|
|
75
|
-
await self.base_data.page.keyboard.insert_text(str(text))
|
|
76
|
-
|
|
77
|
-
@async_method_callback('web', '输入设备', 7, [
|
|
78
|
-
MethodModel(n='删除个数', f='count', p='请输入要删除字符串的个数', d=True)])
|
|
79
|
-
async def w_keyboard_delete_text(self, count: int):
|
|
80
|
-
"""删除光标左侧的字符"""
|
|
81
|
-
for _ in range(0, int(count) + 1):
|
|
82
|
-
await self.base_data.page.keyboard.press("Backspace")
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# @Project: 芒果测试平台
|
|
3
|
-
# @Description:
|
|
4
|
-
# @Time : 2024-04-24 10:43
|
|
5
|
-
# @Author : 毛鹏
|
|
6
|
-
import asyncio
|
|
7
|
-
import ctypes
|
|
8
|
-
import os
|
|
9
|
-
import string
|
|
10
|
-
import traceback
|
|
11
|
-
from typing import Optional
|
|
12
|
-
|
|
13
|
-
from playwright._impl._errors import Error
|
|
14
|
-
from playwright.async_api import async_playwright, Page, BrowserContext, Browser, Playwright, Request, Route
|
|
15
|
-
|
|
16
|
-
from ....enums import BrowserTypeEnum
|
|
17
|
-
from ....exceptions import MangoAutomationError
|
|
18
|
-
from ....exceptions.error_msg import ERROR_MSG_0057, ERROR_MSG_0008, ERROR_MSG_0062, ERROR_MSG_0009, ERROR_MSG_0055
|
|
19
|
-
|
|
20
|
-
"""
|
|
21
|
-
python -m uiautomator2 init
|
|
22
|
-
python -m weditor
|
|
23
|
-
|
|
24
|
-
"""
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class AsyncWebNewBrowser:
|
|
28
|
-
|
|
29
|
-
def __init__(self,
|
|
30
|
-
web_type: int,
|
|
31
|
-
web_path: str | None = None,
|
|
32
|
-
web_max=False,
|
|
33
|
-
web_headers=False,
|
|
34
|
-
web_recording=False,
|
|
35
|
-
web_h5=None,
|
|
36
|
-
is_header_intercept=False,
|
|
37
|
-
web_is_default=False,
|
|
38
|
-
videos_path=None,
|
|
39
|
-
log=None,
|
|
40
|
-
):
|
|
41
|
-
self.lock = asyncio.Lock()
|
|
42
|
-
self.web_type = web_type
|
|
43
|
-
self.web_path = web_path
|
|
44
|
-
self.web_max = web_max
|
|
45
|
-
self.web_headers = web_headers
|
|
46
|
-
self.web_recording = web_recording
|
|
47
|
-
self.web_h5 = web_h5
|
|
48
|
-
self.web_is_default = web_is_default
|
|
49
|
-
self.is_header_intercept = is_header_intercept
|
|
50
|
-
self.videos_path = videos_path
|
|
51
|
-
self.log = log
|
|
52
|
-
self.browser_path = ['chrome.exe', 'msedge.exe', 'firefox.exe', '苹果', '360se.exe']
|
|
53
|
-
self.browser: Optional[None | Browser] = None
|
|
54
|
-
self.playwright: Optional[None | Playwright] = None
|
|
55
|
-
|
|
56
|
-
async def new_web_page(self, count=0) -> tuple[BrowserContext, Page]:
|
|
57
|
-
if self.browser is None:
|
|
58
|
-
async with self.lock:
|
|
59
|
-
if self.browser is None:
|
|
60
|
-
self.browser = await self.new_browser()
|
|
61
|
-
await asyncio.sleep(1)
|
|
62
|
-
try:
|
|
63
|
-
context = await self.new_context()
|
|
64
|
-
page = await self.new_page(context)
|
|
65
|
-
return context, page
|
|
66
|
-
except Exception:
|
|
67
|
-
self.log.error(f'初始化page失败,错误信息:{traceback.format_exc()}')
|
|
68
|
-
self.browser = None
|
|
69
|
-
if count >= 3:
|
|
70
|
-
raise MangoAutomationError(*ERROR_MSG_0057)
|
|
71
|
-
else:
|
|
72
|
-
return await self.new_web_page(count=count + 1)
|
|
73
|
-
|
|
74
|
-
async def new_browser(self) -> Browser:
|
|
75
|
-
self.playwright = await async_playwright().start()
|
|
76
|
-
if self.web_type == BrowserTypeEnum.CHROMIUM.value or self.web_type == BrowserTypeEnum.EDGE.value:
|
|
77
|
-
browser = self.playwright.chromium
|
|
78
|
-
elif self.web_type == BrowserTypeEnum.FIREFOX.value:
|
|
79
|
-
browser = self.playwright.firefox
|
|
80
|
-
elif self.web_type == BrowserTypeEnum.WEBKIT.value:
|
|
81
|
-
browser = self.playwright.webkit
|
|
82
|
-
else:
|
|
83
|
-
raise MangoAutomationError(*ERROR_MSG_0008)
|
|
84
|
-
if self.web_is_default:
|
|
85
|
-
try:
|
|
86
|
-
return await browser.launch()
|
|
87
|
-
except Error as error:
|
|
88
|
-
self.log.error(f'初始化浏览器失败-1,类型:{error},详情:{traceback.format_exc()}')
|
|
89
|
-
raise MangoAutomationError(*ERROR_MSG_0062)
|
|
90
|
-
else:
|
|
91
|
-
try:
|
|
92
|
-
if self.web_max:
|
|
93
|
-
return await browser.launch(
|
|
94
|
-
headless=self.web_headers,
|
|
95
|
-
executable_path=self.web_path if self.web_path else self.__search_path(),
|
|
96
|
-
args=['--start-maximized']
|
|
97
|
-
)
|
|
98
|
-
else:
|
|
99
|
-
return await browser.launch(
|
|
100
|
-
headless=self.web_headers,
|
|
101
|
-
executable_path=self.web_path if self.web_path else self.__search_path()
|
|
102
|
-
)
|
|
103
|
-
except Error as error:
|
|
104
|
-
self.log.error(f'初始化浏览器失败-2,类型:{error},详情:{traceback.format_exc()}')
|
|
105
|
-
raise MangoAutomationError(*ERROR_MSG_0009, value=(self.web_path,))
|
|
106
|
-
|
|
107
|
-
async def new_context(self) -> BrowserContext:
|
|
108
|
-
args_dict = {'ignore_https_errors': True}
|
|
109
|
-
if self.web_is_default:
|
|
110
|
-
args_dict["viewport"] = {"width": 1920, "height": 1080}
|
|
111
|
-
if self.web_h5:
|
|
112
|
-
args_dict.update(self.playwright.devices[self.web_h5])
|
|
113
|
-
if not (self.web_is_default or self.web_h5):
|
|
114
|
-
args_dict["no_viewport"] = True
|
|
115
|
-
if self.web_recording and self.videos_path:
|
|
116
|
-
args_dict["record_video_dir"] = self.videos_path
|
|
117
|
-
context = await self.browser.new_context(**args_dict)
|
|
118
|
-
context.set_default_timeout(3000)
|
|
119
|
-
return context
|
|
120
|
-
|
|
121
|
-
async def new_page(self, context: BrowserContext) -> Page:
|
|
122
|
-
try:
|
|
123
|
-
page = await context.new_page()
|
|
124
|
-
page.set_default_timeout(3000)
|
|
125
|
-
if self.is_header_intercept:
|
|
126
|
-
await page.route("**/*", self.wen_intercept_request) # 应用拦截函数到页面的所有请求
|
|
127
|
-
return page
|
|
128
|
-
except Error as error:
|
|
129
|
-
self.log.error(f'初始化page失败,类型:{error},详情:{traceback.format_exc()}')
|
|
130
|
-
raise MangoAutomationError(*ERROR_MSG_0009, value=(self.web_path,))
|
|
131
|
-
|
|
132
|
-
async def close(self):
|
|
133
|
-
if self.browser:
|
|
134
|
-
await self.browser.close()
|
|
135
|
-
|
|
136
|
-
def __search_path(self, ):
|
|
137
|
-
drives = []
|
|
138
|
-
for letter in string.ascii_uppercase:
|
|
139
|
-
drive = f"{letter}:\\"
|
|
140
|
-
if ctypes.windll.kernel32.GetDriveTypeW(drive) == 3:
|
|
141
|
-
drives.append(drive)
|
|
142
|
-
for i in drives:
|
|
143
|
-
for root, dirs, files in os.walk(i):
|
|
144
|
-
if self.browser_path[self.web_type] in files:
|
|
145
|
-
return os.path.join(root, self.browser_path[self.web_type])
|
|
146
|
-
|
|
147
|
-
raise MangoAutomationError(*ERROR_MSG_0055)
|
|
148
|
-
|
|
149
|
-
async def wen_intercept_request(self, route: Route, request: Request):
|
|
150
|
-
pass
|
|
151
|
-
|
|
152
|
-
async def wen_recording_api(self, request: Request, project_product: int):
|
|
153
|
-
pass
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# @Project: 芒果测试平台
|
|
3
|
-
# @Description: # @Time : 2023-04-25 22:33
|
|
4
|
-
# @Author : 毛鹏
|
|
5
|
-
import asyncio
|
|
6
|
-
|
|
7
|
-
from playwright.async_api import Locator
|
|
8
|
-
|
|
9
|
-
from mangotools.decorator import async_method_callback
|
|
10
|
-
from mangotools.models import MethodModel
|
|
11
|
-
from ....tools import Meta
|
|
12
|
-
from ....uidrives._base_data import BaseData
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
class AsyncWebPage(metaclass=Meta):
|
|
16
|
-
"""页面操作"""
|
|
17
|
-
|
|
18
|
-
def __init__(self, base_data: BaseData):
|
|
19
|
-
self.base_data = base_data
|
|
20
|
-
|
|
21
|
-
@async_method_callback('web', '页面操作', 0, [
|
|
22
|
-
MethodModel(n='页签下标', f='individual', p='请输入页签下标,从1开始数', d=True)])
|
|
23
|
-
async def w_switch_tabs(self, individual: int):
|
|
24
|
-
"""切换页签"""
|
|
25
|
-
pages = self.base_data.context.pages
|
|
26
|
-
await pages[int(individual) + 1].bring_to_front()
|
|
27
|
-
self.base_data.page = pages[int(individual) + 1]
|
|
28
|
-
await asyncio.sleep(1)
|
|
29
|
-
|
|
30
|
-
@async_method_callback('web', '页面操作', 1)
|
|
31
|
-
async def w_close_current_tab(self):
|
|
32
|
-
"""关闭当前页签"""
|
|
33
|
-
await asyncio.sleep(2)
|
|
34
|
-
pages = self.base_data.context.pages
|
|
35
|
-
await pages[-1].close()
|
|
36
|
-
self.base_data.page = pages[0]
|
|
37
|
-
|
|
38
|
-
@async_method_callback('web', '页面操作', 2, [MethodModel(f='locating')])
|
|
39
|
-
async def w_open_new_tab_and_switch(self, locating: Locator):
|
|
40
|
-
"""点击并打开新页签"""
|
|
41
|
-
|
|
42
|
-
await locating.click()
|
|
43
|
-
await asyncio.sleep(2)
|
|
44
|
-
pages = self.base_data.context.pages
|
|
45
|
-
new_page = pages[-1]
|
|
46
|
-
await new_page.bring_to_front()
|
|
47
|
-
self.base_data.page = new_page
|
|
48
|
-
await asyncio.sleep(1)
|
|
49
|
-
|
|
50
|
-
@async_method_callback('web', '页面操作', 3)
|
|
51
|
-
async def w_refresh(self):
|
|
52
|
-
"""刷新页面"""
|
|
53
|
-
await self.base_data.page.reload()
|
|
54
|
-
|
|
55
|
-
@async_method_callback('web', '页面操作', 4)
|
|
56
|
-
async def w_go_back(self):
|
|
57
|
-
"""返回上一页"""
|
|
58
|
-
await self.base_data.page.go_back()
|
|
59
|
-
|
|
60
|
-
@async_method_callback('web', '页面操作', 5)
|
|
61
|
-
async def w_go_forward(self):
|
|
62
|
-
"""前进到下一页"""
|
|
63
|
-
await self.base_data.page.go_forward()
|