WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

Unable to switch from Notepad session to Print Queue windows

Open AmitVerma4HP opened this issue 8 years ago • 8 comments

I am trying to automate below steps with latest Appium and WinAppDriver on Windows 10

  1. Open existing notepad test file
  2. Go for print from menu option
  3. Once print is issued then open respective print queue ( like process = new ProcessBuilder("rundll32.exe","printui.dll","PrintUIEntry","/o","/n",printerName).start(); )
  4. Then switch control from Notepad session to Print Queue session to validate the queued job.

I need to know how can I switch from Notepad session to Print Queue session?

What I have tried

  1. Tried setCapability for appTopLevelWindow but session could not be created
  2. Windows handle switch works within same app context but not across apps

Any help and clue will be highly appreciated.

AmitVerma4HP avatar Mar 12 '18 21:03 AmitVerma4HP

Hi @AmitVerma4HP,

In general you would want one session per one UI application. In this case, you already have a session for Notepad and simply needs to create one for the Printer Queue UI you launched yourself. To do this you can create a Desktop Session, locate the Printer Queue UI window, and attach to it. E.g.

DesiredCapabilities desktopAppCapabilities = new DesiredCapabilities();
desktopAppCapabilities.SetCapability("app", CommonTestSettings.DesktopAppId);
var desktopSession = new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), desktopAppCapabilities);

var printerQueueWindow = desktopSession.FindElementByClassName("PrintUI_PrinterQueue");
var printerQueueWindowHandle = printerQueueWindow.GetAttribute("NativeWindowHandle");
printerQueueWindowHandle = (int.Parse(printerQueueWindowHandle)).ToString("x"); // Convert to Hex

// Create a new session by attaching to an existing application top level window
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("appTopLevelWindow", printerQueueWindowHandle);
session = new WindowsDriver<WindowsElement>(new Uri(CommonTestSettings.WindowsApplicationDriverUrl), appCapabilities);

You can also locate the Printer Queue window using the printer name to avoid picking the wrong window if multiple Printer Queue windows exist. In addition, I would highly recommend launching the Printer Queue window from Settings app instead of executing start process locally. That way, you would then be able to run your test script on a remote machine that has WinAppDriver running.

timotiusmargo avatar Mar 12 '18 23:03 timotiusmargo

@timotiusmargo Thanks for the quick feedback. Let me try this and get back.

AmitVerma4HP avatar Mar 13 '18 17:03 AmitVerma4HP

@timotiusmargo I tried the solution but it isn't creating session. It fails with org.openqa.selenium.SessionNotCreatedException: Unable to create new remote session. desired capabilities = Capabilities [{platformName=Windows, appTopLevelWindow=8064a, deviceName=DESKTOP-46461K1}], required capabilities = Capabilities [{}]

Below is the method that I am using - _public static void switchToPrintQueue() throws MalformedURLException { DesiredCapabilities desktopAppCapabilities = new DesiredCapabilities(); desktopAppCapabilities.setCapability("app", "Root"); desktopAppCapabilities.setCapability("deviceName", "DESKTOP-46461K1"); DesktopSession = new WindowsDriver(new URL(WindowsApplicationDriverUrl), desktopAppCapabilities); Assert.assertNotNull(DesktopSession);

    	printerQueueWindow = DesktopSession.findElementByClassName("PrintUI_PrinterQueue");
    	printerQueueWindowHandle = printerQueueWindow.getAttribute("NativeWindowHandle");
    	int printerQueueWindowHandleInt=Integer.parseInt(printerQueueWindowHandle);
    	
    	printerQueueWindowHandleHex = Integer.toHexString(printerQueueWindowHandleInt);
    	
    	// Create a new session by attaching to an existing application top level window
    	DesiredCapabilities appCapabilities = new DesiredCapabilities();
    	appCapabilities.setCapability("appTopLevelWindow", printerQueueWindowHandleHex);
    	appCapabilities.setCapability("deviceName", "DESKTOP-46461K1");
    	PrinterQueueSession = new WindowsDriver<WindowsElement>(new URL(WindowsApplicationDriverUrl), appCapabilities);
    	Assert.assertNotNull(PrinterQueueSession);
    }_

AmitVerma4HP avatar Mar 20 '18 00:03 AmitVerma4HP

Hi @AmitVerma4HP,

Can you paste the JSON wire protocol logs from WinAppDriver.exe console that includes the retrieval of the NativeWindowHandle and the appTopLevelWindow session creation? For example:

==========================================
GET /session/02B74126-B681-4E21-BCCF-396E22BA39BB/element/42.267352/attribute/NativeWindowHandle HTTP/1.1
Accept: application/json, image/png
Host: 127.0.0.1:4723


HTTP/1.1 200 OK
Content-Length: 80
Content-Type: application/json

{"sessionId":"02B74126-B681-4E21-BCCF-396E22BA39BB","status":0,"value":"267352"}

==========================================
POST /session HTTP/1.1
Accept: application/json, image/png
Content-Length: 78
Content-Type: application/json;charset=utf-8
Host: 127.0.0.1:4723

{"desiredCapabilities":{"appTopLevelWindow":"41458","platformName":"Windows"}}

HTTP/1.1 200 OK
Content-Length: 126
Content-Type: application/json

{"sessionId":"C44DCD24-1005-4A48-8FC4-21BC5EBFEFB5","status":0,"value":{"appTopLevelWindow":"41458","platformName":"Windows"}}

==========================================
GET /session/C44DCD24-1005-4A48-8FC4-21BC5EBFEFB5/source HTTP/1.1
Accept: application/json, image/png
Host: 127.0.0.1:4723



HTTP/1.1 200 OK
Content-Length: 16118
Content-Type: application/json

{"sessionId":"C44DCD24-1005-4A48-8FC4-21BC5EBFEFB5","status":0,"value":"<?xml version=\"1.0\" encoding=\"utf-16\"?><Window AcceleratorKey=\"\" AccessKey=\"\" AutomationId=\"\" ClassName=\"PrintUI_PrinterQueue\" FrameworkId=\"Win32\" HasKeyboardFocus=\"False\" HelpText=\"\" IsContentElement=\"True\" IsControlElement=\"True\" IsEnabled=\"True\" IsKeyboardFocusable=\"True\" IsOffscreen=\"False\" IsPassword=\"False\" IsRequiredForForm=\"F [...]

timotiusmargo avatar Mar 20 '18 20:03 timotiusmargo

@timotiusmargo I could attach session to PrintQueue window after changing winappdriver.js in my npm appium as mentioned in https://github.com/appium/appium/issues/9106

Now I am stuck with getting XPath to validate print queue title or queued print job. Used inspect to get properties but it fails each time

AmitVerma4HP avatar Mar 20 '18 21:03 AmitVerma4HP

Here is the code - // Ensure you have Correct Print Queue opened Assert.assertNotNull(PrinterQueueSession.findElementByXPath("TitleBar//[starts-with(@Value, "HPB8033E")]")); System.out.println("Double checked that we opened correct printer queue => " +printerName);

	    //Validate Job Name
	    Assert.assertTrue(PrinterQueueSession.findElementByXPath("ListItem//[starts-with(@Name, \"test1 - Notepad\")]").getText().contains("test1 - Notepad"),"Expected Print Job NOT FOUND in Print Queue");

JSON WireProtocol Logs [debug] [JSONWP Proxy] Proxying [GET /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce] to [GET http://127.0.0.1:4728/wd/hub/session/B90ADA83-8975-4189-96C1-70240EBE05D2] with body: {} [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] ========================================== [WinAppDriver] [STDOUT] GET /wd/hub/session/B90ADA83-8975-4189-96C1-70240EBE05D2 HTTP/1.1 [WinAppDriver] [STDOUT] Accept: / [WinAppDriver] [STDOUT] Connection: keep-alive [WinAppDriver] [STDOUT] Content-Type: application/json [WinAppDriver] [STDOUT] Host: 127.0.0.1:4728 [WinAppDriver] [STDOUT] User-Agent: appium [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] HTTP/1.1 200 OK [WinAppDriver] [STDOUT] Content-Length: 128 [WinAppDriver] [STDOUT] Content-Type: application/json [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] {"sessionId":"B90ADA83-8975-4189-96C1-70240EBE05D2","status":0,"value":{"appTopLevelWindow":"0xA06E0","platformName":"Windows"}} [debug] [JSONWP Proxy] Got response with status 200: "{"sessionId":"B90ADA83-8975-4189-96C1-70240EBE05D2","status":0,"value":{"appTopLevelWindow":"0xA06E0","platformName":"Windows"}}" [JSONWP Proxy] Replacing sessionId B90ADA83-8975-4189-96C1-70240EBE05D2 with 7caca227-e70c-473d-ab9e-6b5c26258dce [HTTP] <-- GET /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce 200 7 ms - 128 [HTTP] --> GET /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce {} [MJSONWP] Driver proxy active, passing request on via HTTP proxy [debug] [JSONWP Proxy] Proxying [GET /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce] to [GET http://127.0.0.1:4728/wd/hub/session/B90ADA83-8975-4189-96C1-70240EBE05D2] with body: {} [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] ========================================== [WinAppDriver] [STDOUT] GET /wd/hub/session/B90ADA83-8975-4189-96C1-70240EBE05D2 HTTP/1.1 [WinAppDriver] [STDOUT] Accept: / [WinAppDriver] [STDOUT] Connection: keep-alive [WinAppDriver] [STDOUT] Content-Type: application/json [WinAppDriver] [STDOUT] Host: 127.0.0.1:4728 [WinAppDriver] [STDOUT] User-Agent: appium [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] HTTP/1.1 200 OK [WinAppDriver] [STDOUT] Content-Length: 128 [WinAppDriver] [STDOUT] Content-Type: application/json [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] {"sessionId":"B90ADA83-8975-4189-96C1-70240EBE05D2","status":0,"value":{"appTopLevelWindow":"0xA06E0","platformName":"Windows"}} [debug] [JSONWP Proxy] Got response with status 200: "{"sessionId":"B90ADA83-8975-4189-96C1-70240EBE05D2","status":0,"value":{"appTopLevelWindow":"0xA06E0","platformName":"Windows"}}" [JSONWP Proxy] Replacing sessionId B90ADA83-8975-4189-96C1-70240EBE05D2 with 7caca227-e70c-473d-ab9e-6b5c26258dce [HTTP] <-- GET /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce 200 26 ms - 128 [HTTP] --> POST /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce/element {"using":"xpath","value":"TitleBar//[starts-with(@Value, "HPB8033E")]"} [MJSONWP] Driver proxy active, passing request on via HTTP proxy [debug] [JSONWP Proxy] Proxying [POST /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce/element] to [POST http://127.0.0.1:4728/wd/hub/session/B90ADA83-8975-4189-96C1-70240EBE05D2/element] with body: {"using":"xpath","value":"TitleBar//[starts-with(@Value, "HPB8033E")]"} [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] ========================================== [WinAppDriver] [STDOUT] POST /wd/hub/session/B90ADA83-8975-4189-96C1-70240EBE05D2/element HTTP/1.1 [WinAppDriver] [STDOUT] Accept: / [WinAppDriver] [STDOUT] Connection: keep-alive [WinAppDriver] [STDOUT] Content-Length: 73 [WinAppDriver] [STDOUT] Content-Type: application/json [WinAppDriver] [STDOUT] Host: 127.0.0.1:4728 [WinAppDriver] [STDOUT] User-Agent: appium [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] {"using":"xpath","value":"TitleBar//[starts-with(@Value, "HPB8033E")]"} [WinAppDriver] [STDOUT] HTTP/1.1 500 Internal Error [WinAppDriver] [STDOUT] Content-Length: 136 [WinAppDriver] [STDOUT] Content-Type: application/json [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] {"status":19,"value":{"error":"XPath Lookup Error","message":"Invalid XPath expression: TitleBar//[starts-with(@Value, "HPB8033E")]"}} [MJSONWP] Encountered internal error running command: {"w3cStatus":400,"jsonwp":{"status":19,"value":{"error":"XPath Lookup Error","message":"Invalid XPath expression: TitleBar//[starts-with(@Value, "HPB8033E")]"}}} ProxyRequestError: Could not proxy command to remote server. Original error: 500 - {"status":19,"value":{"error":"XPath Lookup Error","message":"Invalid XPath expression: TitleBar//[starts-with(@Value, "HPB8033E")]"}} at JWProxy.proxy$ (C:\Users\E2E Lab\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\jsonwp-proxy\proxy.js:152:13) at tryCatch (C:\Users\E2E Lab\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40) at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\E2E Lab\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22) at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (C:\Users\E2E Lab\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21) at GeneratorFunctionPrototype.invoke (C:\Users\E2E Lab\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37) at [HTTP] <-- POST /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce/element 500 371 ms - 199 [HTTP] --> POST /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce/element {"using":"xpath","value":"TitleBar//[starts-with(@Value, "HPB8033E")]"} [MJSONWP] Driver proxy active, passing request on via HTTP proxy [debug] [JSONWP Proxy] Proxying [POST /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce/element] to [POST http://127.0.0.1:4728/wd/hub/session/B90ADA83-8975-4189-96C1-70240EBE05D2/element] with body: {"using":"xpath","value":"TitleBar//[starts-with(@Value, "HPB8033E")]"} [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] ========================================== [WinAppDriver] [STDOUT] POST /wd/hub/session/B90ADA83-8975-4189-96C1-70240EBE05D2/element HTTP/1.1 [WinAppDriver] [STDOUT] Accept: / [WinAppDriver] [STDOUT] Connection: keep-alive [WinAppDriver] [STDOUT] Content-Length: 73 [WinAppDriver] [STDOUT] Content-Type: application/json [WinAppDriver] [STDOUT] Host: 127.0.0.1:4728 [WinAppDriver] [STDOUT] User-Agent: appium [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] {"using":"xpath","value":"TitleBar//[starts-with(@Value, "HPB8033E")]"} [WinAppDriver] [STDOUT] HTTP/1.1 500 Internal Error [WinAppDriver] [STDOUT] Content-Length: 136 [WinAppDriver] [STDOUT] Content-Type: application/json [WinAppDriver] [STDOUT] [WinAppDriver] [STDOUT] {"status":19,"value":{"error":"XPath Lookup Error","message":"Invalid XPath expression: TitleBar//[starts-with(@Value, "HPB8033E")]"}} [MJSONWP] Encountered internal error running command: {"w3cStatus":400,"jsonwp":{"status":19,"value":{"error":"XPath Lookup Error","message":"Invalid XPath expression: TitleBar//[starts-with(@Value, "HPB8033E")]"}}} ProxyRequestError: Could not proxy command to remote server. Original error: 500 - {"status":19,"value":{"error":"XPath Lookup Error","message":"Invalid XPath expression: TitleBar//[starts-with(@Value, "HPB8033E")]"}} at JWProxy.proxy$ (C:\Users\E2E Lab\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\jsonwp-proxy\proxy.js:152:13) at tryCatch (C:\Users\E2E Lab\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:67:40) at GeneratorFunctionPrototype.invoke [as _invoke] (C:\Users\E2E Lab\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:315:22) at GeneratorFunctionPrototype.prototype.(anonymous function) [as throw] (C:\Users\E2E Lab\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:100:21) at GeneratorFunctionPrototype.invoke (C:\Users\E2E Lab\AppData\Roaming\npm\node_modules\appium\node_modules\babel-runtime\regenerator\runtime.js:136:37) at [HTTP] <-- POST /wd/hub/session/7caca227-e70c-473d-ab9e-6b5c26258dce/element 500 301 ms - 199 [HTTP] --> DELETE /wd/hub/session/12a84140-e463-4765-8bd7-519e30eaa184 {} [debug] [MJSONWP] Calling AppiumDriver.deleteSession() with args: ["12a84140-e463-4765-8bd7-519e30eaa184"] [debug] [BaseDriver] Event 'quitSessionRequested' logged at 1521564050250 (09:40:50 GMT-0700 (Pacific Daylight Time)) [Appium] Removing session 12a84140-e463-4765-8bd7-519e30eaa184 from our master session list [debug] [WinAppDriver] Deleting WinAppDriver session [debug] [WinAppDriver] Deleting WinAppDriver server session

AmitVerma4HP avatar Mar 20 '18 21:03 AmitVerma4HP

Hi @AmitVerma4HP,

Glad to hear that the attach to top level window feature helped your scenario. For the XPath query issue, would you open a different issue and simply move the last comment as it is no longer relevant to the original topic? This will help others to easily find similar solution for the issue they may encounter.

timotiusmargo avatar Mar 20 '18 21:03 timotiusmargo

Done and thank you for your prompt response @timotiusmargo

AmitVerma4HP avatar Mar 20 '18 21:03 AmitVerma4HP