From 519406f1080ed9b611dc7f436bdd94024d753799 Mon Sep 17 00:00:00 2001 From: lefthandedgoat Date: Sat, 7 Sep 2013 13:07:22 -0500 Subject: [PATCH] Fix for #105 Inconsisten behaviour between element and someElementWithin --- basictests/Program.fs | 14 ++++++++++++++ canopy/canopy.fs | 23 +++++++++++++++++------ canopy/configuration.fs | 3 ++- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/basictests/Program.fs b/basictests/Program.fs index 44d5ddd3..592a22d5 100644 --- a/basictests/Program.fs +++ b/basictests/Program.fs @@ -446,6 +446,19 @@ if not (browser :? OpenQA.Selenium.PhantomJS.PhantomJSDriver) then click "#confirmation_test" alert() == "Not the message") +context "multiple elements test" + +before (fun _ -> !^ "http://lefthandedgoat.github.io/canopy/testpages/") + +"no error with multiple elements" &&&& fun _ -> + read (element "input") === "test value 1" + +"error with multiple elements" &&&& fun _ -> + throwIfMoreThanOneElement <- true + failsWith "More than one element was selected when only one was expected for selector: input" + read (element "input") === "test value 1" + + context "tiling windows" "start multiple browsers and tile them" &&& (fun _ -> start chrome @@ -461,6 +474,7 @@ context "tiling windows" ) context "todo tests" + "write a test that tests the whole internet!" &&& todo run () diff --git a/canopy/canopy.fs b/canopy/canopy.fs index 1e99bf30..12fb5515 100644 --- a/canopy/canopy.fs +++ b/canopy/canopy.fs @@ -244,20 +244,31 @@ let private findMany cssSelector timeout (searchContext : ISearchContext) = findByFunction cssSelector timeout findElements searchContext //get elements + +let private elementFromList cssSelector elementsList = + match elementsList with + | [] -> null + | x :: [] -> x + | x :: y :: _ -> + if throwIfMoreThanOneElement then raise (CanopyMoreThanOneElementFoundException(sprintf "More than one element was selected when only one was expected for selector: %s" cssSelector)) + else x + let private someElementFromList cssSelector elementsList = match elementsList with - | [x] -> Some(x) - | [] -> None - | _ -> raise (CanopyMoreThanOneElementFoundException(sprintf "More than one element was selected when only one was expected for selector: %s" cssSelector)) + | [] -> None + | x :: [] -> Some(x) + | x :: y :: _ -> + if throwIfMoreThanOneElement then raise (CanopyMoreThanOneElementFoundException(sprintf "More than one element was selected when only one was expected for selector: %s" cssSelector)) + else Some(x) + +let elements cssSelector = findMany cssSelector elementTimeout browser -let element cssSelector = find cssSelector elementTimeout browser +let element cssSelector = cssSelector |> elements |> elementFromList cssSelector let elementWithin cssSelector (elem:IWebElement) = find cssSelector elementTimeout elem let parent elem = elem |> elementWithin ".." -let elements cssSelector = findMany cssSelector elementTimeout browser - let elementsWithin cssSelector elem = findMany cssSelector elementTimeout elem let someElement cssSelector = cssSelector |> elements |> someElementFromList cssSelector diff --git a/canopy/configuration.fs b/canopy/configuration.fs index 6bd5930c..5bbeb5af 100644 --- a/canopy/configuration.fs +++ b/canopy/configuration.fs @@ -14,4 +14,5 @@ let mutable wipSleep = 1.0 let mutable runFailedContextsFirst = false let mutable reporter : IReporter = new ConsoleReporter() :> IReporter let mutable disableSuggestOtherSelectors = false -let mutable autoPinBrowserRightOnLaunch = true \ No newline at end of file +let mutable autoPinBrowserRightOnLaunch = true +let mutable throwIfMoreThanOneElement = false \ No newline at end of file