简体   繁体   English

如何使用 TestStack.White 框架通过变量标题查找 Window?

[英]How to find Window by variable title using TestStack.White framework?

I am using TestStack.White framework to automate opening new document in MS Word 2013.我正在使用 TestStack.White 框架在 MS Word 2013 中自动打开新文档。

I am opening Microsoft Word application with:我正在打开 Microsoft Word 应用程序:

   Application application = Application.Launch("winword.exe");

After that, I am trying to get the window by partial title:之后,我试图通过部分标题获取窗口:

   Window window = application.GetWindow("Word", InitializeOption.NoCache);

But it throws an exception saying that there is no such window.但是它抛出一个异常,说没有这样的窗口。

Window title is: Document1 - Word窗口标题为: Document1 - Word

The question is: How to get a window by partial title taking into consideration that the title is changing every time: "Document2 - Word", "Document3 - Word", etc.问题是:考虑到标题每次都在变化,如何通过部分标题获取窗口:“Document2 - Word”、“Document3 - Word”等。

Also tried *Word but looks like this func does not support wildcards也试过 *Word 但看起来这个 func 不支持通配符

If I invoke: List windows = application.GetWindows();如果我调用: List windows = application.GetWindows(); after launching an application, windows list is empty.启动应用程序后,windows 列表为空。

Thanks in advance, Ostap提前致谢,奥斯塔普

It looks like opening window takes some noticeable time.看起来打开窗口需要一些明显的时间。 GUI testing frameworks often have functions like Wait() to make sure the window is already created/visible/enabled. GUI 测试框架通常具有像 Wait() 这样的函数来确保窗口已经创建/可见/启用。 I'm not an expert in Teststack.White.我不是 Teststack.White 的专家。 Probably this document may help: http://teststackwhite.readthedocs.io/en/latest/AdvancedTopics/Waiting/可能这份文件可能会有所帮助: http : //teststackwhite.readthedocs.io/en/latest/AdvancedTopics/Waiting/

You can use EnumWindows to find all the open windows.您可以使用EnumWindows查找所有打开的窗口。

Within that callback you'll get a window handle which you can then us with GetWindowTextLength and GetWindowText在该回调中,您将获得一个窗口句柄,然后您可以使用GetWindowTextLengthGetWindowText

This will let you decide what window handle is to the window you want.这将让您决定您想要的窗口的窗口句柄。 From there you can use GetWindowThreadProcessId to retrieve the process ID for the word document.从那里您可以使用GetWindowThreadProcessId来检索 word 文档的进程 ID。

And finally with that you can create a TestStack White application using the Application.Start()最后,您可以使用 Application.Start() 创建一个 TestStack White 应用程序

public static Window GetWindowBySubstring(this Application app, string titleSubString)
{
    return app.GetWindows().FirstOrDefault(w => w.Title.Contains(titleSubString));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM