microsoft / WinAppDriver

Windows Application Driver
MIT License
3.7k stars 1.41k forks source link

How to attach session for already opened Application in "Java" #569

Open prasanna532 opened 5 years ago

prasanna532 commented 5 years ago

I am able to see the solution in C# but there is no solution in Java

ilpersi commented 5 years ago

I am using Appium and this is how I got it working like this

DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
desktopCapabilities.setCapability("platformName", "Windows");
desktopCapabilities.setCapability("deviceName", "WindowsPC");
desktopCapabilities.setCapability("app", "Root");

// You get the desktop session
WindowsDriver DesktopSession = new WindowsDriver(new URL("http://127.0.0.1:4723/wd/hub"), desktopCapabilities);

// Here you find the already running application and get the handle
WebElement MAWebElement = DesktopSession.findElementByName("My RunningApp");
String MAWinHandleStr = MAWebElement.getAttribute("NativeWindowHandle");
int MAWinHandleInt = Integer.parseInt(MAWinHandleStr);
String MAWinHandleHex = Integer.toHexString(MAWinHandleInt);

// You attach to the already running application
DesiredCapabilities MACapabilities = new DesiredCapabilities();
MACapabilities.setCapability("platformName", "Windows");
MACapabilities.setCapability("deviceName", "WindowsPC");
// You set the Handle as one of the capabilities
MACapabilities.setCapability("appTopLevelWindow", MAWinHandleHex);

// My Application Session
WindowsDriver MASession = new WindowsDriver(new URL("http://127.0.0.1:4723/wd/hub"), MACapabilities);
prasanna532 commented 5 years ago

@ilpersi

I tried the way what you have mentioned but it is throwing the following error:

I think hexcode code should be something like 0x.. for winhandles right but in java it is not converting to that. i tried appending with 0x but it showing the below exception..

Caused by: org.openqa.selenium.WebDriverException: 0x270990is not a top level window handle (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 102 milliseconds Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:32:19.891Z' System info: host: 'user', ip: '10.1.xxx.xx', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181' Driver info: driver.version: WindowsDriver at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166) at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$new$0(JsonWireProtocolResponse.java:53) at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$getResponseFunction$2(JsonWireProtocolResponse.java:91) at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:123) at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source) at java.util.Spliterators$ArraySpliterator.tryAdvance(Unknown Source) at java.util.stream.ReferencePipeline.forEachWithCancel(Unknown Source) at java.util.stream.AbstractPipeline.copyIntoWithCancel(Unknown Source) at java.util.stream.AbstractPipeline.copyInto(Unknown Source) at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source) at java.util.stream.FindOps$FindOp.evaluateSequential(Unknown Source) at java.util.stream.AbstractPipeline.evaluate(Unknown Source) at java.util.stream.ReferencePipeline.findFirst(Unknown Source) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126) ... 18 more

christophe-garcia commented 5 years ago

Hello, i'm trying to made some tests with WinAppDriver in Java, but like @prasanna532, but i'm facing issues too. I've already tried the calculator sample with the appId, and this is working. I'm trying now to launch the Calc.exe and to create session by attaching to app top level window.

The part i'm not able to solve in Java is the one which has to create session by attaching to app top level window. This part in c# :

// Create session by attaching to Cortana top level window
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("appTopLevelWindow", CortanaTopLevelWindowHandle);
CortanaSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);

Does someone has some sample code in Java for this part ?

Thanks by advance for your help.

OhAye commented 5 years ago

+1 - need a solution for this problem in Java please

ilpersi commented 5 years ago

The code below is working fine for me. I use Appium to run it. You need to manually run the calculator before launching this.

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.windows.WindowsDriver;
import java.net.*;

public class TestAppiumCalc {

    public static void main(String[] args) {

        try {

            DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
            desktopCapabilities.setCapability("platformName", "Windows");
            desktopCapabilities.setCapability("deviceName", "WindowsPC");
            desktopCapabilities.setCapability("app", "Root");

            WindowsDriver desktopSession = new WindowsDriver(new URL("http://127.0.0.1:4723/wd/hub"), desktopCapabilities);

            WebElement BHWebElement = desktopSession.findElementByName("Calculator");
            String CalcWinHandleStr = BHWebElement.getAttribute("NativeWindowHandle");
            int CalcWinHandleInt = Integer.parseInt(CalcWinHandleStr);
            String CalcWinHandleHex = Integer.toHexString(CalcWinHandleInt);

            DesiredCapabilities CalcCapabilities = new DesiredCapabilities();
            CalcCapabilities.setCapability("platformName", "Windows");
            CalcCapabilities.setCapability("deviceName", "WindowsPC");
            CalcCapabilities.setCapability("appTopLevelWindow", CalcWinHandleHex);
            System.out.println("Calc Handle is: " + CalcWinHandleHex);

            WindowsDriver calcSession = new WindowsDriver(new URL("http://127.0.0.1:4723/wd/hub"), CalcCapabilities);

            calcSession.findElementByName("One").click();
            calcSession.findElementByName("Plus").click();
            calcSession.findElementByName("Seven").click();
            calcSession.findElementByName("Equals").click();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

}