Winappdriver: Is there a way to attach?

Created on 18 Nov 2016  路  20Comments  路  Source: microsoft/WinAppDriver

Is there a way to attach to a currently running application? Using the "app" capability it is possible to configure the driver to launch an application, but for large applications that take 1-2 min to load and setup It'd be very helpful to be able to attach to them, once started, and then run all tests.

Aside form that, what is the best practice to verify a test that I'm currently writing? It'd be very time consuming to wait for an application to load every time while trying to get a test green. Is there an easier way to verify what's being written?

Enhancement

Most helpful comment

Do we have the same above code for Java ?

All 20 comments

Hello @ivan-prodanov,

The JSON wire protocol doesn't seem to cover the scenario of creating a session by attaching to already running application. What kind of information would you pass to the session creation to ensure that you can attach to your already running app? Does you app take 1-2 minutes before showing the actual UI window? Understanding this scenario will help us address issue that you may have.

If you create a new session of your app, will it create a new instance on top of existing one? Otherwise, you may be able to leave it running and see if you can run the test without restarting the app all the time. Using inspect.exe helps me in identifying the element tree and parameters I can write against. This will also help you be more confident writing more lines of code without having to retest every line.

Our application, Revit, does take 2-4 minutes to load as it is a 3D modeling environment...even with SSD and 16 Gigs of Ram.

You can try Revit out yourself to see the load time: http://www.autodesk.com/products/revit-family/free-trial

While inspect.exe helps identify the element tree, for our application, Revit, with thousands of controls inspect.exe appears to be super slow and crashes frequently.
Here is a video overview of the UI.

Any chance the Appium Inspector will be released to work with Windows Apps?

Indeed I agree with @ianceicys. I suggest this to be marked as a feature request for a mechanism that allows us to run tests on already executed applications. I'm confident as well when using inspect.exe, but delays are not easy to measure without running the actual test and calibrating them accordingly. It's easy to do with notepad or calculator, but in more complex scenarios - e.g 3d visualization of seismic data is just a slow process.

I have a similar scenario with a bootstrapper-application which will check for updates and then start another application with some arguments, then quit itself. I would want to open a session to the newly opened application.

I have the same needed as I develop a ide like product, which is multi process backed, It need a long time to start. As many workflow need be test on the ide, when I balance the test to different test suite, attach will be a must needed function!

i second for the above requirement. In my case, App1 opens App2 if we click a button. In that case i have to attach or switch to the App2 (which is already opened) from App1 to complete the test scenario .

@timotiusmargo @ivan-prodanov This is a much needed functionality for a automation framework. Please consider this as a important enhancement for winappdriver.

Thanks and good luck.

Hey Guyz,
I may be missing something over here, but I believe we already have workaround for this scenario..
Once you have got the desktopsession object, I guess you should be able to access all applications available in desktop.

DesiredCapabilities desktopCapabilities = new DesiredCapabilities(); 
desktopCapabilities.SetCapability("app", "Root"); 
desktopCapabilities.SetCapability("deviceName", "WindowsPC");
RemoteWebDriver desktopSession = new RemoteWebDriver(new Uri("http://127.0.0.1:4723"), desktopCapabilities);

the desktop root session is a great way to find windows that aren't in your existing tree.
We're also investigating a way to better support attach.

@dhapolapankaj Could you please share a sample code snippet for that function to find windows that aren't in existing tree?

@vijay44 correct me If I have not understood it properly

I haven't mentioned we can get the objects which are not visible in Inspect tree.
Ironically, we have some discrepancy in objects availability in WinAppDriver and Inspect.exe
Please refer Issue #212

All I am saying is whatever objects available in Inspect.exe, WinAppDriver should be able to intercept them.

In context to this issue, we already have an application running and @ivan-prodanov is asking if we can directly access this application object hierarchy. Till the time we don't have capability to attach we can have workaround to get the desktop session object and access the already running application.

Since I work in the Selenium WebDriver based project as well as I am using WinAppDriver,
I find a great potential in WinAppDriver, if it can access the DOM structure of Web Browser also, I believe tomorrow we don't have to use Selenium WebDriver ;)

Hi @dhapolapankaj,

Sorry if made you confused. Really what I asked was a sample code snippet for the workaround of attach function.

For example I have to paste a value to a notepad (which is already opened) by coping the value from calculator result display after doing some calculation.

@vijay44 the workaround is get the desktop root object and then search for your notepad.

@dhapolapankaj do you happen to know how to switch between the apps ? getWindowHandles() and switchTo() doesn't seems to work for Vijay44's scenario above.. any thoughts ?

In v1.0-RC release, Windows Application Driver now supports attaching to an existing app window. As it is shown in the documentation and samples, you can now create a session based on any top level window. Once the session is created, you can perform all operation as if you started the application yourself. Below is an example taken from the documentation on how to create a new session by attaching to Cortana window with the help of a Desktop Session to locate the window.

```c#
DesktopSession.Keyboard.SendKeys(Keys.Meta + "s" + Keys.Meta);

var CortanaWindow = DesktopSession.FindElementByName("Cortana");
var CortanaTopLevelWindowHandle = CortanaWindow.GetAttribute("NativeWindowHandle");
CortanaTopLevelWindowHandle = (int.Parse(CortanaTopLevelWindowHandle)).ToString("x"); // Convert to Hex

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

// Use the session to control Cortana
CortanaSession.FindElementByAccessibilityId("SearchTextBox").SendKeys("add");
```

Notice the use of the new appTopLevelWindow capability above to specify the application top level window to attach to.

Our test sample also includes test scenarios that demonstrate attaching to other existing session top level window without the help of a Desktop Session below.

Thank you everyone for your patience and for your suggestions that make WinAppDriver great.

Do we have the same above code for Java ?

Can I use the appTopLevelWindow to attach a browser opened by selenium?

Iam also having the same issue How to find the appToplevelWindow in Python or Java or if i can find the powershell script also fine.

I'm also want some samples in python.

Here's how I find an open Edge browser and attach to it in Python...

    def getEdgeDriver(self):
        # Launch a driver for the Windows Desktop (Root)
        desired_caps = {}
        desired_caps["app"] = "Root"
        desktop = self.launchApp(desired_caps)

        # Use the desktop driver to locate the window for Edge browser
        win = WebDriverWait(desktop, 120).until(EC.presence_of_element_located((By.CLASS_NAME,'ApplicationFrameWindow')))
        # Get an app driver for Edge from the window
        app_driver = self.getDriverFromWin(win)
        return app_driver

    def getDriverFromWin(self, win):
        win_handle1 = win.get_attribute("NativeWindowHandle")
        win_handle = format(int(win_handle1), 'x') # convert to hex string

        # Launch new session attached to the window
        desired_caps = {}
        desired_caps["appTopLevelWindow"] = win_handle
        driver = self.launchApp(desired_caps)
        driver.switch_to_window(win_handle)
        return driver

    def launchApp(self, desired_caps):
        dut_url = "http://" + self.dut_ip + ":" + self.app_port
        driver = webdriver.Remote(
            command_executor = dut_url,
            desired_capabilities = desired_caps)
        return driver

Do we have code above attaching to a running process using Javascript?
@timotiusmargo

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomw93 picture tomw93  路  4Comments

SimonKirkhamCL4U picture SimonKirkhamCL4U  路  3Comments

jsa34 picture jsa34  路  3Comments

didhddldlq picture didhddldlq  路  3Comments

Blank517 picture Blank517  路  4Comments