Running canopy tests with FAKE

FAKE can be used to run a variety of different testing frameworks. In this tutorial we are looking at Canopy support.

alt text

Setup your canopy project

Consider a simple canopy program.fs file:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
#r "canopy.dll"

open canopy
open runner
open System

//overwrite default path
canopy.configuration.phantomJSDir <- @".\"

//start an instance of the browser
start phantomJS

"taking canopy for a spin" &&& fun _ ->

    //go to url
    url "http://localhost:81/canopy/testpages/"

    //assert that the element with an id of 'welcome' has the text 'Welcome'
    "#welcome" == "Welcome"

//run all tests
run()

quit()

Although Selenium (which is the framework behind canopy) supports all browsers you might want to run your tests with the headless browser PhantomJS. To grab the latest version just install the NuGet package:

1: 
install-package PhantomJS

Normally canopy loads PhantomJS.exe from C:\ but in our case we want to use the installed one so we have to override the path in our test script and set the current path as location of PhantomJS.exe.

Run canopy tests in FAKE

The target in FAKE basically hosts the website in IIS Express and starts the canopy tests. IIS Express requires a configuration template ("iisexpress-template.config") which can be copied from %ProgramFiles%\IIS Express\AppServer\applicationhost.config.

This sample target will require the FAKE.IIS package to be installed and referenced in your script, though the package isn't required to run Canopy tests.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
Target "CanopyTests" (fun _ ->
	let hostName = "localhost"
	let port = 81

    let config = createConfigFile(project, 1, "iisexpress-template.config", websiteDir + "/" + project, hostName, port)
    let webSiteProcess = HostWebsite id config 1

    let result =
        ExecProcess (fun info ->
            info.FileName <- (buildDir @@ "CanopyTests.exe")
            info.WorkingDirectory <- buildDir
        ) (System.TimeSpan.FromMinutes 5.)

    ProcessHelper.killProcessById webSiteProcess.Id
 
    if result <> 0 then failwith "Failed result from canopy tests"
)

Please note that HostWebsite starts the IIS Express process asynchronous and does NOT wait until the IIS Express successfully started. Issue #403

namespace System
val id : x:'T -> 'T
Multiple items
type TimeSpan =
  struct
    new : ticks:int64 -> TimeSpan + 3 overloads
    member Add : ts:TimeSpan -> TimeSpan
    member CompareTo : value:obj -> int + 1 overload
    member Days : int
    member Divide : divisor:float -> TimeSpan + 1 overload
    member Duration : unit -> TimeSpan
    member Equals : value:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member Hours : int
    member Milliseconds : int
    ...
  end

--------------------
TimeSpan ()
TimeSpan(ticks: int64) : TimeSpan
TimeSpan(hours: int, minutes: int, seconds: int) : TimeSpan
TimeSpan(days: int, hours: int, minutes: int, seconds: int) : TimeSpan
TimeSpan(days: int, hours: int, minutes: int, seconds: int, milliseconds: int) : TimeSpan
TimeSpan.FromMinutes(value: float) : TimeSpan
val failwith : message:string -> 'T