def select(page_name): region = ReadConfig().get_region platform = ReadConfig().get_platform filename = None if platform == "android": if region == "cn": filename = mapping.get(page_name) elif platform == "ios": pass if filename is None: raise TypeError("cannot find the mapping relationship for " + page_name) return ReadYaml().read(os.path.join(root_dir, "element", filename))
def local_run(): env = ReadConfig().get_env # args = [root_dir + "/case/", "-m S", "--alluredir", project_dir + "/report/raw_data/"] # 单独执行一个级别用例 args = [root_dir + "/case/", "--alluredir", project_dir + "/report/raw_data/"] # args = [root_path + "/case/", "-m", env, "--alluredir", project_dir + "/report/raw_data/"] # args = [root_dir + "/case/", "-m S", "--reruns", "1", "--reruns-delay", "5", "--alluredir", project_dir + "/report/raw_data/"] # args = [root_dir + "/case/", "--reruns", "1", "--reruns-delay", "5", "--alluredir", project_dir + "/report/raw_data/"] pytest.main(args)
def clear_all_message(driver): driver.open_notification() if ReadConfig().get_platform == "android": element = driver( resourceId="com.android.systemui:id/shelf_dismiss_text") if element.wait(timeout=3.0): element.click() else: driver.press("back")
def get_installed_package_name() -> str: package_name = None for package in ReadConfig().get_package_name: package_info = os.popen("adb shell \"pm list packages |grep " + package + "\"").read() if package_info is not None: package_name = package break if package_name is None: raise RuntimeError("can not find package: {}".format(package_name)) return package_name
def driver(): if platform == "android": driver = u2.connect() # 连接设备(usb只能插入单个设备) driver.set_fastinput_ime(True) # 启用自动化定制的输入法 driver.settings["operation_delay"] = (0, 0) # 操作后的延迟,(0, 0)表示操作前等待0s,操作后等待0s driver.settings['operation_delay_methods'] = ['click', 'swipe', 'drag', 'press'] driver.implicitly_wait(ReadConfig().get_implicitly_wait) # 设置隐式等待时间,默认20s driver.unlock() # 解锁设备 with driver.watch_context(builtin=True) as ctx: ctx.when("^(下载|更新)").when("取消").click() # 当同时出现(下载 或 更新)和 取消 按钮时,点击取消 ctx.when("跳过%").click() ctx.when("%允许%").click() ctx.when("继续").click() ctx.when("每次开启").click() ctx.when("创建").when("取消").click() ctx.when("恢复").when("取消").click() for element in ReadConfig().get_popup_elements: ctx.when(element).click() ctx.wait_stable(seconds=1.5) # 开启弹窗监控,并等待界面稳定(两个弹窗检查周期内没有弹窗代表稳定) yield driver driver.set_fastinput_ime(False) # 关闭自动化定制的输入法 driver.app_stop_all() # 停止所有应用 driver.screen_off() # 息屏 elif platform == "ios": driver = wda.USBClient().session(alert_action=wda.AlertAction.ACCEPT) driver.wait_ready() driver.implicitly_wait(ReadConfig().get_implicitly_wait) driver.unlock() # 解锁 with driver.alert.watch_and_click(): for element in ReadConfig().get_popup_elements: ele = driver(labelContains=element).get(timeout=2.5, raise_error=False) if ele is None: continue else: ele.tap() yield driver driver.close() driver.lock() # 息屏
def take_screenshot(self, name="截图") -> str: """ 屏幕截图 """ screenshot_path = os.path.join(ReadConfig().get_project_dir, "report", "screenshot") if not os.path.exists(screenshot_path): os.makedirs(screenshot_path) filename = os.path.join(screenshot_path, str(uuid.uuid1()) + ".png") self.driver.screenshot(filename) logging.info("take screenshot: %s", str(filename)) with allure.step("进行截图"): allure.attach.file( filename, name + " " + time.strftime("%m-%d %H:%M:%S", time.localtime()) + " " + str(filename), allure.attachment_type.PNG) # 添加截图到报告中 return filename
""" Author: W9007851 Date: 2020/10/28 File: run.py """ import os import time import pytest from common import project_dir from common.config_parser import ReadConfig from soloop.v122 import root_dir platform = ReadConfig().get_platform os.system('python -m uiautomator2 init') def local_run(): # env = ReadConfig().get_env args = [root_dir + "/case/", "-m B", "--alluredir", project_dir + "/report/raw_data/"] # 单独执行一个级别用例 # args = [root_dir + "/case/", "--alluredir", project_dir + "/report/raw_data/"] # args = [root_path + "/case/", "-m", env, "--alluredir", project_dir + "/report/raw_data/"] # args = [root_dir + "/case/", "-m S", "--reruns", "1", "--reruns-delay", "5", "--alluredir", project_dir + "/report/raw_data/"] # args = [root_dir + "/case/", "--reruns", "1", "--reruns-delay", "5", "--alluredir", project_dir + "/report/raw_data/"] pytest.main(args) def generate_report(): report_path = os.path.join(project_dir, "report", "html_report_" + time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime(time.time())))
def __init__(self, driver: Union[Device, wda.Client]): self.driver = driver self.platform = ReadConfig().get_platform self.implicitly_wait = ReadConfig().get_implicitly_wait self.check_error_toast = ReadConfig().get_check_error_toast