Skip to content

Commit

Permalink
修复部分问题
Browse files Browse the repository at this point in the history
  • Loading branch information
5656565566 committed Feb 6, 2025
1 parent 8a5a421 commit 19b515b
Show file tree
Hide file tree
Showing 15 changed files with 664 additions and 423 deletions.
28 changes: 16 additions & 12 deletions actuator/devices/adb/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from base import Devices
from config import get_config, PATH_WORKING
from model import Tip, Image

class AdbDevice(Devices):
def __init__(self, name: str, device: _AdbDevice) -> None:
Expand All @@ -26,7 +27,7 @@ def click(self, x: int, y: int) -> int:

self.device.click(x, y)

return f"{self.device} 点击位置 {x} {y}"
return Tip(f"{self.device} 点击位置 {x} {y}")

def swipe(self, x1: int, y1: int, x2: int, y2: int, time: float) -> int:

Expand All @@ -36,46 +37,49 @@ def swipe(self, x1: int, y1: int, x2: int, y2: int, time: float) -> int:
y2 = y2 + (self._offset(5) if self.random else 0)
self.device.swipe(x1, y1, x2, y2, time)

return f"{self.device} 滑动 {x1} {y1} => {x2} {y2} 耗时 {time}"
return Tip(f"{self.device} 滑动 {x1} {y1} => {x2} {y2} 耗时 {time}")

def keyevent(self, key_id: int) -> int:
self.device.keyevent(key_id)
return f"{self.device} 单击按键 {key_id}"
return Tip(f"{self.device} 单击按键 {key_id}")

def openApp(self, name: str, appActivity: str) -> int:

self.device.shell(f'am start {appActivity}', timeout=2)

return f"{self.device} 打开了 APP {name}"
return Tip(f"{self.device} 打开了 APP {name}")

def textInput(self, text: Union[str, list]):

if isinstance(text, list):
text = "".join(text)

self.device.shell(f'input text "{text}"', timeout=2)
return f"{self.device} 输入文本 {text}"
return Tip(f"{self.device} 输入文本 {text}")

def shell(self, cmd: str):
"""终端执行命令"""

return f"{self.device} 执行 命令 {cmd}", self.device.shell(cmd, timeout=2)
return Tip(f"{self.device} 执行 命令 {cmd}", self.device.shell(cmd, timeout=2))


def appName(self):
"""前台APP名"""

res = self.device.shell("dumpsys activity activities | grep \"mResumedActivity\"", timeout=2)

return f"{self.device} 前台 APP {res}", res
return Tip(f"{self.device} 前台 APP {res}", res)

def get_screenshot(self) -> bytes:
return self.screenshot_file.getvalue()
def get_screenshot(self) -> Image:
return Image(self.screenshot_file.getvalue())

def screenshot(self, filePath: Path= None):

save_object = None

self.screenshot_file.seek(0)
self.screenshot_file.truncate(0)

if get_config().save_screenshot:
save_object = PATH_WORKING / f"{self.name}.png"

Expand All @@ -87,9 +91,9 @@ def screenshot(self, filePath: Path= None):
if save_object:
with open(save_object, 'wb') as file:
file.write(self.screenshot_file.getvalue())
return f"对 {self.device.serial} 的截图并保存到了 {save_object}", self.screenshot_file.getvalue()
return Tip(f"对 {self.device.serial} 的截图并保存到了 {save_object}"), self.get_screenshot()

return f"对 {self.device.serial} 进行截图并保存到内存", self.screenshot_file.getvalue()
return Tip(f"对 {self.device.serial} 进行截图并保存到内存"), self.get_screenshot()

except:
return f"无法为 {self.device.serial} 截图 请检查设备状态", None
return Tip(f"无法为 {self.device.serial} 截图 请检查设备状态")
43 changes: 26 additions & 17 deletions actuator/devices/web/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from log import logger
from config import get_config, PATH_WORKING
from model import Tip, Image

import os

Expand Down Expand Up @@ -98,7 +99,7 @@ def _init_browser(self, driver_path: Path) -> WebDriver:

def _init_test(self):
if not self.browser:
return "请先打开一个页面 !"
return Tip("请先打开一个页面 !")

def open_url(self, url: str, name: str = None):

Expand All @@ -108,8 +109,12 @@ def open_url(self, url: str, name: str = None):
if len(self.pages) == 0:
self._init_browser(self.driver_path)
self.browser.get(url)

self.pages[name] = url
self.pages[name] = url
return Tip(f"打开浏览器并访问 {url}")

else:
self.pages[name] = url
return Tip(f"新增标签页 {url}")

def close_url(self, name: str):
# 如果是最后一个网页关闭浏览器
Expand All @@ -123,16 +128,16 @@ def clickByText(self, text: str):
logger.debug(f"{self.page} 查找并点击显示文本为 '{text}' 的元素")

if not switch_to_frame_with_element(self.browser, By.LINK_TEXT, text):
return f"无法通过 {text} 找到元素"
return Tip(f"无法通过 {text} 找到元素")

try:
# 通过文本定位链接或按钮元素
element = self.browser.find_element(By.LINK_TEXT, text)
element.click()
return f"点击成功: {text}"
return Tip(f"点击成功: {text}")

except Exception as e:
return f"无法通过文本 '{text}' 定位并点击元素: {e}"
return Tip(f"无法通过文本 '{text}' 定位并点击元素: {e}")


def clickByAny(self, by: ByType, any: str):
Expand All @@ -143,14 +148,14 @@ def clickByAny(self, by: ByType, any: str):
logger.debug(f"{self.page} 使用依据 {by} 查找并点击元素 {any}")

if not switch_to_frame_with_element(self.browser, by, any):
return f"无法通过 {any} 找到元素"
return Tip(f"无法通过 {any} 找到元素")

try:
element = self.browser.find_element(by, any)
element.click()
return f"点击成功: {any}"
return Tip(f"点击成功: {any}")
except Exception as e:
return f"无法通过 '{any}' 定位并点击元素: {e}"
return Tip(f"无法通过 '{any}' 定位并点击元素: {e}")


def click(self, x: int, y: int):
Expand All @@ -163,7 +168,7 @@ def click(self, x: int, y: int):
self.actions.move_by_offset(x, y).click().perform()
# 重置鼠标位置,以免后续操作受影响
self.actions.move_by_offset(-x, -y).perform()
return f"{self.page} 点击位置 {x}, {y}"
return Tip(f"{self.page} 点击位置 {x}, {y}")

def swipe(self, x1: int, y1: int, x2: int, y2: int, time: float) -> int:
"""模拟滑动"""
Expand All @@ -176,7 +181,7 @@ def swipe(self, x1: int, y1: int, x2: int, y2: int, time: float) -> int:
self.actions.move_by_offset(x1, y1).click_and_hold().move_by_offset(x2 - x1, y2 - y1).release().perform()
self.actions.move_by_offset(-x2, -y2).perform()

return f"{self.page} 滑动 {x1}, {y1} => {x2}, {y2} 耗时 {time}"
return Tip(f"{self.page} 滑动 {x1}, {y1} => {x2}, {y2} 耗时 {time}")

def textInput(self, text: Union[str, list]):
"""输入文本,支持中文及其他字符"""
Expand All @@ -191,7 +196,7 @@ def textInput(self, text: Union[str, list]):
logger.debug(f"{self.page} 输入文本 {t}")
self.actions.send_keys(t).perform()

return f"{self.page} 输入文本 {text}"
return Tip(f"{self.page} 输入文本 {text}")

def shell(self, js: str):
"""执行 JS 脚本"""
Expand All @@ -200,9 +205,12 @@ def shell(self, js: str):

logger.debug(f"{self.page} 执行命令")
result = self.browser.execute_script(js)
return f"{self.page} 执行命令", result
return Tip(f"{self.page} 执行命令"), result

def get_screenshot(self) -> Image:
return Image(self.screenshot_file)

def screenshot(self, filePath: Path= None) -> Path:
def screenshot(self, filePath: Path= None):
"""保存屏幕截图"""
save_object = None

Expand All @@ -218,15 +226,16 @@ def screenshot(self, filePath: Path= None) -> Path:
if save_object:
with open(save_object, 'wb') as file:
file.write(self.screenshot_file)
return f"对 {self.browser.title} 的截图并保存到了 {save_object}", self.screenshot_file
return Tip(f"对 {self.browser.title} 的截图并保存到了 {save_object}"), self.get_screenshot()

return f"对 {self.browser.title} 进行截图并保存到内存", self.screenshot_file
return Tip(f"对 {self.browser.title} 进行截图并保存到内存"), self.get_screenshot()

except Exception as e:
logger.warning(f"无法截图,请检查设备状态。错误信息: {e}")
return f"无法截图,请检查设备状态。错误信息: {e}"
return Tip(f"无法截图,请检查设备状态。错误信息: {e}")

def start_browser(self):
"""启动浏览器"""
self._init_browser()

def kill_browser(self):
Expand Down
55 changes: 30 additions & 25 deletions actuator/devices/windows/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from base import Devices
from config import get_config, PATH_WORKING
from log import logger
from model import Tip, Image

import psutil
import subprocess
Expand Down Expand Up @@ -157,19 +158,19 @@ def gameClick(self, x: int, y: int, key: str = None) -> int:
y = y + (self._offset(5) if self.random else 0)

pydirectinput.click(x, y, button=key)
return f"Windows 点击位置 {x} {y}"
return Tip(f"Windows 点击位置 {x} {y}")

def mouseClick(self, key: str) -> int:
pyautogui.click(button=key)
return f"点击鼠标按钮: {key}"
return Tip(f"点击鼠标按钮: {key}")

def gameMouseClick(self, key: str) -> int:
pydirectinput.click(button=key)
return f"点击鼠标按钮: {key}"
return Tip(f"点击鼠标按钮: {key}")

def getMouse(self) -> int:
x, y = pyautogui.position()
return f"当前鼠标位置: ({x}, {y})", x, y
return Tip(f"当前鼠标位置: ({x}, {y})"), x, y

def doubleClick(self, key: str = None) -> int:

Expand All @@ -180,7 +181,7 @@ def doubleClick(self, key: str = None) -> int:
y = y + (self._offset(5) if self.random else 0)

pyautogui.doubleClick(x, y, button=key)
return f"Windows 双击位置 {x} {y}"
return Tip(f"Windows 双击位置 {x} {y}")

def gameDoubleClick(self, key: str = None) -> int:

Expand All @@ -191,7 +192,7 @@ def gameDoubleClick(self, key: str = None) -> int:
y = y + (self._offset(5) if self.random else 0)

pydirectinput.doubleClick(x, y, button=key)
return f"Windows 双击位置 {x} {y}"
return Tip(f"Windows 双击位置 {x} {y}")

def click(self, x: int, y: int, key: str = None) -> int:

Expand All @@ -202,7 +203,7 @@ def click(self, x: int, y: int, key: str = None) -> int:
y = y + (self._offset(5) if self.random else 0)

pyautogui.click(x, y, button=key)
return f"Windows 点击位置 {x} {y}"
return Tip(f"Windows 点击位置 {x} {y}")

def textInput(self, text: Union[str, list]):

Expand All @@ -216,7 +217,7 @@ def textInput(self, text: Union[str, list]):
if isinstance(text, list):
text = "".join(text)

return f"windows 输入文本 {text}"
return Tip(f"windows 输入文本 {text}")

def gameSwipe(self, x1: int, y1: int, x2: int, y2: int, time: float) -> int:
if isinstance(x1, list):
Expand All @@ -233,7 +234,7 @@ def gameSwipe(self, x1: int, y1: int, x2: int, y2: int, time: float) -> int:
pydirectinput.moveTo(x2, y1, duration=time / 1000)
pydirectinput.mouseUp(button="left")

return f"Windows 鼠标滑动 {x1} {y1} => {x2} {y2} 耗时 {time}"
return Tip(f"Windows 鼠标滑动 {x1} {y1} => {x2} {y2} 耗时 {time}")

def swipe(self, x1: int, y1: int, x2: int, y2: int, time: float) -> int:

Expand All @@ -251,36 +252,36 @@ def swipe(self, x1: int, y1: int, x2: int, y2: int, time: float) -> int:
pyautogui.moveTo(x2, y1, duration=time / 1000)
pyautogui.mouseUp(button="left")

return f"Windows 鼠标滑动 {x1} {y1} => {x2} {y2} 耗时 {time}"
return Tip(f"Windows 鼠标滑动 {x1} {y1} => {x2} {y2} 耗时 {time}")

def gameKeyevent(self, key_id: str, time: int = 0) -> int:
if time == 0:
pydirectinput.press(key_id)
return f"Windows 单击按键 {key_id}"
return Tip(f"Windows 单击按键 {key_id}")

else:
pydirectinput.keyDown(key_id)
sleep(time)
pydirectinput.keyUp(key_id)
return f"Windows 按住按键 {key_id} 耗时 {time}"
return Tip(f"Windows 按住按键 {key_id} 耗时 {time}")



def keyevent(self, key_id: str, time: int = 0) -> int:
if time == 0:
pyautogui.press(key_id)
return f"Windows 单击按键 {key_id}"
return Tip(f"Windows 单击按键 {key_id}")

else:
pyautogui.keyDown(key_id)
sleep(time)
pyautogui.keyUp(key_id)
return f"Windows 按住按键 {key_id} 耗时 {time}"
return Tip(f"Windows 按住按键 {key_id} 耗时 {time}")


def hotkey(self, *hotkey):
pyautogui.hotkey(hotkey)
return f"Windows 按组合键 {hotkey}"
return Tip(f"Windows 按组合键 {hotkey}")

def shell(self, cmd: str):
try:
Expand All @@ -292,39 +293,43 @@ def shell(self, cmd: str):
def activateWindow(self, name: str):

if not self.applications.get(name):
return f"Windows 未找到窗口 {name}"
return Tip(f"Windows 未找到窗口 {name}")

application = self.applications.get(name)

window = gw.getWindowsWithTitle(application.title())[0]

window.activate()

return f"Windows 切换到窗口 {name}"
return Tip(f"Windows 切换到窗口 {name}")

def get_all_windows_titles(self):
return gw.getAllTitles()

def get_screenshot(self) -> bytes:
return self.screenshot_file.getvalue()
def get_screenshot(self) -> Image:
return Image(self.screenshot_file.getvalue())

def screenshot(self, filePath: Path= None) -> Path:
def screenshot(self, filePath: Path= None):
save_object = None

self.screenshot_file.seek(0)
self.screenshot_file.truncate(0)

if get_config().save_screenshot:
save_object = PATH_WORKING / "windows.png"

if filePath:
save_object = filePath

try:
screenshot = pyautogui.screenshot(self.screenshot_file)
screenshot = pyautogui.screenshot()
screenshot.save(self.screenshot_file, format= "PNG")

if save_object:
screenshot.save(save_object, "png")
return f"对 Windows 截图并保存到 {save_object}", self.screenshot_file.getvalue()
screenshot.save(save_object, format=screenshot.format)
return Tip(f"对 Windows 截图并保存到 {save_object}"), self.get_screenshot()

return f"对 Windows 的截图并保存到了 {save_object}", self.screenshot_file.getvalue()
return Tip(f"对 Windows 的截图并保存到了内存"), self.get_screenshot()

except Exception as e:
return f"无法为 Windows 设备 截图 请检查设备状态 {e}", False
return Tip(f"无法为 Windows 设备 截图 请检查设备状态 {e}")
Loading

0 comments on commit 19b515b

Please sign in to comment.