Upload CSV Data

Introduction

Often times a scenario will require different parameters for each iteration of a test (e.g. login/password credentials). Testable provides a simple way to upload a CSV data file and use it while writing a Node.js script or a PhantomJS/SlimerJS script.

Getting Started

Start by create a new test case using the Create Test button on the dashboard or Test Case -> New… in the left side panel.

Enter the test case name Next.

Select Node.js Script as the Scenario type and press Next again.

Step 2

Expand on the Files block and press the Add/Upload Files button.

Step 3

Now let’s upload our data file. We will use the following in this example:

SYMBOL,DESCRIPTION
IBM,Company 1 for demo
MSFT,Company 2 for demo
AAPL,Company 3 for demo

Step 4

The upload modal will provide a preview of the data and if everything looks good press the Looks Good! button.

Step 5

Looks great! Time to use it in our script. Click on the Code tab of the Script and enter the following code:

const rows = await dataTable.open('demo.csv').next();
http.get('http://sample.testable.io/stocks/' + rows[0].data['SYMBOL']);

In this example we get the next row available from the demo.csv data table and access the SYMBOL column. See the section below for more details on using your data.

Click the Run button in the upper right and notice that the results correspond to GET http://sample.testable.io/stocks/IBM, the symbol in the first row of our demo csv.

Now press Next again and go ahead and configure the test and run it similar to the getting started guide.

For detailed information about the data API continue reading.

API

The dataTable module provides the script all functionality related to accessing the rows of an uploaded CSV.

const dataTable = require('testable-utils').dataTable;
open(name)

Open a data table to get started by using its name. The name can be changed by clicking on the test case, going to the Data tab and pressing the Edit Name button for the data table in question.

const dt = dataTable.open('demo.csv');
dataTable

The object returned from dataTable.open(name) has the following properties:

  1. headers: An array of the CSV’s headers.
  2. details: Info about the data file. Has the following properties: id, name, createdAt, updatedAt, numRows, type, charset, size, url, thumbnailUrl, and deleteUrl.
dataTable.get(rowIndex)

Get the row at rowIndex and return a Promise. Row indices start at 1.

The following example gets the 3rd row in the file:

const row = await dataTable.open('demo.csv').get(i);
log.info(row.data['SYMBOL']);
dataTable.random()

Get a randomly chosen row and return a Promise.

const row = await dataTable.open('demo.csv').random();
log.info(row.data['SYMBOL']);
dataTable.next([options])

Returns a Promise. Iterates through the table and passes the next X rows as an array. There is one global iterator per test execution. Each call to this method will receive unique rows back. This is useful, for example, if you wanted to use unique login credentials per test iteration.

The following options are available:

  1. wrap: By default the iterator will wrap back around to the first row once it reaches the last row during test execution. To prevent this and fail instead, set this option to false.
  2. rows: The number of rows to return. Defaults to 1.
const rows = await dataTable.open('demo.csv').next({ wrap: false, rows: 2 });
log.info(rows[0].data['SYMBOL']);
row

The rows provided by the above dataTable methods have the following properties:

  1. data: An object of header -> row value mappings. row.data['SYMBOL'] returns the value in this row for the SYMBOL column or undefined if no column with that name is found.
  2. index: The row index within the CSV file. Indices are 1 based.
  3. indexed: An array of values for this row if you want to access them by index (0 based in this case). For example if SYMBOL was the 2nd column you could access this row’s symbol using row.indexes[1].