Python pywinauto PC端自动化测试核心代码封装类

该文提供两个使用Python的pywinauto库进行PC端自动化测试的核心代码封装类示例,包含初始化、控件获取、点击、文本设置、值获取等多种功能,便于进行UI自动化测试。

Python pywinauto PC端自动化测试核心代码封装类

以下是一个基于pywinauto的自动化测试核心代码封装类的完整代码实例,其中包含多个函数实例并加上中文注释

方案1

import pywinauto
import time

class PywinautoWrapper:
    def __init__(self, app_path):
        """
        初始化函数,传入应用程序的路径
        """
        self.app_path = app_path
        self.app = pywinauto.Application().start(self.app_path)

    def get_app(self):
        """
        获取应用程序对象
        """
        return self.app

    def get_main_window(self):
        """
        获取主窗口对象
        """
        return self.app.top_window()

    def get_control(self, control_type, control_name, parent=None):
        """
        获取控件对象
        """
        if parent is None:
            parent = self.get_main_window()
        return parent.child_window(control_type=control_type, best_match=control_name)

    def get_controls(self, control_type, control_name, parent=None):
        """
        获取多个控件对象
        """
        if parent is None:
            parent = self.get_main_window()
        return parent.child_windows(control_type=control_type, best_match=control_name)

    def click_control(self, control_type, control_name, parent=None, double_click=False):
        """
        点击控件
        """
        control = self.get_control(control_type, control_name, parent)
        if double_click:
            control.double_click_input()
        else:
            control.click_input()

    def right_click_control(self, control_type, control_name, parent=None):
        """
        右键点击控件
        """
        control = self.get_control(control_type, control_name, parent)
        control.right_click_input()

    def set_text(self, control_type, control_name, text, parent=None):
        """
        设置控件文本
        """
        control = self.get_control(control_type, control_name, parent)
        control.set_text(text)

    def get_text(self, control_type, control_name, parent=None):
        """
        获取控件文本
        """
        control = self.get_control(control_type, control_name, parent)
        return control.get_text()

    def get_value(self, control_type, control_name, parent=None):
        """
        获取控件值
        """
        control = self.get_control(control_type, control_name, parent)
        return control.get_value()

    def is_checked(self, control_type, control_name, parent=None):
        """
        判断控件是否被选中
        """
        control = self.get_control(control_type, control_name, parent)
        return control.is_checked()

    def check(self, control_type, control_name, parent=None):
        """
        选中控件
        """
        control = self.get_control(control_type, control_name, parent)
        control.check()

    def uncheck(self, control_type, control_name, parent=None):
        """
        取消选中控件
        """
        control = self.get_control(control_type, control_name, parent)
        control.uncheck()

    def select(self, control_type, control_name, item_text, parent=None):
        """
        选择下拉框中的项
        """
        control = self.get_control(control_type, control_name, parent)
        control.select(item_text)

    def get_selected_text(self, control_type, control_name, parent=None):
        """
        获取下拉框中当前选中的项的文本
        """
        control = self.get_control(control_type, control_name, parent)
        return control.get_selected_text()

    def get_selected_index(self, control_type, control_name, parent=None):
        """
        获取下拉框中当前选中的项的索引
        """
        control = self.get_control(control_type, control_name, parent)
        return control.get_selected_index()

    def get_item_count(self, control_type, control_name, parent=None):
        """
        获取下拉框中的项的个数
        """
        control = self.get_control(control_type, control_name, parent)
        return control.item_count()

    def select_radio_button(self, control_type, control_name, parent=None):
        """
        选择单选按钮
        """
        control = self.get_control(control_type, control_name, parent)
        control.select()

    def is_enabled(self, control_type, control_name, parent=None):
        """
        判断控件是否可用
        """
        control = self.get_control(control_type, control_name, parent)
        return control.is_enabled()

    def is_visible(self, control_type, control_name, parent=None):
        """
        判断控件是否可见
        """
        control = self.get_control(control_type, control_name, parent)
        return control.is_visible()

    def is_exist(self, control_type, control_name, parent=None):
   
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

《代码爱好者》

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值