简体   繁体   中英

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.

I am opening Microsoft Word application with:

   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

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.

Also tried *Word but looks like this func does not support wildcards

If I invoke: List windows = application.GetWindows(); after launching an application, windows list is empty.

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. I'm not an expert in Teststack.White. Probably this document may help: http://teststackwhite.readthedocs.io/en/latest/AdvancedTopics/Waiting/

You can use EnumWindows to find all the open windows.

Within that callback you'll get a window handle which you can then us with GetWindowTextLength and GetWindowText

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.

And finally with that you can create a TestStack White application using the Application.Start()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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