From 47641ff97db9106c8de9c24050eea032e7286b8f Mon Sep 17 00:00:00 2001 From: Chris Holt Date: Mon, 1 Sep 2014 18:29:22 -0500 Subject: [PATCH 1/3] Performance - 3 optimizations --- src/canopy/canopy.fs | 22 +++++++++++++++++----- src/canopy/configuration.fs | 4 +++- src/canopy/finders.fs | 4 +++- tests/basictests/Program.fs | 27 +++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/canopy/canopy.fs b/src/canopy/canopy.fs index 99318bdb..97816285 100644 --- a/src/canopy/canopy.fs +++ b/src/canopy/canopy.fs @@ -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 optmizeByDisablingCoverageReport = false then searchedFor <- (cssSelector, browser.Url) :: searchedFor let findInIFrame () = let iframes = findByCss "iframe" searchContext.FindElements if iframes.IsEmpty then @@ -210,10 +210,14 @@ 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 finder = hints.[cssSelector] + seq { yield finder cssSelector searchContext.FindElements } + 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 -> [] @@ -771,4 +775,12 @@ let addFinder finder = let currentFinders = configuredFinders configuredFinders <- (fun cssSelector f -> currentFinders cssSelector f - |> Seq.append (seq { yield finder cssSelector f })) \ No newline at end of file + |> Seq.append (seq { yield finder cssSelector f })) + +//hints +let css selector = hints.[selector] <- findByCss; selector +let xpath selector = hints.[selector] <- findByXpath; selector +let jquery selector = hints.[selector] <- findByJQuery; selector +let label selector = hints.[selector] <- findByLabel; selector +let text selector = hints.[selector] <- findByText; selector +let value selector = hints.[selector] <- findByValue; selector \ No newline at end of file diff --git a/src/canopy/configuration.fs b/src/canopy/configuration.fs index bad57482..70e64e7f 100644 --- a/src/canopy/configuration.fs +++ b/src/canopy/configuration.fs @@ -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 \ No newline at end of file +let mutable writeToSelectWithOptionValue = true +let mutable optimizeBySkippingIFrameCheck = false +let mutable optmizeByDisablingCoverageReport = false \ No newline at end of file diff --git a/src/canopy/finders.fs b/src/canopy/finders.fs index 05303447..27932431 100644 --- a/src/canopy/finders.fs +++ b/src/canopy/finders.fs @@ -64,4 +64,6 @@ let mutable defaultFinders = yield findByText cssSelector f yield findByJQuery cssSelector f } - ) \ No newline at end of file + ) + +let hints = new System.Collections.Generic.Dictionary (By -> System.Collections.ObjectModel.ReadOnlyCollection) -> IWebElement list)>() \ No newline at end of file diff --git a/tests/basictests/Program.fs b/tests/basictests/Program.fs index ce4294c6..200b923e 100644 --- a/tests/basictests/Program.fs +++ b/tests/basictests/Program.fs @@ -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" From bf6efe4b15917d3aa7d8eb094d3c0cbc868ea325 Mon Sep 17 00:00:00 2001 From: Chris Holt Date: Sun, 7 Sep 2014 13:40:23 -0500 Subject: [PATCH 2/3] Performance feedback for Sergey #163 --- src/canopy/canopy.fs | 32 +++++++++++++++++++++++--------- src/canopy/configuration.fs | 2 +- src/canopy/finders.fs | 3 ++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/canopy/canopy.fs b/src/canopy/canopy.fs index 97816285..d75dc2ad 100644 --- a/src/canopy/canopy.fs +++ b/src/canopy/canopy.fs @@ -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 = - if optmizeByDisablingCoverageReport = false then 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 @@ -211,8 +211,10 @@ let rec private findElements cssSelector (searchContext : ISearchContext) : IWeb try let results = if (hints.ContainsKey cssSelector) then - let finder = hints.[cssSelector] - seq { yield finder cssSelector searchContext.FindElements } + 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)) @@ -778,9 +780,21 @@ let addFinder finder = |> Seq.append (seq { yield finder cssSelector f })) //hints -let css selector = hints.[selector] <- findByCss; selector -let xpath selector = hints.[selector] <- findByXpath; selector -let jquery selector = hints.[selector] <- findByJQuery; selector -let label selector = hints.[selector] <- findByLabel; selector -let text selector = hints.[selector] <- findByText; selector -let value selector = hints.[selector] <- findByValue; selector \ No newline at end of file +let private addHintFinder hints finder = hints |> Seq.append (seq { yield finder }) +let private addSelector finder selector hintType = + //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" \ No newline at end of file diff --git a/src/canopy/configuration.fs b/src/canopy/configuration.fs index 70e64e7f..3793b0ef 100644 --- a/src/canopy/configuration.fs +++ b/src/canopy/configuration.fs @@ -21,4 +21,4 @@ let mutable throwIfMoreThanOneElement = false let mutable configuredFinders = finders.defaultFinders let mutable writeToSelectWithOptionValue = true let mutable optimizeBySkippingIFrameCheck = false -let mutable optmizeByDisablingCoverageReport = false \ No newline at end of file +let mutable optimizeByDisablingCoverageReport = false \ No newline at end of file diff --git a/src/canopy/finders.fs b/src/canopy/finders.fs index 27932431..09c5bc39 100644 --- a/src/canopy/finders.fs +++ b/src/canopy/finders.fs @@ -66,4 +66,5 @@ let mutable defaultFinders = } ) -let hints = new System.Collections.Generic.Dictionary (By -> System.Collections.ObjectModel.ReadOnlyCollection) -> IWebElement list)>() \ No newline at end of file +let addedHints = System.Collections.Generic.Dictionary() +let hints = new System.Collections.Generic.Dictionary (By -> System.Collections.ObjectModel.ReadOnlyCollection) -> IWebElement list)>>() \ No newline at end of file From 945a8f3f874ecf255f15304fe0f52f2b35940685 Mon Sep 17 00:00:00 2001 From: Chris Holt Date: Sun, 7 Sep 2014 13:49:09 -0500 Subject: [PATCH 3/3] bug fix --- src/canopy/canopy.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/canopy/canopy.fs b/src/canopy/canopy.fs index d75dc2ad..9956e680 100644 --- a/src/canopy/canopy.fs +++ b/src/canopy/canopy.fs @@ -781,7 +781,7 @@ let addFinder finder = //hints let private addHintFinder hints finder = hints |> Seq.append (seq { yield finder }) -let private addSelector finder selector hintType = +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