Winappdriver: Do we have possibility switch between apps during test executions?

Created on 29 Jun 2018  路  12Comments  路  Source: microsoft/WinAppDriver

Hi all,
I have a case when during test execution appears some form which is not a child of my main app?
How can I work with it?

Most helpful comment

Here's the C#. I haven't had a chance to convert it to Java yet. I'm pretty shaky with Java, so if anyone is better at it, feel free to take over. :) This script gets the desktop waits for a window of the expected name to exist on the desktop, and then creates a new session based off of that found window.

// Get the root desktop
DesiredCapabilities rootCapabilities = new DesiredCapabilities();
rootCapabilities.SetCapability("app", "Root");
// Create a session for the desktop
WindowsDriver<WindowsElement> desktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), rootCapabilities);

// Set up a loop so it doesn't fail if it tries to find the window too soon. Note: if you have multiple cases where you need to wait for a window to be present, it's best to abstract this logic out to a helper function.
WindowsElement arcMapWindow = null;
DateTime end = DateTime.Now.AddSeconds(90);
while(arcMapWindow == null && DateTime.Now < end)
{
   try
   {
   // Use the desktop session to find a window named "Untitled - ArcMap"
      arcMapWindow = desktopSession.FindElementByName("Untitled - ArcMap");
   }
   catch (InvalidOperationException) { }

   // Wait a second before retrying to not kill the processor on the machine I wrote this on
   Task.Delay(1000).Wait();
}

// If WAP never found the window, fail out with a known message
if(arcMapWindow == null)
{
    throw new NotFoundException("ArcMap window not found");
}
// After getting the window, get it's handle
string arcMapTopLevelWindowHandle = arcMapWindow.GetAttribute("NativeWindowHandle");
// Convert to Hex
arcMapTopLevelWindowHandle = (int.Parse(arcMapTopLevelWindowHandle)).ToString("x"); 
// Create session by attaching to the top level window
capabilities.SetCapability("appTopLevelWindow", arcMapTopLevelWindowHandle);
Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), capabilities);

// Do stuff with the session here.

All 12 comments

You can get the "root" desktop session and find the window from there:

https://github.com/Microsoft/WinAppDriver/blob/v1.0-RC/README.md#creating-a-desktop-session
then...
https://github.com/Microsoft/WinAppDriver/blob/v1.0-RC/README.md#attaching-to-an-existing-app-window

So basically, it will be a separate session from your main app session, but it will give you access to everything on the desktop. I can dig out an example I did for work a few weeks ago if the documentation isn't clear?

@PandaMagnus could you please so kind show your example(java?) ? thank you

Sorry for the delay, I missed your response! I'll open up my example as soon as I can, but it's in C#. :( It would take me a while but I could probably stumble through converting it to Java.

Here's the C#. I haven't had a chance to convert it to Java yet. I'm pretty shaky with Java, so if anyone is better at it, feel free to take over. :) This script gets the desktop waits for a window of the expected name to exist on the desktop, and then creates a new session based off of that found window.

// Get the root desktop
DesiredCapabilities rootCapabilities = new DesiredCapabilities();
rootCapabilities.SetCapability("app", "Root");
// Create a session for the desktop
WindowsDriver<WindowsElement> desktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), rootCapabilities);

// Set up a loop so it doesn't fail if it tries to find the window too soon. Note: if you have multiple cases where you need to wait for a window to be present, it's best to abstract this logic out to a helper function.
WindowsElement arcMapWindow = null;
DateTime end = DateTime.Now.AddSeconds(90);
while(arcMapWindow == null && DateTime.Now < end)
{
   try
   {
   // Use the desktop session to find a window named "Untitled - ArcMap"
      arcMapWindow = desktopSession.FindElementByName("Untitled - ArcMap");
   }
   catch (InvalidOperationException) { }

   // Wait a second before retrying to not kill the processor on the machine I wrote this on
   Task.Delay(1000).Wait();
}

// If WAP never found the window, fail out with a known message
if(arcMapWindow == null)
{
    throw new NotFoundException("ArcMap window not found");
}
// After getting the window, get it's handle
string arcMapTopLevelWindowHandle = arcMapWindow.GetAttribute("NativeWindowHandle");
// Convert to Hex
arcMapTopLevelWindowHandle = (int.Parse(arcMapTopLevelWindowHandle)).ToString("x"); 
// Create session by attaching to the top level window
capabilities.SetCapability("appTopLevelWindow", arcMapTopLevelWindowHandle);
Session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), capabilities);

// Do stuff with the session here.

Thank you @PandaMagnus. I'll be adding this to the FAQ and crediting you (if you don't mind).

Thank you @PandaMagnus , i was stuck some where in my project and this helped me to overcome it. More Power to you.

@hassanuz @angad16 You're welcome! Glad it was helpful to someone. :) And (wow, over a year later,) nope don't mind at all.

@angad16 @hassanuz Oh, and just to note that I forgot to mention in the code I posted: you could also use an ImplicitWait or a WebDriverWait instead of the while loop. Since I posted the original code snippet, I've tried both and all work just fine. Lately I've tended to use WebDriverWaits just so there's some familiarity for people already experienced with Selenium, but under the covers they're all doing similar things. :)

@PandaMagnus I tried the code you have shared above using java language, at the line below

WindowsElement arcMapWindow = Session.findElementByName("JDA Solutions");

I am getting below error message as

java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to io.appium.java_client.windows.WindowsElement

Can you please help me to resolve this issue.

Thanks,
Syed Azhar Farooq

Can someone provide an example with javascript using "selenium-appium"? @PandaMagnus

@syedfarooq sorry for the (very long) delay. I'll see if I can put a Java version together as soon as I can. Again, I'm not very good in Java, so I'll probably have to spend some time banging my head against getting a working example. Hopefully I'll have something together within the next week or so.

@syedfarooq It just dawned on me... did you try an explicit cast? I think in Java, the syntax is just WindowsElement arcMapWindow = (WindowsElement) Session.findElementByName("JDA Solutions");

Was this page helpful?
0 / 5 - 0 ratings