Why Is Canopy Stabilization Layer Built on Top of Selenium?

One of the most crucial concepts of Canopy is reliability - when performing an action the framework tries, during the time span specified via elementTimeout or compareTimeout or pageTimeout, before failing. This improves the experience of writing tests.

Expressiveness

The syntax looks pretty self-explanatory:

  1. "Bio should contain twitter link" &&& fun _ ->
  2. url "https://github.com/Wkalmar"
  3. ".user-profile-bio" == "https://twitter.com/BohdanStupak1"
F#

In one of my previous articles, I have already expressed my opinion regarding power and expressiveness of F#.

Writing More Tests

To start, just create a console application, install nuget package canopy and create tests in Program.fs like below:

  1. open canopy.configuration
  2. open canopy.runner.classic
  3. open System
  4. open canopy.classic
  5. //set path for chrome direver explicitly
  6. chromeDir <- "C:\\"
  7. start chrome
  8. "Left bottom repostitory should be stationwalk.server" &&& fun _ ->
  9. //visit the following url
  10. url "https://github.com/Wkalmar"
  11. //get 4th child of the following selector
  12. let repo = nth 4 ".pinned-repo-item"
  13. //get element with the following selector inside repo element
  14. let firstRepoCaption = repo |> someElementWithin ".js-repo"
  15. match firstRepoCaption with
  16. | Some caption -> read caption == "stationwalk.server" //if found read element caption
  17. //and compare it
  18. | None _ -> failwith "Element not found" //if none element found throw an exception
  19. "Left bottom repostitory should be stationwalk.client" &&& fun _ ->
  20. url "https://github.com/Wkalmar"
  21. let repo = nth 5 ".pinned-repo-item"
  22. let firstRepoCaption = repo |> someElementWithin ".js-repo"
  23. match firstRepoCaption with
  24. | Some caption -> read caption == "stationwalk.client"
  25. | None _ -> failwith "Element not found"
  26. "Bio should contain twitter link" &&& fun _ ->
  27. url "https://github.com/Wkalmar"
  28. ".user-profile-bio" == "https://twitter.com/BohdanStupak1"
  29. run()
  30. printfn "Press any key to exit..."
  31. Console.ReadKey() |> ignore
  32. quit()
Accessing IWebDriver

If you've ever written tests with Selenium using C#, you might be aware of IWebDriver interface which you still might use for some advanced configuration. For example, let's say we want to run our tests with a browser opened fullscreen. Then we can add the following function to our Program.fs file

  1. let maximizeBrowser (browser : IWebDriver) =
  2. browser.Manage().Window.Maximize()
Accessing IWebElement

Most of canopy's assertions, i.e., == accept as a parameter either a string which can be css or xpath selector or instance of IWebElement type which again might be already familiar to you if you've ever written selenium tests using C#. So let's say we want to upload something into file upload control.

  1. let uploadFile fullFilePath =
  2. (element "input[type='file']").SendKeys(fullFilePath)
Splitting Up Big File

Patterns which I've practiced to keep test project maintainable include extracting selectors into page modules and moving tests to separate files.

Let's revisit our github example by moving out selectors into the separate module:

  1. module GithubProfilePage
  2. let pinnedRepository = ".pinned-repo-item"
  3. let bio = ".user-profile-bio"

Now we can reference them in the test which we'll move into a separate module too:

  1. module GithubProfileTests
  2. open canopy.runner.classic
  3. open canopy.classic
  4. let all() =
  5. context "Github page tests"
  6. "Left bottom repostitory should be staionwalk.server" &&& fun _ ->
  7. url "https://github.com/Wkalmar"
  8. let repo = nth 4 GithubProfilePage.pinnedRepository
  9. let firstRepoCaption = repo |> someElementWithin ".js-repo"
  10. match firstRepoCaption with
  11. | Some caption -> read caption == "stationwalk.server"
  12. | None _ -> failwith "Element not found"
  13. "Right bottom repostitory should be staionwalk.client" &&& fun _ ->
  14. url "https://github.com/Wkalmar"
  15. let repo = nth 5 GithubProfilePage.pinnedRepository
  16. let firstRepoCaption = repo |> someElementWithin ".js-repo"
  17. match firstRepoCaption with
  18. | Some caption -> read caption == "stationwalk.client"
  19. | None _ -> failwith "Element not found"
  20. "Bio should contain twitter link" &&& fun _ ->
  21. url "https://github.com/Wkalmar"
  22. GithubProfilePage.bio == "https://twitter.com/BohdanStupak1"

Our Program.fs will look like this:

  1. open canopy.configuration
  2. open canopy.runner.classic
  3. open System
  4. open canopy.classic
  5. chromeDir <- "C:\\"
  6. start chrome
  7. GithubProfileTests.all()
  8. run()
  9. printfn "Press any key to exit..."
  10. Console.ReadKey() |> ignore
  11. quit()
Running Test in Parallel

Recently, Canopy had a major upgrade from 1.x to 2.x and one of the great new features is the ability to run tests in parallel.

Let's revisit our example by using this ability:

  1. module GithubProfileTests
  2. open canopy.parallell.functions
  3. open canopy.types
  4. open prunner
  5. let all() =
  6. "Left bottom repostitory should be stationwalk.server" &&& fun _ ->
  7. let browser = start Chrome
  8. url "https://github.com/Wkalmar" browser
  9. let repo = nth 4 GithubProfilePage.pinnedRepository browser
  10. let firstRepoCaption = someElementWithin ".js-repo" repo browser
  11. match firstRepoCaption with
  12. | Some caption -> equals (read caption browser) "stationwalk.server" browser
  13. | None _ -> failwith "Element not found"
  14. "Right bottom repostitory should be stationwalk.client" &&& fun _ ->
  15. let browser = start Chrome
  16. url "https://github.com/Wkalmar" browser
  17. let repo = nth 5 GithubProfilePage.pinnedRepository browser
  18. let firstRepoCaption = someElementWithin ".js-repo" repo browser
  19. match firstRepoCaption with
  20. | Some caption -> equals (read caption browser) "stationwalk.client" browser
  21. | None _ -> failwith "Element not found"
  22. "Bio should contain twitter link" &&& fun _ ->
  23. let browser = start Chrome
  24. url "https://github.com/Wkalmar" browser
  25. equals GithubProfilePage.bio "https://twitter.com/BohdanStupak1" browser

The key trick to follow here is that each test operates now with its own copy of browser and assertions are now taken from open canopy.parallel.functions to accept browser as an argument. Also, please note the prunner dependency which can be taken from here.

Headless Testing

Testing in a headless browser seems to be the new black now. Although I don't share the sentiment, I still can assure you that testing in headless browsers is supported by Canopy. You can run your tests in headless Chrome as follows:

  1. let browser = start ChromeHeadless
Conclusion

I hope this article has convinced you that Canopy is a robust and easy to use framework which can be used in building end-to-end testing layers of your application.