Windows自动化⑪conftest中获取用例类-方法名称

Windows自动化⑪conftest中获取用例类-方法名称, 进可以通过不同的类名,选择不同的拽起方案

可以判断程序是否登录,联接的场景

问题分析:

  • 对于程序是否启动,如果启动后, 自动登录, 可以直接使用winAppDriver进行连接
  • 对于程序未登录情况, 需要单独处理, 拽起应用程序, 然后 切换到登录页面的窗体,进行登录操作
    以上为 思路
    代码实现如下:
python 复制代码
# -*- coding: utf-8 -*-
"""
conftest - 
Author: duxiaowei
Date: 2024/8/14
"""

import pytest
import time
from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import ElementNotInteractableException
from selenium.common.exceptions import ElementNotVisibleException
from com.common import hwork_start, read_yml


@pytest.fixture(scope="function", autouse=True)
def pc_start(request):
    # 获取当前测试用例的节点
    node = request.node
    # 尝试获取类名
    class_name = ""
    if hasattr(node, 'cls'):
        class_name = node.cls.__name__
        # 方法名总是可以从node.name获取
    method_name = node.name
    # print({"class_name": class_name, "method_name": method_name})

    # #################################################################
    # pro=生产, pre=预生产
    env = read_yml("env.yml")["pro"]
    # env = read_yml("env.yml")["pre"]

    # 登录模块必须先运行进行测试,这块逻辑后面梳理下

    # #################################################################
    app = env["app"]
    exe = env["exe"]
    if method_name != "test_登录":
        if hwork_start(app, exe):
            desired_caps = {}
            desired_caps['app'] = app
            driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', desired_capabilities=desired_caps)
            # 默认全局隐式等待设置
            driver.implicitly_wait(20)
            # 设置启动检查等待,等待搜索
            errors = [NoSuchElementException, ElementNotInteractableException, ElementNotVisibleException]
            wait = WebDriverWait(driver, timeout=100, poll_frequency=2, ignored_exceptions=errors)
            wait.until(EC.visibility_of(driver.find_element(by=By.NAME, value="搜索")))
            # 强制等待
            time.sleep(2)
            return driver
        else:
            print("请检查===应用程序===是否启动成功!")
    else:
        # 需要提供一个参数, 判断是否走[登录-模块测试]
        # 如果测试  登录模块 需要使用 hwork_start(app, exe, 1) 方法
        # 否则 不传 1
        # 确保程序已启动, 登录模块
        if hwork_start(app, exe, 1):
            # 添加启动参数
            desired_caps = {'app': 'Root', 'deviceName': 'WindowsPC', 'platformName': 'Windows'}
            driver = webdriver.Remote(command_executor='http://127.0.0.1:4723', desired_capabilities=desired_caps)
            # 默认全局隐式等待设置
            driver.implicitly_wait(20)
            time.sleep(3)
            # 切换 窗口
            main_win = driver.find_element(By.NAME, exe.split(".")[0])
            window_handle = main_win.get_attribute("NativeWindowHandle")
            window_handle_hex = hex(int(window_handle))
            # 使用窗口句柄创建新的会话
            caps = {
                'appTopLevelWindow': window_handle_hex,
                'deviceName': 'WindowsPC',
                'platformName': 'Windows'
            }
            driver_app = webdriver.Remote(command_executor='http://127.0.0.1:4723', desired_capabilities=caps)
            # 强制等待
            time.sleep(2)
            return driver_app
        else:
            print("请检查===应用程序===是否启动成功!")
python 复制代码
# 后台是否有Hwork.exe , 如果没有就需要 启动Hwork,会比较耗时, 如果有就不需要
# path=程序安装路径, exe=应用名称全名, fi=是否是登录模块
def hwork_start(path, exe, fi=0):
    all_process = get_all_process()
    if exe in all_process:
        return True
    else:
        try:
            # 需要启动Hwork
            from pywinauto.application import Application
            # 启动
            Application(backend='uia').start(path)
            time.sleep(15)
            if fi == 0:
                # 发送Alt+F4快捷键, 关闭窗口
                pyautogui.hotkey('alt', 'f4')

            return True
        except :
            return False
python 复制代码
import psutil

# 获取当前windows , 所有进程信息名称, 并去重
def get_all_process():
    list_process = set()
    # 获取当前windows , 所有进程信息
    for proc in psutil.process_iter(['pid', 'name']):
        list_process.add(proc.info["name"])
    return list_process
相关推荐
dpxiaolong15 分钟前
RK3588平台用v4l工具调试USB摄像头实践(亮度,饱和度,对比度,色相等)
android·windows
꧁坚持很酷꧂1 小时前
Winddows11官网下载安装VMware Workstation Pro17(图文详解)
windows
心随_风动1 小时前
主流操作系统对比分析(macOS、Linux、Windows、Unix)
linux·windows·macos
老狼孩111221 小时前
2025新版懒人精灵零基础及各板块核心系统视频教程-全分辨率免ROOT自动化开发
android·机器人·自动化·lua·脚本开发·懒人精灵·免root开发
Yang_yangyang2 小时前
mysql定时备份、打包、传输
数据库·mysql·自动化
兔子蟹子2 小时前
Java集合框架解析
java·windows·python
依旧阳光的老码农5 小时前
Windows下使用 VS Code + g++ 开发 Qt GUI 项目的完整指南
开发语言·windows·qt
哈哈幸运6 小时前
Linux Awk 深度解析:10个生产级自动化与云原生场景
linux·云原生·自动化·awk·三剑客
b***25119 小时前
自动点焊机批发:工业制造的高效之选
物联网·自动化·制造
annus mirabilis10 小时前
使用n8n构建自动化工作流:从数据库查询到邮件通知的使用指南
运维·数据库·自动化·n8n