Add helpful macro and procs for testing
This commit is contained in:
parent
d422b07394
commit
cb5923d9f8
1 changed files with 59 additions and 21 deletions
|
|
@ -1,5 +1,48 @@
|
|||
import os, options
|
||||
import os, options, unittest
|
||||
import webdriver
|
||||
import macros
|
||||
|
||||
macro with*(obj: typed, code: untyped): untyped =
|
||||
## Execute a set of statements with an object
|
||||
expectKind code, nnkStmtList
|
||||
result = code
|
||||
|
||||
# Simply inject obj into call
|
||||
for i in 0 ..< result.len:
|
||||
if result[i].kind in {nnkCommand, nnkCall}:
|
||||
result[i].insert(1, obj)
|
||||
|
||||
template click*(session: Session, element: string, strategy=CssSelector) =
|
||||
let el = session.findElement(element, strategy)
|
||||
check el.isSome()
|
||||
el.get().click()
|
||||
|
||||
template sendKeys*(session: Session, element, keys: string) =
|
||||
let el = session.findElement(element)
|
||||
check el.isSome()
|
||||
el.get().sendKeys(keys)
|
||||
|
||||
template sendKeys*(session: Session, element: string, keys: varargs[Key]) =
|
||||
let el = session.findElement(element)
|
||||
check el.isSome()
|
||||
|
||||
# focus
|
||||
el.get().click()
|
||||
for key in keys:
|
||||
session.press(key)
|
||||
|
||||
template ensureExists*(session: Session, element: string) =
|
||||
let el = session.findElement(element)
|
||||
check el.isSome()
|
||||
|
||||
template check*(session: Session, element: string, function: untyped) =
|
||||
let el = session.findElement(element)
|
||||
check function(el)
|
||||
|
||||
template checkText*(session: Session, element, expectedValue: string) =
|
||||
let el = session.findElement(element)
|
||||
check el.isSome()
|
||||
check el.get().getText() == expectedValue
|
||||
|
||||
proc waitForLoad*(session: Session, timeout=20000) =
|
||||
var waitTime = 0
|
||||
|
|
@ -14,29 +57,24 @@ proc waitForLoad*(session: Session, timeout=20000) =
|
|||
if waitTime > timeout:
|
||||
doAssert false, "Wait for load time exceeded"
|
||||
|
||||
proc wait*(session: Session) =
|
||||
session.waitForLoad()
|
||||
|
||||
proc wait*(session: Session, msTimeout: int) =
|
||||
session.waitForLoad(msTimeout)
|
||||
|
||||
proc logout*(session: Session) =
|
||||
# Check whether we can log out.
|
||||
let logoutLink = session.findElement(
|
||||
"Logout",
|
||||
LinkTextSelector
|
||||
).get()
|
||||
logoutLink.click()
|
||||
with session:
|
||||
click "#profile-btn"
|
||||
click "#logout-btn"
|
||||
|
||||
proc login*(session: Session, user, password: string) =
|
||||
let logIn = session.findElement("#login-btn").get()
|
||||
logIn.click()
|
||||
with session:
|
||||
click "#login-btn"
|
||||
|
||||
let usernameField = session.findElement(
|
||||
"#login-form input[name='username']"
|
||||
)
|
||||
sendKeys "#login-form input[name='username']", "admin"
|
||||
sendKeys "#login-form input[name='password']", "admin"
|
||||
|
||||
let passwordField = session.findElement(
|
||||
"#login-form input[name='password']"
|
||||
)
|
||||
sendKeys "#login-form input[name='password']", Key.Enter
|
||||
|
||||
usernameField.get().sendKeys("admin")
|
||||
passwordField.get().sendKeys("admin")
|
||||
passwordField.get().click() # Focus field.
|
||||
session.press(Key.Enter)
|
||||
|
||||
waitForLoad(session, 5000)
|
||||
wait(5000)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue