Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to use element in << operator #151

Merged
merged 1 commit into from
May 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions src/canopy/canopy.fs
Original file line number Diff line number Diff line change
Expand Up @@ -278,35 +278,41 @@ let first cssSelector = (elements cssSelector).Head
let last cssSelector = (List.rev (elements cssSelector)).Head

//read/write
let private writeToSelect cssSelector text =
let elem = element cssSelector
let private writeToSelect (elem:IWebElement) (text:string) =
let options = Seq.toList (elem.FindElements(By.XPath(sprintf "option[text()='%s']" text)))
match options with
| [] -> raise (CanopyOptionNotFoundException(sprintf "element %s does not contain value %s" cssSelector text))
| [] -> raise (CanopyOptionNotFoundException(sprintf "element %s does not contain value %s" (elem.ToString()) text))
| head::tail -> head.Click()

let ( << ) cssSelector text =
wait elementTimeout (fun _ ->
let writeToElement (e : IWebElement) =
if e.TagName = "select" then
writeToSelect cssSelector text
else
let readonly = e.GetAttribute("readonly")
if readonly = "true" then
raise (CanopyReadOnlyException(sprintf "element %s is marked as read only, you can not write to read only elements" cssSelector))
try e.Clear() with ex -> ex |> ignore
e.SendKeys(text)

elements cssSelector
|> List.map (fun elem ->
try
writeToElement elem
true
with
| :? CanopyReadOnlyException as ex -> reraise()
| _ -> false)
|> List.exists (fun elem -> elem = true)
let private writeToElement (e : IWebElement) (text:string) =
if e.TagName = "select" then
writeToSelect e text
else
let readonly = e.GetAttribute("readonly")
if readonly = "true" then
raise (CanopyReadOnlyException(sprintf "element %s is marked as read only, you can not write to read only elements" (e.ToString())))
try e.Clear() with ex -> ex |> ignore
e.SendKeys(text)

let ( << ) item text =
match box item with
| :? IWebElement as elem -> writeToElement elem text
| :? string as cssSelector ->
wait elementTimeout (fun _ ->
elements cssSelector
|> List.map (fun elem ->
try
writeToElement elem text
true
with
//Note: Enrich exception with proper cssSelector description
| :? CanopyReadOnlyException -> raise (CanopyReadOnlyException(sprintf "element %s is marked as read only, you can not write to read only elements" cssSelector))
| _ -> false)
|> List.exists (fun elem -> elem = true)
)
| _ -> raise (CanopyNotStringOrElementException(sprintf "Can't read %O because it is not a string or element" item))



let private textOf (element : IWebElement) =
match element.TagName with
Expand Down
9 changes: 8 additions & 1 deletion tests/basictests/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ test (fun _ ->
"#lastName" << "Smith"
"#lastName" == "Smith"

"writing to #lastName (as element) sets text to John" &&& fun _ ->
!^ testpage
let lastname = element "#lastname"
clear lastname
lastname << "John"
lastname == "John"

"writing to .lastName sets text to new Smith in both boxes" &&& fun _ ->
!^ testpage
clear "#lastName"
Expand Down Expand Up @@ -590,7 +597,7 @@ context "pluggable finders tests"
//and let canopy do the work of trying to find something by convention
let findByHref href f =
try
let cssSelector = sprintf "a[href='%s']" href
let cssSelector = sprintf "a[href*='%s']" href
f(By.CssSelector(cssSelector)) |> List.ofSeq
with | ex -> []

Expand Down