diff --git a/basictests/Program.fs b/basictests/Program.fs index 16728ed7..b544cd64 100644 --- a/basictests/Program.fs +++ b/basictests/Program.fs @@ -1,6 +1,7 @@ module main open System +open OpenQA.Selenium open canopy open runner open configuration @@ -545,6 +546,13 @@ context "User Agents tests" context "Resize tests" +"Firefox should be resized to 400,400" &&& fun _ -> + start firefox + url "http://resizemybrowser.com/" + resize (400,400) + "#cWidth" == "400" + "#cHeight" == "400" + "Chrome should be resized to iPhone4" &&& fun _ -> start chrome url "http://resizemybrowser.com/" @@ -552,12 +560,13 @@ context "Resize tests" "#cWidth" == "320" "#cHeight" == "480" -"Firefox should be resized to 400,400" &&& fun _ -> +"Firefox should be resized to 400,500 then rotated to 500,400" &&& fun _ -> start firefox url "http://resizemybrowser.com/" - resize (400,400) - "#cWidth" == "400" + resize (400,500) + rotate() "#cHeight" == "400" + "#cWidth" == "500" "Chrome should be resized and rotated to iPhone4" &&& fun _ -> start chrome @@ -566,20 +575,28 @@ context "Resize tests" rotate() "#cHeight" == "320" "#cWidth" == "480" - + +context "pluggable finders tests" + +//example of a finder you could write so you didnt have to write boring selectors, just add this +//and let canopy do the work of trying to find something by convention +let findByHref href f = + try + let cssSelector = sprintf "a[href='%s']" href + f(By.CssSelector(cssSelector)) |> List.ofSeq + with | ex -> [] + +addFinder findByHref + "Firefox should be resized to 400,500 then rotated to 500,400" &&& fun _ -> - start firefox - url "http://resizemybrowser.com/" - resize (400,500) - rotate() - "#cHeight" == "400" - "#cWidth" == "500" + url "http://lefthandedgoat.github.io/canopy/index.html" + click "about.html" + on "http://lefthandedgoat.github.io/canopy/about.html" context "todo tests" "write a test that tests the whole internet!" &&& todo - run () switchTo mainBrowser @@ -587,4 +604,4 @@ coverage testpage coverage() coverage "http://scrumy.com/silenter39delayed" -quit() +quit() \ No newline at end of file diff --git a/canopy/canopy.fs b/canopy/canopy.fs index 2e50d669..2dd8eb78 100644 --- a/canopy/canopy.fs +++ b/canopy/canopy.fs @@ -13,6 +13,7 @@ open configuration open levenshtein open reporters open types +open finders let mutable (browser : IWebDriver) = null let mutable (failureMessage : string) = null @@ -110,7 +111,7 @@ let waitFor (f : unit -> bool) = let rec private findElements cssSelector (searchContext : ISearchContext) : IWebElement list = searchedFor <- (cssSelector, browser.Url) :: searchedFor let findInIFrame () = - let iframes = finders.findByCss "iframe" searchContext.FindElements + let iframes = findByCss "iframe" searchContext.FindElements if iframes.IsEmpty then browser.SwitchTo().DefaultContent() |> ignore [] @@ -123,10 +124,13 @@ let rec private findElements cssSelector (searchContext : ISearchContext) : IWeb !webElements try - configuredFinders cssSelector searchContext.FindElements - |> Seq.append (seq { yield (findInIFrame()) }) - |> Seq.filter(fun list -> not(list.IsEmpty)) - |> Seq.head + let results = + configuredFinders cssSelector searchContext.FindElements + |> Seq.filter(fun list -> not(list.IsEmpty)) + if Seq.isEmpty results then + findInIFrame() + else + results |> Seq.head with | ex -> [] let private findByFunction cssSelector timeout waitFunc searchContext = @@ -643,4 +647,10 @@ let coverage (url : 'a) = let p = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"canopy\") let f = DateTime.Now.ToString("MMM-d_HH-mm-ss-fff") let ss = screenshot p f - reporter.coverage nonMutableInnerUrl ss \ No newline at end of file + reporter.coverage nonMutableInnerUrl ss + +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 diff --git a/canopy/finders.fs b/canopy/finders.fs index f0f1cd25..c2e044c5 100644 --- a/canopy/finders.fs +++ b/canopy/finders.fs @@ -59,7 +59,6 @@ let findByValue value f = 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 -//(string -> (By -> seq) -> seq) let mutable defaultFinders = (fun cssSelector f -> seq { @@ -71,4 +70,4 @@ let mutable defaultFinders = yield findBySizzle cssSelector f yield findByJQuery cssSelector f } - ) + ) \ No newline at end of file