Implements ability to press buttons.

This commit is contained in:
Dominik Picheta 2018-05-19 13:18:15 +01:00
commit a2be578271

View file

@ -1,4 +1,6 @@
import httpclient, uri, json, tables, options
# For reference, this is brilliant: https://github.com/jlipps/simple-wd-spec
import httpclient, uri, json, tables, options, strutils, unicode
type
WebDriver* = ref object
@ -134,6 +136,55 @@ proc sendKeys*(self: Element, text: string) =
discard checkResponse(resp.body)
type
# https://w3c.github.io/webdriver/#keyboard-actions
Key* = enum
Unidentified = 0,
Cancel,
Help,
Backspace,
Tab,
Clear,
Return,
Enter,
Shift,
Control,
Alt,
Pause,
Escape
proc toUnicode(key: Key): Rune =
Rune(0xE000 + ord(key))
proc press*(self: Session, keys: varargs[Key]) =
let reqUrl = $(self.driver.url / "session" / self.id / "actions")
let obj = %*{"actions": [
{
"type": "key",
"id": "keyboard",
"actions": []
}
]}
for key in keys:
obj["actions"][0]["actions"].elems.add(
%*{
"type": "keyDown",
"value": $toUnicode(key)
}
)
obj["actions"][0]["actions"].elems.add(
%*{
"type": "keyUp",
"value": $toUnicode(key)
}
)
let resp = self.driver.client.post(reqUrl, $obj)
if resp.status != Http200:
raise newException(WebDriverException, resp.status)
discard checkResponse(resp.body)
when isMainModule:
let webDriver = newWebDriver()
let session = webDriver.createSession()