Skip to content

Examples

Use these examples as a starting point for your own tests. Each test is a small wiki page plus a Lua file, as described in Write tests. More pages are in the wiki examples.

Every example uses the same wiki page. Change only the file name:

---
Test
---
|script |lua fixture |
|runFile;|Examples.Reflex.login-test |

login-test.lua

local browser = Sel.start() -- or Play.start()
browser.navigate("https://example.com/login")
browser.locator("id=username").fill("testuser")
browser.locator("id=password").fill("secret123")
browser.locator("xpath=//button[@type='submit']").click()
browser.locator("css=.dashboard").wait()
print("Welcome message: " .. browser.locator("css=.welcome-message").text())
browser.close()
return "Login test passed"

wait() retries until the element exists or the timeout passes. See Browser testing.

api-test.lua

local body = Json.encode({name = "John Doe", email = "john@example.com"})
local created = Rest.post("https://api.example.com/users", body, "Content-Type:application/json")
assert(created.status == 201, "Expected 201, got " .. created.status)
local response = Rest.get("https://api.example.com/users/1")
local user = Json.decode(response.body)
assert(user.name == "John Doe", "Name mismatch")
return "API test passed"

A failed assert fails the test and shows your message.

Run the same steps for every row of a spreadsheet. Row 1 holds the headers.

excel-test.lua

local workbook = Xlsx.open("files/testdata.xlsx")
local sheet = workbook.sheet("TestCases")
local browser = Sel.start()
for row = 2, sheet.lastRow() do
local username = sheet.getCell(row, 1)
local password = sheet.getCell(row, 2)
browser.navigate("https://example.com/login")
browser.locator("id=username").fill(username)
browser.locator("id=password").fill(password)
browser.locator("id=submit").click()
print("Row " .. row .. ": " .. browser.locator("css=.result").text())
end
browser.close()
workbook.close()
return "Data-driven test completed"

A failed browser action stops the script. To keep the loop running when one row fails, wrap the row in pcall.