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
相关推荐
蒜丶3 小时前
Windows 11 22H2 跳过联网激活
windows
摘星编程7 小时前
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
windows·elasticsearch·kibana
YJlio7 小时前
PsPing 学习笔记(14.1):ICMP Ping 进阶——替代系统 ping 的正确姿势
windows·笔记·学习
郑泰科技7 小时前
mstsc 频繁断开是由svchost 策略引起的吗?
windows·笔记·负载均衡
开开心心就好8 小时前
OCR识别工具可加AI接口,快捷键截图翻译便捷
java·网络·windows·随机森林·电脑·excel·推荐算法
技术小甜甜9 小时前
[AI 工程实践] 远程调用 Ollama 报错解析:如何解决“本地文件找不到”的误区
ai·自动化·llm·agent·ollama·’人工智能·aider
喂自己代言9 小时前
Windows 10 22H2 纯系统自带工具安全方案
windows·安全
天才程序YUAN13 小时前
Windows自动修改系统环境变量(PATH)中所有 D 盘路径的脚本
运维·windows
2501_9240641114 小时前
2025年主流Web自动化测试工具功能与适用场景对比
前端·测试工具·自动化
幺零九零零14 小时前
Windows + Docker + k6 + InfluxDB + Grafana
windows·docker·grafana