As @iamdanchiv noted in his answer, browser.keys() will be deprecated, but there is a workaround (and I have to provide a PR for this).
What browser.keys() does under the hood is to call /session/:sessionId/keys in the WebDriver JsonWire protocol. However, if you look at the list of endpoints in the W3C Webdriver specification, that endpoint is not listed. I believe that it was previously part of the list, but was dropped. Instead, the specification states to use the endpoint /session/:sessionId/element/:elementId/value , which you can use to call the webdriverio browser.elementIdValue(ID, value) method instead.
Now, if you read the /session/:sessionId/keys specifications mentioned in the Selenium documentation on JsonWireProtocol , itβs pretty easy to replicate using other WebDriver features. The endpoint /session/:sessionId/keys just does this:
Sends strokes of a key sequence to the active element.
There is an endpoint that we can call to capture the current active element, which is /session/:sessionId/element/active , which is displayed in webdriverio browser.elementActive() .
In this case, all we need to do is to redefine this browser.keys() to first find out what the active element is, and then send the keys to this element.
So, this is the solution to solve the problem, if you want to send browser.keys("hello world") :
var result = browser.elementActive(); var activeElement = result.value && result.value.ELEMENT; if(activeElement){ browser.elementIdValue(activeElement, "hello world"); }
Note that this does not replicate the behavior of /session/:sessionId/keys exactly, which also does this according to the Selenium documentation:
This command is similar to the send keys command in every aspect, except for implicit termination: modifiers are not freed at the end of the call. Rather, the state of the modifier keys is maintained between calls, so interaction with the mouse can be performed by pressing the modifier keys.
The above solution implicitly issues modifier keys such as "SHIFT", "CTRL" at the end of the key sequence. Therefore, if you want to hold the key and do interactions with the mouse, then you are out of luck, we may have to wait until browsers implement the Webdriver API. But if all you wanted to do was send "CTRL" + "C", you can just send an array of these keys:
var result = browser.elementActive(); var activeElement = result.value && result.value.ELEMENT; if(activeElement){ browser.elementIdValue(activeElement, ["CTRL", "c"]); }