Introduction:
Canopy is a powerful web automation testing tool designed for F# developers. Its concise syntax simplifies the process of automating web testing. In this blog post, we'll explore some code examples to showcase the ease and efficiency of Canopy.
Documentation - http://lefthandedgoat.github.io/canopy/.
GitHub - https://github.com/lefthandedgoat/canopy
Getting started kit - https://github.com/lefthandedgoat/canopyStarterKit/.
Real world examples - https://github.com/lefthandedgoat/turtletestAutomation
Getting started
To get started just add Canopy and Selenium.WebDriver.ChromeDriver to your project. You will also need Chrome installed.
Examples
Let's start with a simple test that opens up the test page.
open canopy.runner.classic
open canopy.configuration
open canopy.classic
canopy.configuration.chromeDir <- System.AppContext.BaseDirectory
start chrome
//this is how you define a test
"open test page" &&& fun _ ->
//go to url
url "http://lefthandedgoat.github.io/canopy/testpages/"
//assert that the browser is currently on a url
on "http://lefthandedgoat.github.io/canopy/testpages/"
//run all tests
run()
printfn "press [enter] to exit"
System.Console.ReadLine() |> ignore
quit()
Now let's add some more tests to showcase the other features of Canopy.
open canopy.runner.classic
open canopy.configuration
open canopy.classic
canopy.configuration.chromeDir <- System.AppContext.BaseDirectory
//this is how you define a test
"open test page" &&& fun _ ->
start chrome
//go to url
url "http://lefthandedgoat.github.io/canopy/testpages/"
//assert that the browser is currently on a url
on "http://lefthandedgoat.github.io/canopy/testpages/"
quit()
"set firstname" &&& fun _ ->
start chrome
url "http://lefthandedgoat.github.io/canopy/testpages/"
//set the value to "Jim"
"#firstName" << "Jim"
//verify the value was set
"#firstName" == "Jim"
quit()
"click button" &&& fun _ ->
start chrome
url "http://lefthandedgoat.github.io/canopy/testpages/"
//click button
click "#button"
//verify that the text below the button has changed
"#button_clicked" == "button clicked"
quit()
//run all tests
run()
printfn "press [enter] to exit"
System.Console.ReadLine() |> ignore
In a real-world testing scenario we might want to perform an action before and after each test (setup/cleanup), so let's refactor our test to do that.
open canopy.runner.classic
open canopy.configuration
open canopy.classic
canopy.configuration.chromeDir <- System.AppContext.BaseDirectory
//will run before every test
before (fun () ->
start chrome
url "http://lefthandedgoat.github.io/canopy/testpages/"
)
//will run after every test
after (fun () ->
quit()
)
//this is how you define a test
"open test page" &&& fun _ ->
//assert that the browser is currently on a url
on "http://lefthandedgoat.github.io/canopy/testpages/"
"set firstname" &&& fun _ ->
//set the value to "Jim"
"#firstName" << "Jim"
//verify the value was set
"#firstName" == "Jim"
"click button" &&& fun _ ->
//click button
click "#button"
//verify that the text below the button has changed
"#button_clicked" == "button clicked"
//run all tests
run()
printfn "press [enter] to exit"
System.Console.ReadLine() |> ignore
We can also run things once before all of the tests and once after all of the tests with once and lastly.
open canopy.runner.classic
open canopy.configuration
open canopy.classic
canopy.configuration.chromeDir <- System.AppContext.BaseDirectory
//will run once before all of the tests
once (fun () ->
start chrome
url "http://lefthandedgoat.github.io/canopy/testpages/"
)
//will run once after all of the tests
lastly (fun () ->
quit()
)
//this is how you define a test
"open test page" &&& fun _ ->
//assert that the browser is currently on a url
on "http://lefthandedgoat.github.io/canopy/testpages/"
"set firstname" &&& fun _ ->
//set the value to "Jim"
"#firstName" << "Jim"
//verify the value was set
"#firstName" == "Jim"
"click button" &&& fun _ ->
//click button
click "#button"
//verify that the text below the button has changed
"#button_clicked" == "button clicked"
//run all tests
run()
printfn "press [enter] to exit"
System.Console.ReadLine() |> ignore
Additional mentions
Fast selectors
Canopy provides a bunch of easy-to-use selectors.
css ".name"
xpath "//div/span"
jquery ".name:first"
label "First Name"
text "Last Name"
value "Submit"
Multiple browsers
You can also open multiple browsers.
start firefox
let mainBrowser = browser
start chrome
let secondBrowser = browser
//switch back to mainBrowser after opening secondBrowser
switchTo mainBrowser
Conclusion
Canopy is a valuable tool for automating web testing in F#. Whether you're interacting with web elements, asserting text, or navigating between pages - Canopy simplifies the testing process.