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
相关推荐
李狗蛋儿啊1 小时前
zero自动化框架搭建---Git安装详解
运维·git·自动化
专注VB编程开发20年3 小时前
除了 EasyXLS,加载和显示.xlsx 格式的excel表格,并支持单元格背景色、边框线颜色和粗细等格式化特性
c++·windows·excel·mfc·xlsx
Natsuagin4 小时前
轻松美化双系统启动界面与同步时间设置(Windows + Ubuntu)
linux·windows·ubuntu·grub
xing.yu.CTF5 小时前
Web入侵实战分析-常见web攻击类应急处置实验2
运维·服务器·windows·web安全·apache·php漏洞·phpstudy后门漏洞
ceffans7 小时前
PDF文档中文本解析
c++·windows·pdf
SummerGao.7 小时前
Windows 快速搭建C++开发环境,安装C++、CMake、QT、Visual Studio、Setup Factory
c++·windows·qt·cmake·visual studio·setup factory
Mike_188702783518 小时前
1688代采下单API接口使用指南:实现商品采集与自动化下单
前端·python·自动化
Otaku love travel8 小时前
自动化网页检测提醒
自动化·公告监测·提醒
Igallta_8136228 小时前
【小游戏】C++控制台版本俄罗斯轮盘赌
c语言·开发语言·c++·windows·游戏·游戏程序
wave_sky10 小时前
Visual Studio中打开多个项目
ide·windows·visual studio