Skip to content

Commit

Permalink
Merge pull request #164 from lefthandedgoat/performance-163-2
Browse files Browse the repository at this point in the history
Performance #163 2
  • Loading branch information
lefthandedgoat committed Sep 8, 2014
2 parents d2365c5 + 945a8f3 commit f3ef882
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
36 changes: 31 additions & 5 deletions src/canopy/canopy.fs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ let waitFor = waitFor2 "Condition not met in given amount of time. If you want t

//find related
let rec private findElements cssSelector (searchContext : ISearchContext) : IWebElement list =
searchedFor <- (cssSelector, browser.Url) :: searchedFor
if optimizeByDisablingCoverageReport = false then searchedFor <- (cssSelector, browser.Url) :: searchedFor
let findInIFrame () =
let iframes = findByCss "iframe" searchContext.FindElements
if iframes.IsEmpty then
Expand All @@ -210,10 +210,16 @@ let rec private findElements cssSelector (searchContext : ISearchContext) : IWeb

try
let results =
configuredFinders cssSelector searchContext.FindElements
|> Seq.filter(fun list -> not(list.IsEmpty))
if (hints.ContainsKey cssSelector) then
let finders = hints.[cssSelector]
finders
|> Seq.map (fun finder -> finder cssSelector searchContext.FindElements)
|> Seq.filter(fun list -> not(list.IsEmpty))
else
configuredFinders cssSelector searchContext.FindElements
|> Seq.filter(fun list -> not(list.IsEmpty))
if Seq.isEmpty results then
findInIFrame()
if optimizeBySkippingIFrameCheck then [] else findInIFrame()
else
results |> Seq.head
with | ex -> []
Expand Down Expand Up @@ -771,4 +777,24 @@ let addFinder finder =
let currentFinders = configuredFinders
configuredFinders <- (fun cssSelector f ->
currentFinders cssSelector f
|> Seq.append (seq { yield finder cssSelector f }))
|> Seq.append (seq { yield finder cssSelector f }))

//hints
let private addHintFinder hints finder = hints |> Seq.append (seq { yield finder })
let private addSelector finder hintType selector =
//gaurd against adding same hintType multipe times and increase size of finder seq
if not <| (hints.ContainsKey(selector) && addedHints.[selector] |> List.exists (fun hint -> hint = hintType)) then
if hints.ContainsKey(selector) then
hints.[selector] <- addHintFinder hints.[selector] finder
addedHints.[selector] <- [hintType] @ addedHints.[selector]
else
hints.[selector] <- seq { yield finder }
addedHints.[selector] <- [hintType]
selector

let css = addSelector findByCss "css"
let xpath = addSelector findByXpath "xpath"
let jquery = addSelector findByJQuery "jquery"
let label = addSelector findByLabel "label"
let text = addSelector findByText "text"
let value = addSelector findByValue "value"
4 changes: 3 additions & 1 deletion src/canopy/configuration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ let mutable disableSuggestOtherSelectors = false
let mutable autoPinBrowserRightOnLaunch = true
let mutable throwIfMoreThanOneElement = false
let mutable configuredFinders = finders.defaultFinders
let mutable writeToSelectWithOptionValue = true
let mutable writeToSelectWithOptionValue = true
let mutable optimizeBySkippingIFrameCheck = false
let mutable optimizeByDisablingCoverageReport = false
5 changes: 4 additions & 1 deletion src/canopy/finders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@ let mutable defaultFinders =
yield findByText cssSelector f
yield findByJQuery cssSelector f
}
)
)

let addedHints = System.Collections.Generic.Dictionary<string, string list>()
let hints = new System.Collections.Generic.Dictionary<string, seq<(string -> (By -> System.Collections.ObjectModel.ReadOnlyCollection<IWebElement>) -> IWebElement list)>>()
27 changes: 27 additions & 0 deletions tests/basictests/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,33 @@ context "other tests"
url "http://lefthandedgoat.github.io/canopy/testpages/iframe2"
first "body" |> elementWithin "#states" |> elementWithin "1" |> read |> is "Alabama"

context "hints tests"
"css hint" &&& fun _ ->
url testpage
let firstName = css "#firstName"
firstName == "John"
css "#lastName" == "Doe"

"xpath hint" &&& fun _ ->
url testpage
let firstName = xpath "id('firstName')"
firstName == "John"
xpath "id('lastName')" == "Doe"

"jquery hint" &&& fun _ ->
url testpage
let firstName = jquery "#firstName"
firstName == "John"
jquery "#lastName" == "Doe"

"value hint" &&& fun _ ->
url testpage
value "Click Me!!" == "Click Me!!"

"text hint" &&& fun _ ->
url testpage
text "ajax button not clicked" == "ajax button not clicked"

context "dragging"
"draging works" &&& fun _ ->
url "http://scrumy.com/silenter39delayed"
Expand Down

0 comments on commit f3ef882

Please sign in to comment.