Skip to content

Write tests

Use this page to write a FitNesse test. Keep the wiki page small and put the logic in a Lua file.

Section titled “Recommended: a wiki page that runs a Lua file”

Create a .wiki page and a .lua file with the same name.

mytest.wiki

---
Test
---
|script |lua fixture |
|runFile;|Examples.Reflex.mytest |

mytest.lua

local browser = Sel.start()
browser.navigate("https://example.com")
browser.locator("id=submit").click()
browser.close()
return "Test passed"

The path uses dots for folders: Examples.Reflex.mytest is FitNesseRoot/Examples/Reflex/mytest.lua. All Reflex libraries are available, exactly as in any script. A failed assert or error fails the test.

Put helpers in a module and load it with require:

-- helpers.lua
local M = {}
function M.login(browser, user, pass)
browser.locator("id=username").fill(user)
browser.locator("id=password").fill(pass)
browser.locator("id=submit").click()
end
return M
-- mytest.lua
local helpers = require("Examples/Reflex/helpers")
local browser = Sel.start()
browser.navigate("https://example.com/login")
helpers.login(browser, "testuser", "secret123")
browser.close()

runFile; and run; share one Lua context, so the wiki can read what the file set:

|script |lua fixture |
|runFile;|Examples.Reflex.mytest |
|$result=|run; |return someVariable|

Use run;, set; and get; for one-liners and to pass values between the wiki and Lua:

|script|lua fixture |
|set; |myVar |$wikiVar |
|run; |print(myVar) |
|run; |result = "computed"|
|$val= |get; |result |

lua fixture has these checks:

|script |lua fixture |
|isEqual; |$actual |expected |
|notEqual; |$val1 |$val2 |
|isTrue; |$condition |
|contains; |$text |substring|
|startsWith; |$text |prefix |
|matchesRegex;|$text |pattern.*|

Fixtures call library functions straight from a table. Use them for quick checks. The fixture name is the library name plus fixture, for example sel fixture, browser fixture, rest fixture. Fixture reference lists every method.

Start a browser and pass it to browser fixture:

|script |sel fixture|
|$browser=|start; |
|script |browser fixture|$browser |
|navigate;|!-https://example.com/login-! |
|fill; |id=username |testuser |
|click; |xpath=//button[@type='submit'] |
|wait; |id=dashboard |5000 |
|$text= |text; |css=.welcome-message |
|close; | |

Selectors are the same as in Lua: see Browser testing. The optional second argument of wait; is a timeout in milliseconds.

Call a REST API:

|script |rest fixture |
|$response=|get; |!-https://api.example.com/users-! |
|show |status;|$response |

Variables that start with $ last for the whole test page.

By default a test stops at the first failed step. To keep going and mark only the failed step:

|script |conf fixture|
|stopOnException;|false |