microsoft / WinAppDriver

Windows Application Driver
MIT License
3.66k stars 1.4k forks source link

Trying to send Function Key (F6) #834

Open hicks86 opened 5 years ago

hicks86 commented 5 years ago

Using Appium and WinAppdriver to test drive our Microsoft Word Addin (using Addin-Express). So far I have been able to do most things however hitting a bit of a brick wall with the .SendKeys() functionality.

I want to send a Function key to toggle a particular mode on the addin (keyboard driven mode) This is what I am currently doing:

var element = session.FindElementByName("Name of Document - WORD");
//setup
element.SendKeys(Keys.F6);

The result is that it switches the F6 mode on for a split-second then switches it off - (I can tell as a little keyboard will appear when this mode is active, and disappears when it is not).

Any ideas why it this is occurring?

Below is the AppiumLog


[WD Proxy] Proxying [POST /wd/hub/session/4efbbda2-fa45-4229-ab96-e4b86641c699/element/42.26283008/value] to [POST http://127.0.0.1:4724/wd/hub/session/C16C6E26-E8D0-494C-B32F-7001FDD38AEC/element/42.26283008/value] with body: {"value":[""],"text":""}
[WinAppDriver] [STDOUT] 
[WinAppDriver] [STDOUT] 
[WinAppDriver] [STDOUT] ==========================================
[WinAppDriver] [STDOUT] POST /wd/hub/session/C16C6E26-E8D0-494C-B32F-7001FDD38AEC/element/42.26283008/value HTTP/1.1
[WinAppDriver] [STDOUT] Accept: application/json, */*
[WinAppDriver] [STDOUT] Connection: close
[WinAppDriver] [STDOUT] Content-Length: 30
[WinAppDriver] [STDOUT] Content-Type: application/json; charset=utf-8
[WinAppDriver] [STDOUT] Host: 127.0.0.1:4724
[WinAppDriver] [STDOUT] User-Agent: appium
[WinAppDriver] [STDOUT] 
[WinAppDriver] [STDOUT] {"value":[""],"text":""}
[WinAppDriver] [STDOUT] HTTP/1.1 200 OK
[WinAppDriver] [STDOUT] Content-Length: 63
[WinAppDriver] [STDOUT] Content-Type: application/json
[WinAppDriver] [STDOUT] 
[WinAppDriver] [STDOUT] {"sessionId":"C16C6E26-E8D0-494C-B32F-7001FDD38AEC","status":0}
[WD Proxy] Got response with status 200: {"sessionId":"C16C6E26-E8D0-494C-B32F-7001FDD38AEC","status":0}
[WD Proxy] Replacing sessionId C16C6E26-E8D0-494C-B32F-7001FDD38AEC with 4efbbda2-fa45-4229-ab96-e4b86641c699
[HTTP] <-- POST /wd/hub/session/4efbbda2-fa45-4229-ab96-e4b86641c699/element/42.26283008/value 200 144 ms - 63
kicks321 commented 5 years ago

SendKeys() is like this, it's a POST function, so whenever you as the developer calls that method, all you are doing is telling the local WinAppDriver to send some type of keyboard command to a specific element.

What you actually want to check out, is generically telling the the server to not send anything to any element, but press the F6 key like so as long as you are focused in on your application:

session.Keyboard.PressKey(OpenQA.Selenium.Keys.F6);

hicks86 commented 5 years ago

Hi @Rangyia thanks for the response. Yup that what I assumed as much. So I dug a little deeper into what was actually being sent. So according to this Windows help documentation on the WindowsForm.Keys enum (https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.keys) the value that should be sent from pressing the F6 key is 117.

I put together a quick WinForm app to capture OnKeyDown event. Using the keyboard manual approach I received 117. However driving the app from Appium using the code you suggested I receive 255 117 255. With some further investigation it looks like the app that I am testing will disable the mode if it receives any other keys. So hence the reason why I am seeing the particular mode come on, then immediately switch off.

So the Selenium.Keys.F6 (and I imagine many more) is not sending the exact same KeyValue as the Window Forms Keys spec. Not sure if this is a problem for anyone else but it caused me issues.

As a work around I am now using SendKeys.SendWait("{F6}"); from the System.Windows.Forms namespace (https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys). This works a treat and is significantly quicker.