findElement returns Option[Element] and added close.
This commit is contained in:
parent
879ba8aabc
commit
1400f3f1d8
2 changed files with 20 additions and 7 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import httpclient, uri, json, tables
|
import httpclient, uri, json, tables, options
|
||||||
|
|
||||||
type
|
type
|
||||||
WebDriver* = ref object
|
WebDriver* = ref object
|
||||||
|
|
@ -61,6 +61,12 @@ proc createSession*(self: WebDriver): Session =
|
||||||
|
|
||||||
return Session(id: sessionObj["value"]["sessionId"].getStr(), driver: self)
|
return Session(id: sessionObj["value"]["sessionId"].getStr(), driver: self)
|
||||||
|
|
||||||
|
proc close*(self: Session) =
|
||||||
|
let reqUrl = $(self.driver.url / "session" / self.id)
|
||||||
|
let resp = self.driver.client.request(reqUrl, HttpDelete)
|
||||||
|
|
||||||
|
let respObj = checkResponse(resp.body)
|
||||||
|
|
||||||
proc navigate*(self: Session, url: string) =
|
proc navigate*(self: Session, url: string) =
|
||||||
## Instructs the session to navigate to the specified URL.
|
## Instructs the session to navigate to the specified URL.
|
||||||
let reqUrl = $(self.driver.url / "session" / self.id / "url")
|
let reqUrl = $(self.driver.url / "session" / self.id / "url")
|
||||||
|
|
@ -81,15 +87,20 @@ proc getPageSource*(self: Session): string =
|
||||||
return respObj{"value"}.getStr()
|
return respObj{"value"}.getStr()
|
||||||
|
|
||||||
proc findElement*(self: Session, selector: string,
|
proc findElement*(self: Session, selector: string,
|
||||||
strategy = CssSelector): Element =
|
strategy = CssSelector): Option[Element] =
|
||||||
let reqUrl = $(self.driver.url / "session" / self.id / "element")
|
let reqUrl = $(self.driver.url / "session" / self.id / "element")
|
||||||
let reqObj = %*{"using": toKeyword(strategy), "value": selector}
|
let reqObj = %*{"using": toKeyword(strategy), "value": selector}
|
||||||
let resp = self.driver.client.postContent(reqUrl, $reqObj)
|
let resp = self.driver.client.post(reqUrl, $reqObj)
|
||||||
|
if resp.status == Http404:
|
||||||
|
return none(Element)
|
||||||
|
|
||||||
let respObj = checkResponse(resp)
|
if resp.status != Http200:
|
||||||
|
raise newException(WebDriverException, resp.status)
|
||||||
|
|
||||||
|
let respObj = checkResponse(resp.body)
|
||||||
|
|
||||||
for key, value in respObj["value"].getFields().pairs():
|
for key, value in respObj["value"].getFields().pairs():
|
||||||
return Element(id: value.getStr(), session: self)
|
return some(Element(id: value.getStr(), session: self))
|
||||||
|
|
||||||
proc getText*(self: Element): string =
|
proc getText*(self: Element): string =
|
||||||
let reqUrl = $(self.session.driver.url / "session" / self.session.id /
|
let reqUrl = $(self.session.driver.url / "session" / self.session.id /
|
||||||
|
|
@ -106,4 +117,6 @@ when isMainModule:
|
||||||
"Entertainment-System/dp/B073BVHY3F"
|
"Entertainment-System/dp/B073BVHY3F"
|
||||||
session.navigate(amazonUrl)
|
session.navigate(amazonUrl)
|
||||||
|
|
||||||
echo session.findElement("#priceblock_ourprice").getText()
|
echo session.findElement("#priceblock_ourprice").get().getText()
|
||||||
|
|
||||||
|
session.close()
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
author = "Dominik Picheta"
|
author = "Dominik Picheta"
|
||||||
description = "Implementation of the WebDriver w3c spec."
|
description = "Implementation of the WebDriver w3c spec."
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue