WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

Do we have possibility switch between apps during test executions?

Open ant-skelia opened this issue 8 years ago • 17 comments

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?

ant-skelia avatar Jun 29 '18 10:06 ant-skelia

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 avatar Jun 30 '18 19:06 PandaMagnus

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

ant-skelia avatar Jul 02 '18 06:07 ant-skelia

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.

PandaMagnus avatar Aug 01 '18 23:08 PandaMagnus

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.

PandaMagnus avatar Aug 08 '18 16:08 PandaMagnus

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

hassanuz avatar Nov 14 '18 20:11 hassanuz

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

angad16 avatar Jan 30 '20 09:01 angad16

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

PandaMagnus avatar Jan 30 '20 15:01 PandaMagnus

@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 avatar Jan 30 '20 15:01 PandaMagnus

@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

syedfarooq avatar Feb 26 '20 11:02 syedfarooq

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

singhrajkr avatar Dec 14 '20 20:12 singhrajkr

@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.

PandaMagnus avatar Jan 07 '21 14:01 PandaMagnus

@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");

PandaMagnus avatar Jan 08 '21 16:01 PandaMagnus

Hi!

I tried your option and it's almost fix my problem.I didn't get the possibility to click on any element in the new screen.

My issue is that i'm opening a Excel file from the system itself and I want to work with him (read data).

I tried a lot of options.

In some of them i'm getting the window of Excel in front of the system that I export it from but can't click on the excel.

Could anyone please help me?

I'm working on the latest version of WinApp driver on C#.

Thank you!

Noam17135 avatar Jun 17 '21 13:06 Noam17135

@Noam17135 Do you have your code pushed somewhere that I can look at?

PandaMagnus avatar Aug 03 '21 14:08 PandaMagnus

Here's the Java example if someone is still struggling with it.

private static WindowsDriver desktopSession=null; private static WindowsDriver edgeSession=null; //switch to desktop/root Session appCapabilities.setCapability("app", "Root"); desktopSession = new WindowsDriver(new URL("http://127.0.0.1:4723"), appCapabilities);

//getting the native window handle of app to switch to String NativeWindowHandle=desktopSession.findElementByName("xyz"); //name of the app you wish to switch to int natWinHandleInt = Integer.parseInt(NativeWindowHandle); String natWinHandleStr = Integer.toHexString(natWinHandleInt); String handle="0x"+natWinHandleStr;

//switching to different app appCapabilities.setCapability("appTopLevelWindow", handle); edgeSession=new WindowsDriver(new URL("http://127.0.0.1:4723"), appCapabilities);

//do stuff with the new app

Hope this helps!! Let me know if anyone needs more help.

shilpamalhotra-git avatar Aug 27 '21 07:08 shilpamalhotra-git

@PandaMagnus thanks a lot for this example in C#. Worked fine for me. My application has a button that opens a another app and now I can manage both of them, which is great!

igorrecioh avatar Oct 04 '21 11:10 igorrecioh

Here's the Java example if someone is still struggling with it.

private static WindowsDriver desktopSession=null; private static WindowsDriver edgeSession=null; //switch to desktop/root Session appCapabilities.setCapability("app", "Root"); desktopSession = new WindowsDriver(new URL("http://127.0.0.1:4723"), appCapabilities);

//getting the native window handle of app to switch to String NativeWindowHandle=desktopSession.findElementByName("xyz"); //name of the app you wish to switch to int natWinHandleInt = Integer.parseInt(NativeWindowHandle); String natWinHandleStr = Integer.toHexString(natWinHandleInt); String handle="0x"+natWinHandleStr;

//switching to different app appCapabilities.setCapability("appTopLevelWindow", handle); edgeSession=new WindowsDriver(new URL("http://127.0.0.1:4723"), appCapabilities);

//do stuff with the new app

Hope this helps!! Let me know if anyone needs more help.

Thanks, @shilpamalhotra-git for this java version, I am trying with your code but it actually not performing that action. it may be because of my changes in code. it would be helpful if you take look at it. please suggest to me the changes required.

WebElement NativeWindowHandle11 = driver.findElementByName("Counter System"); String NativeWindowHandle = String.valueOf(NativeWindowHandle11); int natWinHandleInt = Integer.parseInt(NativeWindowHandle); String natWinHandleStr = Integer.toHexString(natWinHandleInt); String handle = "0x"+natWinHandleStr; cap.setCapability("appTopLevelWindows", handle); driver1 = new WindowsDriver(new URL("http://127.0.0.1:4723"), cap);

MandarMKamble avatar Oct 29 '22 12:10 MandarMKamble