Skip to content

Commit

Permalink
#257 should improve problem
Browse files Browse the repository at this point in the history
  • Loading branch information
lefthandedgoat committed Apr 19, 2016
1 parent 72f0d39 commit 0b92e44
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/canopy/finders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,29 @@ let private loadJQuery () =
jsBrowser.ExecuteScript(load) |> ignore
wait 2.0 (fun _ -> jsBrowser.ExecuteScript(jqueryExistsScript) :?> bool)

//total cheese but instead of making a By type, we will just do 'FindElements' style because
//thats the only way its currently used. Cry
let findByJQuery cssSelector _ =
type ByJQuery (selector) =
inherit OpenQA.Selenium.By()

do
let findElements (context : ISearchContext) =
loadJQuery()
if context :? IWebDriver
then
let script = sprintf """return jQuery("%s").get();""" selector
(browser :?> IJavaScriptExecutor).ExecuteScript(script) :?> ReadOnlyCollection<IWebElement>
else
let script = sprintf """return jQuery("%s", arguments[0]).get();""" selector
let wrapper = context :?> OpenQA.Selenium.Internal.IWrapsDriver
(wrapper.WrappedDriver :?> IJavaScriptExecutor).ExecuteScript(script, wrapper) :?> ReadOnlyCollection<IWebElement>

base.FindElementsMethod <- fun context -> findElements context

base.FindElementMethod <- fun context -> findElements context |> Seq.head

let findByJQuery jquerySelector f =
try
loadJQuery()
let script = sprintf """return jQuery("%s").get();""" cssSelector
(browser :?> IJavaScriptExecutor).ExecuteScript(script) :?> ReadOnlyCollection<IWebElement> |> List.ofSeq
with | ex -> []
f(ByJQuery(jquerySelector) :> By) |> List.ofSeq
with | _ -> []

//you can use this as an example to how to extend canopy by creating your own set of finders, tweaking the current collection, or adding/removing
let mutable defaultFinders =
Expand Down
8 changes: 8 additions & 0 deletions tests/basictests/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ test (fun _ ->
count ".item" 5
true === (element "span" |> someElementWithin ".specialItem").IsSome

"element within works with jquery selectors" &&& fun _ ->
url "http://lefthandedgoat.github.io/canopy/testpages/elementWithin"
"spanned item 6" === (element ".specialItem:visible" |> parent |> elementWithin ".specialItem:visible").Text

"element within works with jquery selectors and respects scope" &&& fun _ ->
url "http://lefthandedgoat.github.io/canopy/testpages/elementWithin"
false === (element ".specialItem:visible" |> someElementWithin ".specialItem:visible").IsSome

"parent of firstItem and secondItem is list" &&& fun _ ->
url "http://lefthandedgoat.github.io/canopy/testpages/parent"
"list" === (element "#firstItem" |> parent).GetAttribute("id")
Expand Down

0 comments on commit 0b92e44

Please sign in to comment.