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
相关推荐
逻极4 小时前
Claude Code 实战:Spec-Kit、Kiro、OpenSpec 规范驱动开发三剑客
ide·人工智能·驱动开发·ai·自动化
lht6319356125 小时前
从Windows通过XRDP远程访问和控制银河麒麟 v10服务器
linux·运维·服务器·windows
3DVisionary5 小时前
从手动到智能:XTOM-STATION自动化检测中心在复杂曲面零件全尺寸检测中的应用
运维·自动化·自动驾驶·工业自动化·质量控制·自动化3d测量·复杂零件检测
阿豪学编程5 小时前
环境变量与程序地址空间
linux·运维·windows
墨倾许6 小时前
《Windows 11 + Docker:极简DVWA靶场搭建全记录》—— 附详细排错指南与最终解决方案
windows·笔记·网络安全·docker·容器·靶场
stark张宇9 小时前
盘点Nfs 文件服务在Windows上的坑??
linux·windows·centos
杨凯凡10 小时前
Docker环境搭建:Windows/macOS/Linux全平台教程
windows·macos·docker
阿昭L10 小时前
COM组件
windows
富唯智能13 小时前
智能投资,快速回本:复合机器人如何缩短你的投资回收期?
自动化·工业机器人·复合机器人
枫叶丹415 小时前
测试自动化新突破:金仓KReplay助力金融核心系统迁移周期缩减三周
网络·金融·自动化