Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for optionally setting a window foreground when connecting the window #1068

Conversation

yihleego
Copy link
Contributor

该 PR 目的:在 Windows 环境下,初始化airtest.core.win.Windows对象时,允许开发者选择性跳过connect()方法中的set_foreground()功能。

特殊场景

我们在 Windows 环境下调用airtest.core.api.connect_device(uri)方法发现某个窗口(实际样式是菜单)闪了一下就消失了。

disappear

airtest.core.win.Windows源码中,发现对象在初始化时会调用一次set_foreground()方法。
(似乎该窗口接收到任意非鼠标点击指令都会触发关闭事件?)

得益于 Python 的特性和 Airtest 的CUSTOM_DEVICES的设计,我们临时通过以下方式禁用了该功能:

from airtest.core.helper import G
from airtest.core.win import Windows

class HackingWindows(Windows):
    def connect(self, handle=None, **kwargs):
        if handle:
            handle = int(handle)
            self.app = self._app.connect(handle=handle)
            self._top_window = self.app.window(handle=handle).wrapper_object()
        else:
            for k in ["process", "timeout"]:
                if k in kwargs:
                    kwargs[k] = int(kwargs[k])
            self.app = self._app.connect(**kwargs)
            self._top_window = self.app.top_window().wrapper_object()
        # Eureka! We've got it.
        #self.set_foreground()

G.CUSTOM_DEVICES['windows'] = HackingWindows

虽然这种方式可以解决上述的问题,但是实现不够优雅,于是希望能将其作为一个 featrue 实现。

实现方案

airtest.core.win.Windowsconnect方法新增一个foreground参数,默认为True,与原行为保持一致。

使用方法

d = connect_device("Windows:///123456?foreground=False")
w = Windows(handle=123456, foreground=False)

最后,非常感谢 Airtest 的各位 Maintainers 和 Contributors,Airtest 不仅在自动化测试方面很优秀,在其他场景也非常好用,例如:我们使用 Airtest 实现了机器人流程自动化。
希望能和大家共同完善项目。

@yimelia
Copy link
Member

yimelia commented Jul 19, 2022

你好,非常感谢你的PR!
有一个疑问是,描述中提到的问题描述不是很清楚,所以想再确认下,是set_foreground会导致什么问题么?
有没有什么软件窗口可以供我们进行测试,并查看到这个问题的?

@yihleego
Copy link
Contributor Author

yihleego commented Jul 19, 2022

你好,非常感谢你的PR! 有一个疑问是,描述中提到的问题描述不是很清楚,所以想再确认下,是set_foreground会导致什么问题么? 有没有什么软件窗口可以供我们进行测试,并查看到这个问题的?

@yimelia 抱歉,让您久等了

运行环境

  • Windows 10
  • Python 3.9
  • Airtest 1.2.6(最新)
  • 企业微信 4.0.8.6604(最新,官网下载)

功能描述

通过 Airtest 实现的脚本,自动点击主窗口中的 [Add Calendar] 按钮,弹出日程菜单,并点击其中一个菜单选项。

process

测试结果

普通情况下,点击 [Add Calendar] 按钮,通过connect_device连接菜单窗口,会立即消失:

normal

禁用Windows类中的connect方法中的set_foreground后,菜单不会消失,后续操作正常进行:

disabled

Demo

附 Demo 仓库链接:https://github.com/yihleego/airtest-pr-demo
包含一个 test.py 文件和若干素材图片

@yimelia
Copy link
Member

yimelia commented Aug 29, 2022

抱歉回复有点晚,内容已经确认没有问题,非常感谢你的PR!谢谢支持!
预计内容将在下一个版本被发布到pypi,即>=1.2.7版本可用

@yimelia yimelia merged commit 14d03bf into AirtestProject:master Aug 29, 2022
@yimelia yimelia mentioned this pull request Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants