WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

sendkeys fails with error An unknown error occurred in the remote end while processing the command

Open shankarkc opened this issue 8 years ago • 28 comments

My testcode will launch microsoftword and tries to type inside. Here is the code I am using


    	String application="C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\WINWORD.EXE";
    	String appWorkingDir="c:\\tmp";
    	//To suppress the splash screen and want to use blank word doc. so we use flags /q and /w flags respectively
    	String appArguments = "/q /w";
...
....
....
WebElement element =  ((WindowsDriver) webDriver).findElement("name", "Page 1 content");
		if ( element != null ) {
					element.sendKeys(keysToSend);
               }

When I debug I notice that webelement is not NULL...But sendKeys is failing on it. I get exception "An unknown error occurred in the remote end while processing the command." I am not sure how to proceed..

WinAppDriver log

POST /wd/hub/session/7D092E3B-44FD-4C0F-957C-4F99363BEF69/element/42.1115954.2.2/value HTTP/1.1
Accept-Encoding: gzip,deflate
Connection: Keep-Alive
Content-Length: 55
Content-Type: application/json; charset=utf-8
Host: 10.173.75.51:4723
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_112)

{"id":"42.1115954.2.2","value":["This is sample text"]}
HTTP/1.1 500 Internal Error
Content-Length: 133
Content-Type: application/json

{"status":13,"value":{"error":"unknown error","message":"An unknown error occurred in the remote end while processing the command."}}

It will be helpful for us if you print what exactly went wrong. Please help me to proceed further.

shankarkc avatar Feb 20 '18 10:02 shankarkc

I even tried attaching from desktop session

				DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
                desktopCapabilities.setCapability("app", "Root");
                WindowsDriver rootSession = new WindowsDriver(new URL(wadHubURL), desktopCapabilities);
                Assert.assertNotNull(rootSession);
                
                                
                RemoteWebElement wordShellWindow = (RemoteWebElement) rootSession.findElementByName("Document1 - Word");
                String wordTopLevelWindowHandle = "0x"+ Integer.toHexString(Integer.decode(wordShellWindow.getAttribute("NativeWindowHandle"))).toString();
                
                DesiredCapabilities appCapabilities = new DesiredCapabilities();
                appCapabilities.setCapability("appTopLevelWindow", wordTopLevelWindowHandle);
                webDriver = new WindowsDriver<WindowsElement>(new URL(wadHubURL), appCapabilities);
                Assert.assertNotNull(webDriver);

              Assert.assertNotNull(((WindowsDriver)webDriver).findElement("name", "Page 1 content"));
              RemoteWebElement wordPage = ((WindowsDriver)webDriver).findElement("name", "Page 1 content");
            wordPage.click();
             wordPage.sendKeys("Test");

It fails for last 2 lines...I cannot make it work. Please suggest how to proceed further. My team is blocked bcz of this issue. It would be great if you take a look at this and help us.

shankarkc avatar Feb 20 '18 14:02 shankarkc

@hassanuz / @timotiusmargo These are some observations that might help you to find the rootcause....

Also Please make sure you are running the test on a remote machine and not on local machine. My observation is that when it is running on local machine it works fine ...but when we run on a remote machine we face this issue. On remote machine I am running using below command. On this machine I have MS word installed. I am trying to open it using WAD.

WinAppDriver.exe 10.xxx.xx.51 4723/wd/hub

One more observation is I created similar test for notepad application on remote machine...It works fine..Its able to send keys and click works fine. It fails only on application like microsoft word and that too when executed on a remote machine.

shankarkc avatar Feb 21 '18 03:02 shankarkc

@hassanuz / @timotiusmargo any updates on this?

shankarkc avatar Feb 22 '18 02:02 shankarkc

@hassanuz / @timotiusmargo any updates on this? My team is kind of blocked bcz of this issue. We cannot run the tests in CI environment as we need to run WAD on a remote machine. Your fast response highly appreciated.

shankarkc avatar Feb 26 '18 05:02 shankarkc

Hi @shankarkc,

Thank you for bringing up the issue along with all the information. I reproduced this issue and it came down to an exception that happened when attempting to set focus on the Microsoft Word Page 1 content Edit Control. The UIAutomationClient.IUIAutomationElement.SetFocus system call invoked at this element returned "Operation is not valid due to the current state of the object." error.

Looking at this issue, it doesn't warrant a fix with WinAppDriver as we need to enforce focus on the target element before sending the keyboard input. Your observation that this issue only happens on remote machine (running as administrator) may indicate that there's an access problem when WinAppDriver or the application under test is run as administrator. However, I also replicated the issue even when Microsoft Word is not run as administrator.

Nevertheless, I found that using a generic (non-element targeting) SendKeys works well in the scenario you described. You may need to find what the Java command looks like, but in C# it is as follows:

webDriver.Keyboard.SendKeys("Test");

Expected JSON Wire Protocol command:

POST /session/FC8AA401-30BC-4B22-B5D1-6C729171198C/keys HTTP/1.1

In addition, I also find element click to work fine with the Edit Control you are targeting. If this is also the case with your setup, you can simply use it to enforce the focus on the right element. Otherwise, I would attempt to click on other element within the window to set the focus instead.

A quick tip that I don't get to share enough is that you can set focus on a window by switching to it. I find this to be the most reliable way to enforce focus on an application window. E.g.

session.SwitchTo().Window(session.CurrentWindowHandle);

Would you try the workaround above and let us know?

timotiusmargo avatar Feb 26 '18 23:02 timotiusmargo

@timotiusmargo thanks for the investigation. I will gibe a try using switchto windows.. And non-element targeted send keys and update you.

shankarkc avatar Feb 27 '18 02:02 shankarkc

@timotiusmargo I tried this. But it didnt give me desired results.

webDriver = new WindowsDriver(new URL(wadHubURL), capabilities);
			Thread.sleep(5000);
			webDriver.switchTo().window(webDriver.getWindowHandle());
			((WindowsDriver)webDriver).findElement("name", "Page 1 content").click();

Click failed for me. Here is WAD log

==========================================
POST /wd/hub/session/F3FD0991-1620-4527-A2BF-8017F466E6C8/element HTTP/1.1
Accept-Encoding: gzip,deflate
Connection: Keep-Alive
Content-Length: 41
Content-Type: application/json; charset=utf-8
Host: 10.173.75.51:4723
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_112)

{"using":"name","value":"Page 1 content"}
HTTP/1.1 200 OK
Content-Length: 99
Content-Type: application/json

{"sessionId":"F3FD0991-1620-4527-A2BF-8017F466E6C8","status":0,"value":{"ELEMENT":"42.853088.2.2"}}


==========================================
POST /wd/hub/session/F3FD0991-1620-4527-A2BF-8017F466E6C8/element/42.853088.2.2/click HTTP/1.1
Accept-Encoding: gzip,deflate
Connection: Keep-Alive
Content-Length: 22
Content-Type: application/json; charset=utf-8
Host: 10.173.75.51:4723
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_112)


HTTP/1.1 500 Internal Error
Content-Length: 133
Content-Type: application/json

{"status":13,"value":{"error":"unknown error","message":"An unknown error occurred in the remote end while processing the command."}}


==========================================

I tried to use sendkeys too as you suggested without targeting elements. But it didnt work too.

                        webDriver = new WindowsDriver(new URL(wadHubURL), capabilities);
			Thread.sleep(5000);
			webDriver.switchTo().window(webDriver.getWindowHandle());
				((WindowsDriver)webDriver).getKeyboard().sendKeys("Test");

Here is wad log for same

==========================================
GET /wd/hub/session/694BEFE6-167E-4A09-B213-9C7FE242BCA5/window_handle HTTP/1.1
Accept-Encoding: gzip,deflate
Cache-Control: no-cache
Connection: Keep-Alive
Host: 10.173.75.51:4723
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_112)


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

{"sessionId":"694BEFE6-167E-4A09-B213-9C7FE242BCA5","status":0,"value":"0x00070432"}


==========================================
POST /wd/hub/session/694BEFE6-167E-4A09-B213-9C7FE242BCA5/window HTTP/1.1
Accept-Encoding: gzip,deflate
Connection: Keep-Alive
Content-Length: 21
Content-Type: application/json; charset=utf-8
Host: 10.173.75.51:4723
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_112)

{"name":"0x00070432"}
HTTP/1.1 200 OK
Content-Length: 63
Content-Type: application/json

{"sessionId":"694BEFE6-167E-4A09-B213-9C7FE242BCA5","status":0}


==========================================
POST /wd/hub/session/694BEFE6-167E-4A09-B213-9C7FE242BCA5/keys HTTP/1.1
Accept-Encoding: gzip,deflate
Connection: Keep-Alive
Content-Length: 18
Content-Type: application/json; charset=utf-8
Host: 10.173.75.51:4723
User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_112)


HTTP/1.1 500 Internal Error
Content-Length: 133
Content-Type: application/json

{"status":13,"value":{"error":"unknown error","message":"An unknown error occurred in the remote end while processing the command."}}

It would be great if you unblock my team.

shankarkc avatar Feb 27 '18 08:02 shankarkc

what puzzles me is why it works for notepad and fails for MSWord? As this is not opensource i cannot debug beyond this boundary. WAD doesnt handle apps with splash screens? I have disable splash screen. Still WAD has difficulty to interacting with UI elements.

shankarkc avatar Feb 27 '18 08:02 shankarkc

@timotiusmargo can you pls help me here...can you pls post a code that opens winword on a remote machine and types helloworld? why it works for notepad and fails for MSWord?

shankarkc avatar Mar 02 '18 09:03 shankarkc

I can also verify this issue. It only occurs for us when running tests from a tfs build agent

I can create a test that executes perfectly on the machine. If I execute the same test on the same machine via a tfs build, it fails at sendkeys with an "unknown error" it appears to be able to click find other elements without issue. SendKeys() and clear() both appear to fail

ashton721 avatar Mar 02 '18 23:03 ashton721

@timotiusmargo can we get some solution for this issue. Its a blocker for my team. If no solution in sight we have to evaluate some other tool for our testing. Your faster resolution is highly appreciated.

shankarkc avatar Mar 03 '18 02:03 shankarkc

@hassanuz / @timotiusmargo any updates on this?

shankarkc avatar Mar 04 '18 05:03 shankarkc

Hi @shankarkc,

The issue you saw is caused by the "Page 1 Content" Edit Control that is a part of a custom control used in Microsoft Word, This particular UI control is not keyboard focusable and unlike other non-actionable element, it actively refuse a focus event. For this reason, both set text and click hit the same error.

I understand that you attempted sending non-element targeting keys without success, In addition to this, I also had another workaround below that always succesfuly send the text to Microsoft Word in a remote machine as long as it is interactive (has keyboard and mouse focus).

// Send the input to the first parent node of "Page 1 Content" that can receive focus instead
RemoteWebElement element = session.FindElementByName("Document1");
if (element != null)
{
    element.Click();
    element.SendKeys("Test");
}

Would you try this instead and let us know how it goes? This perform the send text without issue in my setup.

timotiusmargo avatar Mar 05 '18 21:03 timotiusmargo

@timotiusmargo thanks for the pointers. Its working now. I realized now that there are some UI elements that cannot take keyboard inputs and if we send keybord inputs it will fail. As per your suggestion when i used Document1 it works and I am able to type text inside word document.

shankarkc avatar Mar 06 '18 05:03 shankarkc

Thanks for the update @shankarkc!. I'm glad it worked out well. Note that you may still need to refer to "Page 1 Content" when you are trying to retrieve the text value. E.g.

// Send the text input
RemoteWebElement element = session.FindElementByName("Document1");
if (element != null)
{
    element.Click();
    element.SendKeys("Test");
}

// Get the text value
RemoteWebElement element1 = session.FindElementByName("Page 1 content");
if (element1 != null)
{
    var text = element1.Text;
}

timotiusmargo avatar Mar 06 '18 18:03 timotiusmargo

Unfortunately this didn't work for me =/

filepi avatar Jul 23 '19 14:07 filepi

didn't work for me...

didhddldlq avatar Dec 06 '19 05:12 didhddldlq

me neither, I think it's losing the focus or the keyboard interface is being used by previous test runs.

444ghost avatar Dec 06 '19 06:12 444ghost

What makes me crazy is it sometimes work fine. But Once fails, It continuously fails. if I wait few minutes or restart computer, it works( or sometimes not!).

didhddldlq avatar Dec 06 '19 06:12 didhddldlq

What makes me crazy is it sometimes work fine. But Once fails, It continuously fails. if I wait few minutes or restart computer, it works( or sometimes not!).

Exactly the same...

444ghost avatar Dec 06 '19 06:12 444ghost