Script examples
This page gives copy-and-adapt examples for common tasks. Credentials always come from secrets.
Fill an Excel report
Section titled “Fill an Excel report”Xlsx edits an existing workbook, so start from a template in the project. Columns come before rows, both from 1.
local workbook = Xlsx.open("files/template.xlsx")local sheet = workbook.sheet("Sheet1")sheet.setCell(1, 2, "Orders")sheet.setCell(2, 2, Dates.today("yyyy-MM-dd"))workbook.save("report.xlsx")workbook.close()The saved file comes back as a run result. See Xlsx.
Check a REST API
Section titled “Check a REST API”HTTP error statuses are returned, not raised, so assert on them.
Rest.bearer(secret("api_token"))local response = Rest.get("https://api.example.com/orders", { Accept = "application/json" })Assert.equals(200, response.status)
local orders = Json.decode(response.body)Assert.that(#orders > 0, "no orders returned")To send JSON, encode it yourself and set the header:
Rest.post("https://api.example.com/orders", { ["Content-Type"] = "application/json" }, Json.encode({ sku = "A-100", quantity = 2 }))Send a mail
Section titled “Send a mail”The body is HTML. Pass true as the last argument to attach the step log, handy for failure notices.
local smtp = Mail.properties({ host = "smtp.example.com", port = 587, starttls = true, auth = true, user = "bot@example.com", password = secret("mail_password"),})smtp.send("team@example.com", "Nightly report", "<p>The report is ready.</p>", true)Reading a mailbox works with Mail.imap and mailbox.poll: see Mail.
Download files over SFTP
Section titled “Download files over SFTP”local sftp = Sftp.newSession("report-user", secret("sftp_password"), "sftp.example.com")for _, name in ipairs(sftp.listFiles("outgoing")) do if name:match("%.csv$") then sftp.downloadFileTo("/outgoing/" .. name, "input/" .. name) endendsftp.close()Upload with sftp.uploadFileTo(localFile, remoteFile). For key login pass "" as the password and call sftp.setPrivateKey(path). See Sftp.
Log in to a web app
Section titled “Log in to a web app”local browser = Sel.start()browser.navigate("https://example.com/login")browser.locator("id=username").fill("testuser")browser.locator("id=password").fill(secret("app_password"))browser.locator("xpath=//button[@type='submit']").click()browser.locator("css=.dashboard").wait()Sel.close()Selectors, shadow DOM and locators.yaml are in browser testing.
Every function has its own page in the library reference.