From 0b92e4402f538cb5f7eba4b4a3bbf36606700cb4 Mon Sep 17 00:00:00 2001 From: Chris Holt Date: Tue, 19 Apr 2016 11:18:04 -0600 Subject: [PATCH] #257 should improve problem --- src/canopy/finders.fs | 29 ++++++++++++++++++++++------- tests/basictests/Program.fs | 8 ++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/canopy/finders.fs b/src/canopy/finders.fs index a9829363..d5cf574b 100644 --- a/src/canopy/finders.fs +++ b/src/canopy/finders.fs @@ -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 + 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 + + 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 |> 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 = diff --git a/tests/basictests/Program.fs b/tests/basictests/Program.fs index bd9a9d5b..353e9266 100644 --- a/tests/basictests/Program.fs +++ b/tests/basictests/Program.fs @@ -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")