After performing actions of WPF app, not able to perform action on Printer window
ScreenShots_Printer window.docx I am automating ERP product using WinAppDriver where I am able to create sales order for a customer after login to app using this tool. When I run a report for a sales order, it opens 'Print window' for selecting printer. This the same window which appears while printing any office document in windows OS. When I spy objects using inspect.exe, 'Printer window' is not listed under my ERP product tree list, it is a separate child object under the Desktop hierarchy. When I try to perform operation on this 'Print Window', it shows element not found since it is looking Printer window under the ERP product session. Please find the attachment for printer window details.
I tried solution suggested in the incident "https://github.com/Microsoft/WinAppDriver/issues/373" (with capability as 'appTopLevelWindow' ) with following code. But still it fails with same error. This is blocking us to continue further flow in the system. Where am I making mistake ?
.........................................................................................................................................................................
//This 'session' is already created for the ERP product
var existingApplicationTopLevelWindow = session.CurrentWindowHandle;
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("appTopLevelWindow", existingApplicationTopLevelWindow);
PrinterSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
// Click on OK button of 'Print window' PrinterSession.FindElementByAccessibilityId("OK").FindElementByClassName("Button").Click(); ………………………………………………………………………………………………………………………...................................
WinAppDriver logs :
HTTP/1.1 200 OK Content-Length: 134 Content-Type: application/json
{"sessionId":"7510C809-570C-4294-9FCF-B5D7FDFD0338","status":0,"value":"ERP Product"}
========================================== GET /session/7510C809-570C-4294-9FCF-B5D7FDFD0338/title HTTP/1.1 Accept: application/json, image/png Host: 127.0.0.1:4723
HTTP/1.1 200 OK Content-Length: 134 Content-Type: application/json
{"sessionId":"7510C809-570C-4294-9FCF-B5D7FDFD0338","status":0,"value":"ERP Product"}
========================================== POST /session HTTP/1.1 Accept: application/json, image/png Content-Length: 83 Content-Type: application/json;charset=utf-8 Host: 127.0.0.1:4723
{"desiredCapabilities":{"appTopLevelWindow":"0x0005189C","platformName":"Windows"}} HTTP/1.1 200 OK Content-Length: 131 Content-Type: application/json
{"sessionId":"594D312F-B0AC-455D-8932-063A521FBEFF","status":0,"value":{"appTopLevelWindow":"0x0005189C","platformName":"Windows"}}
========================================== GET /session/7510C809-570C-4294-9FCF-B5D7FDFD0338/title HTTP/1.1 Accept: application/json, image/png Host: 127.0.0.1:4723
HTTP/1.1 200 OK Content-Length: 134 Content-Type: application/json
{"sessionId":"7510C809-570C-4294-9FCF-B5D7FDFD0338","status":0,"value":"ERP Product"}
========================================== GET /session/7510C809-570C-4294-9FCF-B5D7FDFD0338/title HTTP/1.1 Accept: application/json, image/png Host: 127.0.0.1:4723
HTTP/1.1 200 OK Content-Length: 134 Content-Type: application/json
{"sessionId":"7510C809-570C-4294-9FCF-B5D7FDFD0338","status":0,"value":"ERP Product"}
========================================== POST /session/594D312F-B0AC-455D-8932-063A521FBEFF/element HTTP/1.1 Accept: application/json, image/png Content-Length: 41 Content-Type: application/json;charset=utf-8 Host: 127.0.0.1:4723
{"using":"accessibility id","value":"OK"} HTTP/1.1 404 Not Found Content-Length: 139 Content-Type: application/json
{"status":7,"value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters."}}
========================================== GET /session/7510C809-570C-4294-9FCF-B5D7FDFD0338/title HTTP/1.1 Accept: application/json, image/png Host: 127.0.0.1:4723
HTTP/1.1 200 OK Content-Length: 134 Content-Type: application/json
{"sessionId":"7510C809-570C-4294-9FCF-B5D7FDFD0338","status":0,"value":"ERP Product"}
Hi @VijayHugar,
I'd recommend creating a desktop session and switching to it.
It should most likely appear under the Desktop Session, and you can also try using the XPath generated from the UI Recorder tool to automate against it.
@hassanuz Thank you for taking your time and looking into this issue! I am still facing the same issue. Below is my complete code. In the first method, I create ERP session and perform action on the product - works fine. After running report in the ERP, printer window appears where I am not able to perform action on it.
Below code is for ERP application session
public static void DesktopSession(string desktopClientExePath) { if (session == null) { // Create a new session to ERP Desktop application DesiredCapabilities appCapabilities = new DesiredCapabilities(); appCapabilities.SetCapability("app", desktopClientExePath); appCapabilities.SetCapability("appArguments", @"WPF.Client.exe"); appCapabilities.SetCapability("appWorkingDir", @"C:\Program Files\ ERP Client");
//appCapabilities.SetCapability("app", );
appCapabilities.SetCapability("deviceName", "Windows");
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities, TimeSpan.FromSeconds(30));
Assert.IsNotNull(session);
Assert.IsNotNull(session.SessionId);
// Verify that Login window is started with ERP title
Assert.AreEqual(" ERP", session.Title);
}
}
After performing actions on ERP products, when report is run, printer window appears for selecting printer. So to click on OK button on printer window, I called following method after running a report. However, while executing last line under the below method, element not found error appears. What mistake I am doing ?
public static void PrintWindowSession() { var printerQueueWindow=session.FindElementByName(@"ERPWIndow]").FindElementByClassName("Window"); //this session is from above ERP session 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 PrinterappCapabilities = new DesiredCapabilities();
PrinterappCapabilities.SetCapability("appTopLevelWindow", printerQueueWindowHandle);
PrinterSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), PrinterappCapabilities);
PrinterSession.FindElementByXPath("/Pane[@ClassName=\"#32769\"][@Name=\"Desktop 1\"]/Window[@ClassName=\"#32770\"] [@Name=\"Print\"]/Button[@ClassName=\"Button\"][@Name=\"OK\"]").Click(); // here it fails.
}
can you try this: once the printer window appears, try this, session.Keyboard.SendKeys(Keys.Enter);
@anunay1 Thanks for your input! I need to perform multiple actions on the Printer window like select the printer from drowndown etc, so session.Keyboard.SendKeys(Keys.Enter) will not be useful.
@hassanuz Can you please look into my code shared above ( My second post) and share your comment ? In order to continue with WinAppDriver, we need to solve this blockage issue, so we need your help.
@VijayHugar As hassanuz pointed out, you need to use the desktop session.
DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
desktopCapabilities.SetCapability("app","Root");
desktopSession = new WindowsDriver
var ShellWindow = desktopSession.FindElementByClassName("ApplicationFrameWindow"); var TopLevelWindowHandle = ShellWindow.GetAttribute("NativeWindowHandle"); TopLevelWindowHandle = (int.Parse(TopLevelWindowHandle)).ToString("x");
DesiredCapabilities appcapabilities = new DesiredCapabilities();
appcapabilities.SetCapability("appTopLevelWindow", TopLevelWindowHandle);
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl),appcapabilities);
or else try this when the printer window opens: var allWindowHandles = session.WindowHandles; session.SwitchTo().Window(allWindowHandles[1]);
check #910
@VijayHugar, has using a desktop session resolved your issue?
@hassanuz @anunay1 I am facing multiple issues while working with multiple windows. I believe It's is not smooth when it comes to working on a application ( my case WPF) where it triggers external windows like 'Print window' , ' File Save as Window' and again switching back to my application. Currently, I am working on this to find any workaround or may be I am doing wrong. I will be posting details report with my findings.
@hassanuz @anunay1 Do we have any example for the scenario mentioned below
- open Microsoft word,type some text. 2)Click on Print, printer window opens. Select printer as 'Microsoft XPS Document' or 'PDF' from the drop down ( this triggers Save As window when clicked on OK button) and click OK. 3)Save As window opens. Select file location in the Save As window and click Save button. 4)Go back to Microsoft word and type something again.
I have seen most of the issues posted related to handling multiple windows as explained above. So it would be great helpful if we have example code for this MS word scenario. I will post an example if I find solution with my application.
@VijayHugar Attached is the which does the following:
open Microsoft word,type some text. 2)Click on Print, printer window opens. Select printer as 'Microsoft XPS Document' or 'PDF' from the drop down ( this triggers Save As window when clicked on OK button) and click OK. 3)Save As window opens. Select file location in the Save As window and click Save button. 4)Go back to Microsoft word and type something again.
And it works perfectly. KT.zip
One more point, if you see the MSword document all the elements comes under one tree.
Finally I got some breakthrough. @hassanuz @anunay1 thank you very much for your support. Please review below code and give your feedback if any.
My tree structure in inspect.exe as followes :
Desktop 1 WPF Application. 2 Create Order. 2 Save As Window. 1 Print Window. 1 List. 2 PDF. 2 Microsoft XPS Document. 2 HP Printer.
My workflow 1.Launch WPF application - Child node under Desktop tree 2.Create Order for customer - - Child node under 'WPF Application' 3.Click on Print order, Printer window opens - Child node under 'Desktop' tree 4. Click on Drop down for selecting, list opens - Child node under 'Desktop' tree 5. Select Printer as 'Microsoft XPS Document - Child node under 'List' tree 6. Save As window opens - Child node under 'WPF Application' 7. Click on Save button. 8.Continue working with WPF application session.
What I have achieved so far I have achieved 1 to 8 scenarios except 5th scenario - which is selecting printer. I am currently working on this case.
My code snippet for above scenario
- Create 'Session' for WPF application Launch application, Create Order and click on Print
- For printer window, Create 'desktopSession' and attach it to 'Session' Identify Printer window and click on OK button. Save As window appears.
- Since Save As window is again under 'WPF application' tree, switch to main window with below code and perform action by clicking on Save button. var allWindowHandles = session.WindowHandles; session.SwitchTo().Window(allWindowHandles[0]);
- Continue working with your WPF application as session is already switched to main window.
Here is my complete code
- WPF session
public static void DesktopSession(string desktopClientExePath) { if (session == null) { // Create a new session to ERP Desktop application DesiredCapabilities appCapabilities = new DesiredCapabilities(); appCapabilities.SetCapability("app", desktopClientExePath); appCapabilities.SetCapability("appArguments", @"WPF.Client.exe"); appCapabilities.SetCapability("appWorkingDir", @"C:\Program Files\ ERP Client");
//appCapabilities.SetCapability("app", );
appCapabilities.SetCapability("deviceName", "Windows");
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities, TimeSpan.FromSeconds(30));
Assert.IsNotNull(session);
Assert.IsNotNull(session.SessionId);
// Verify that Login window is started with ERP title
Assert.AreEqual(" ERP", session.Title);
}
-
Create DesktopSession and attach it to 'Session' DesiredCapabilities desktopCapabilities = new DesiredCapabilities(); desktopCapabilities.SetCapability("app", "Root"); desktopSession = new WindowsDriver
(new Uri("http://127.0.0.1:4723"), desktopCapabilities); var ShellWindow = desktopSession.FindElementByClassName("#32770"); var TopLevelWindowHandle = ShellWindow.GetAttribute("NativeWindowHandle"); TopLevelWindowHandle = (int.Parse(TopLevelWindowHandle)).ToString("x"); //Attach it to session DesiredCapabilities appcapabilities = new DesiredCapabilities(); appcapabilities.SetCapability("appTopLevelWindow", TopLevelWindowHandle); session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appcapabilities); WindowsElement printWin = session.FindElementByName("Print").FindElementByClassName("#32770") as WindowsElement;
WindowsElement printOKButton = printWin.FindElementByName("OK").FindElementByClassName("Button") as WindowsElement; printOKButton.Click();
-
Switch session to main window //Switch session to main window var allWindowHandles = session.WindowHandles; session.SwitchTo().Window(allWindowHandles[0]);
-
On SaveAs window, Click on Save button
WindowsElement savePrintOutputWin= session.FindElementByName("Save Print Output As").FindElementByClassName("#32770") as WindowsElement; WindowsElement clickSaveButton = savePrintOutputWin.FindElementByName("Save") as WindowsElement; cancelSavePrintOutput.Click();
- Continue working with WPF application.
Looks good to me, test it
Now I have automated all the scenario's mentioned under 'My workflow' above. For selecting printer from drowdown, I have used workaround as below.
printerDropDown.Click(); WindowsElement listItems = desktopSession.FindElementByClassName("ComboLBox"); while (true) { if (listItems.Text == value) break; else listItems.SendKeys(Keys.ArrowDown); }
This is mentioned in the "https://github.com/Microsoft/WinAppDriver/issues/389"
@hassanuz @anunay1 Thank you very much. I will be closing this issue.