Skip to content

Instantly share code, notes, and snippets.

@paulirwin
Created June 4, 2015 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulirwin/2185ddd0a855ea5470eb to your computer and use it in GitHub Desktop.
Save paulirwin/2185ddd0a855ea5470eb to your computer and use it in GitHub Desktop.
Smoke Testing in F# with Canopy
module harness
open canopy
open canopy.types
open runner
open System
let mutable baseurl = "http://localhost:49774"
let login username password =
url baseurl
on (baseurl + "/Login")
// set username and password and login
"#Email" << username
"#Password" << password
press enter
on (baseurl + "/")
let urlverify u =
sleep 1
url u
on u
let h1 = someElement "h1";
match h1 with
| Some(h) ->
let hasError = h.Text.ToLower().Contains("server error in");
match hasError with
| true -> raise (CanopyException("server error"))
| false -> ()
| None -> ()
let t = title().Trim().ToLower()
match t with
| "compilation error" -> raise (CanopyException("compilation error"))
| "the resource cannot be found." -> raise (CanopyException("404"))
| _ -> ()
open canopy
open runner
open harness
open configuration
open reporters
open System
Console.Write("Base URL (default is http://localhost:49774): ")
let baseUrlInput = Console.ReadLine()
match baseUrlInput with
| "" -> ()
| _ -> harness.baseurl <- baseUrlInput
Console.Write("Username: ")
let username = Console.ReadLine()
Console.Write("Password: ")
let password = Console.ReadLine()
reporter <- new LiveHtmlReporter(types.BrowserStartMode.Chrome, "c:\\") :> IReporter
start chrome
login username password
SmokeTests.smoketest()
run()
Console.WriteLine("press [enter] to exit")
Console.ReadLine() |> ignore
quit()
module SmokeTests
open canopy
open runner
open harness
let urls =
[
"/Page1";
"/Page2";
"/Page3";
]
let smoketest() =
context "Smoke Tests"
for u in urls do
("Smoke test for " + u) &&& fun _ ->
urlverify (baseurl + u)
@paulirwin
Copy link
Author

To use, change the default baseurl, and add any relative URLs you want tested to the urls list in SmokeTests.fs (or, pull them from a database, or whatever). If you don't need authentication, just comment out the username/password input lines and the "login username password" line from Program.fs.

@paulirwin
Copy link
Author

Also make sure to put the F# files in this order: Harness.fs, SmokeTests.fs, then Program.fs. And of course add a nuget reference to Canopy. You'll need the Selenium Chrome driver installed in C:\ as that's the default location that Selenium looks for it. Feel free to customize from here to change driver location, use other browsers, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment