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):

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

412

被折叠的 条评论
为什么被折叠?



