Skip to content

TEST.loadDataFromCsv()

Loads data from a CSV file.

This is commonly used to externalize tabular test data (e.g. users, locales, configurations) so tests remain clean and reusable.

The file path can be:

  • relative to the current test file, or
  • absolute from the project root

Usage

js
TEST.loadDataFromCsv(filepath: string, options?: object)
TEST.loadDataFromCsv(filepath: string, dataset: string, options?: object)

Parameters

ParameterTypeDescription
filepathstringRelative or absolute path to the file to load
datasetstring(optional) Expression for selecting the dataset to load. e.g. role=admin
optionsobject(optional) Parsing options. See below.

Options

OptionTypeDescription
headerstringSet to row if the first row contains headers (used as property names), or col if the first column contains property names. Defaults to col.
datasetstring(optional) Expression for selecting dataset to load, e.g. role=admin
escapestringEscape character used inside quoted fields. Defaults to \".
delimiterstringDelimiter character. Defaults to , (comma).

Returns

Returns:

  • an array of JavaScript objects, if dataset is not specified
  • a JavaScript object, if dataset is specified

Examples

Given a data file data/users.csv with the following contents:

csv
role,username,password
admin,johndoe,"super secret password"
member,jane,"qwerty123"

To load all entries from the file as an array in the test:

js
var users = TEST.loadDataFromCsv("data/users.csv", {header: "row"})
// Result:
//   users = [
//     {"role": "admin", "username" : "johndoe", "password" : "super secret password"},
//     {"role": "member", "username" : "jane", "password" : "qwerty123"}
//   ]
//

// login with the first user in the list
I.goTo("https://example.com")
I.fill("Username", users[0].username)
I.fill("Password", users[0].password)
I.click("Log In")

To load a single entry from the file as a JavaScript object, use the dataset parameter:

js
// load the entry where the value of "role" equals "admin"
var admin = TEST.loadDataFromCsv("data/users.csv", "role=admin", {header: "row"})
// Result:
//   admin = {"role": "admin", "username" : "johndoe", "password" : "super secret password"}
//

I.goTo("https://example.com")
I.fill("Username", admin.username)
I.fill("Password", admin.password)
I.click("Log In")