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

Windows自动化⑪conftest中获取用例类-方法名称, 进可以通过不同的类名,选择不同的拽起方案
可以判断程序是否登录,联接的场景
问题分析:

  • 对于程序是否启动,如果启动后, 自动登录, 可以直接使用winAppDriver进行连接
  • 对于程序未登录情况, 需要单独处理, 拽起应用程序, 然后 切换到登录页面的窗体,进行登录操作
    以上为 思路

代码实现如下:

# -*- 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("请检查===应用程序===是否启动成功!")

# 后台是否有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
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值