commit cee50130026791deca786ff71b50da0ab2c60039 Author: Dominik Picheta Date: Fri Oct 13 22:00:44 2017 +0100 Initial commit. diff --git a/src/webdriver.nim b/src/webdriver.nim new file mode 100644 index 0000000..d9d3aba --- /dev/null +++ b/src/webdriver.nim @@ -0,0 +1,65 @@ +import httpclient, uri, json, tables + +type + WebDriver* = ref object + url*: Uri + client*: HttpClient + + Session* = object + driver: WebDriver + id*: string + + WebDriverException* = object of Exception + + ProtocolException* = object of WebDriverException + +proc newWebDriver*(url: string = "http://localhost:4444"): WebDriver = + WebDriver(url: url.parseUri, client: newHttpClient()) + +proc createSession*(self: WebDriver): Session = + # Check the readiness of the Web Driver. + let resp = self.client.getContent($(self.url / "status")) + let obj = parseJson(resp) + + if obj{"value", "ready"}.isNil(): + let msg = "Readiness message does not follow spec" + raise newException(ProtocolException, msg) + + if not obj{"value", "ready"}.getBVal(): + raise newException(WebDriverException, "WebDriver is not ready") + + # Create our session. + let sessionReq = %*{"capabilities": {"browserName": "firefox"}} + let sessionResp = self.client.postContent($(self.url / "session"), + $sessionReq) + let sessionObj = parseJson(sessionResp) + if sessionObj{"value", "sessionId"}.isNil(): + raise newException(ProtocolException, "No sessionId in response to request") + + return Session(id: sessionObj["value"]["sessionId"].getStr(), driver: self) + +proc navigate*(self: Session, url: string) = + let reqUrl = $(self.driver.url / "session" / self.id / "url") + let obj = %*{"url": url} + let resp = self.driver.client.postContent(reqUrl, $obj) + + let respObj = parseJson(resp) + if respObj{"value"}.getFields().len != 0: + raise newException(WebDriverException, $respObj) + +proc getPageSource*(self: Session): string = + let reqUrl = $(self.driver.url / "session" / self.id / "source") + let resp = self.driver.client.getContent(reqUrl) + + let respObj = parseJson(resp) + if respObj{"value"}.isNil: + raise newException(WebDriverException, $respObj) + + return respObj{"value"}.getStr() + +when isMainModule: + let webDriver = newWebDriver() + let session = webDriver.createSession() + echo(session) + session.navigate("https://picheta.me") + echo(session.getPageSource()) diff --git a/webdriver.nimble b/webdriver.nimble new file mode 100644 index 0000000..66a8e12 --- /dev/null +++ b/webdriver.nimble @@ -0,0 +1,13 @@ +# Package + +version = "0.1.0" +author = "Dominik Picheta" +description = "Implementation of the WebDriver w3c spec." +license = "MIT" + +srcDir = "src" + +# Dependencies + +requires "nim >= 0.17.2" +