PC端自动化测试实战教程-2-pywinauto 启动PC端应用程序 – 上篇(详细教程)

1.简介

经过上一篇的学习、介绍和了解,想必小伙伴或者童鞋们,已经见识到pywinauto的强大了,今天继续介绍pywinauto,上一篇已经可以打开计算器了,这里宏哥在提供其他方法进行打开计算器和非电脑自带程序。pywinauto 可以启动电脑自带的应用程序,也可以启动电脑安装的应用程序。

2.运行指定的应用程序

运行指定的应用程序,顾名思义就是用户指定运行那款程序,就运行那款程序。宏哥这里将其分为两大类:电脑自带的应用程序,如:记事本、计算器等和电脑安装的应用程序,如:QQ、微信等。分好后,接下来看宏哥是如何这两类应用程序的。

2.1启动电脑自带的应用程序

上一篇已经启动计算器了,今天宏哥启动一下记事本,给小伙伴或者童鞋们来演示一下。

通过start() 方法指定exe应用程序的名称即可。start()方法用于启动一个可执行程序

    def start(self, cmd_line, timeout=None, retry_interval=None,               create_new_console=False, wait_for_idle=True, work_dir=None):         """Start the application as specified by cmd_line"""         # try to parse executable name and check it has correct bitness         if '.exe' in cmd_line and self.backend.name == 'win32':             exe_name = cmd_line.split('.exe')[0] + '.exe'             _warn_incorrect_binary_bitness(exe_name)          if timeout is None:             timeout = Timings.app_start_timeout         if retry_interval is None:             retry_interval = Timings.app_start_retry          start_info = win32process.STARTUPINFO()          # we need to wrap the command line as it can be modified         # by the function         command_line = cmd_line          # Actually create the process         dw_creation_flags = 0         if create_new_console:             dw_creation_flags = win32con.CREATE_NEW_CONSOLE         try:             (h_process, _, dw_process_id, _) = win32process.CreateProcess(                 None,                     # module name                 command_line,            # command line                 None,                     # Process handle not inheritable.                 None,                     # Thread handle not inheritable.                 0,                         # Set handle inheritance to FALSE.                 dw_creation_flags,        # Creation flags.                 None,                     # Use parent's environment block.                 work_dir,                # If None - use parent's starting directory.                 start_info)                # STARTUPINFO structure.         except Exception as exc:             # if it failed for some reason             message = ('Could not create the process "%s"n'                        'Error returned by CreateProcess: %s') % (cmd_line, str(exc))             raise AppStartError(message)          self.process = dw_process_id          if self.backend.name == 'win32':             self.__warn_incorrect_bitness()          def app_idle():             """Return true when the application is ready to start"""             result = win32event.WaitForInputIdle(                 h_process, int(timeout * 1000))              # wait completed successfully             if result == 0:                 return True              # the wait returned because it timed out             if result == win32con.WAIT_TIMEOUT:                 return False              return bool(self.windows())          # Wait until the application is ready after starting it         if wait_for_idle and not app_idle():             warnings.warn('Application is not loaded correctly (WaitForInputIdle failed)', RuntimeWarning)          self.actions.log("Started " + cmd_line + " application.")          return self

相关参数:

  • cmd_line: 是包含路径的启动应用程序的命令以及启动参数
  • timeout: 启动程序的超时时钟设置,默认为5s
  • create_new_console: 创建新的控制台,默认不创建
  • wait_for_idle: 是否等待到程序的Idle状态
  • work_dir: 指定工作目录
2.1.1代码设计

PC端自动化测试实战教程-2-pywinauto 启动PC端应用程序 - 上篇(详细教程)

2.1.2参考代码
# -*- coding:utf-8 -*-  # 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行  # 2.注释:包括记录创建时间,创建人,项目名称。 ''' Created on 2025-01-18 @author: 北京-宏哥 北京宏哥(微信搜索:北京宏哥,关注宏哥,提前解锁更多测试干货!) Project: Windows GUI自动化测试-2-pywinauto 启动PC端应用程序(详细教程) '''  # 3.导入模块  from pywinauto.application import Application # 启动记事本 app = Application(backend="uia").start("notepad.exe")

2.1.3运行代码

1.运行代码,右键Run'Test',就可以看到控制台输出,如下图所示:

PC端自动化测试实战教程-2-pywinauto 启动PC端应用程序 - 上篇(详细教程)

2.运行代码后电脑端的动作(启动记事本)。如下图所示:

PC端自动化测试实战教程-2-pywinauto 启动PC端应用程序 - 上篇(详细教程)

2.2启动电脑安装的应用程序

宏哥这里以大家熟悉的微信为例,属性-查看快捷方式,如下图所示:

PC端自动化测试实战教程-2-pywinauto 启动PC端应用程序 - 上篇(详细教程)

2.1.1代码设计

PC端自动化测试实战教程-2-pywinauto 启动PC端应用程序 - 上篇(详细教程)

2.1.2参考代码
# -*- coding:utf-8 -*-  # 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行  # 2.注释:包括记录创建时间,创建人,项目名称。 ''' Created on 2025-01-18 @author: 北京-宏哥 北京宏哥(微信搜索:北京宏哥,关注宏哥,提前解锁更多测试干货!) Project: Windows GUI自动化测试-2-pywinauto 启动PC端应用程序(详细教程) '''  # 3.导入模块  from pywinauto.application import Application # 启动微信 app = Application(backend="uia").start(r"D:WeChatWeChat.exe")

2.1.3运行代码

1.运行代码,右键Run'Test',就可以看到控制台输出,如下图所示:

PC端自动化测试实战教程-2-pywinauto 启动PC端应用程序 - 上篇(详细教程)

2.运行代码后电脑端的动作(启动微信)。如下图所示:

PC端自动化测试实战教程-2-pywinauto 启动PC端应用程序 - 上篇(详细教程)

3.小结

今天主要分享和讲解了电脑(PC端)系统自带的应用程序,如:记事本、计算器等等,以及电脑(PC端)系统安装的应用程序,如QQ、微信等等。如何启动以及启动的几种方法,非常简单。

 好了,时间不早了今天就分享到这里,感谢你耐心地阅读!

发表评论

评论已关闭。

相关文章