Data Sources

Many tests require special test data. We provide multiple options to extend your otherwise static tests using data sources:

  1. File Fixtures (structured and raw)
  2. Random Numbers
  3. Random Strings

You can define and access data sources inside your sessions using functions exposed under session.dataSources or session.ds. The following examples will use the shorthand form for brevity.

Random Numbers

random_number will generate a uniformly distributed number within a given range:

// Step1: Define the random number generator
var productIds = session.ds.define("random_number", {
  range: [1000, 2000],
  name: "productIds", // Optional identifier for later debugging usage
});

// Step2: Generate and use number in request
session.post("/order", {
  payload: {
    productId: session.ds.generateFrom(productIds)
  }
});

There is also an equivalent shorthand-version for one-time generators:

session.post("/order", {
  payload: {
    productId: session.ds.generate("random_number", { range: [0, 999] })
  }
});

Random String

random_string will generate a random alpha-numeric string with the specified length. This can be useful if you need some kind of token:

// Step1: Define the random string generator
var token = session.ds.define("random_string", {
  length: 42,
});

// Step2: Generate and use string in request
session.post("/submit", {
  payload: {
    token: session.ds.generateFrom(token)
  }
});

There is also an equivalent shorthand-version for one-time generators:

session.post("/submit", {
  payload: {
    token: session.ds.generate("random_string", { length: 42 });
  }
});

Structured Files

Structured files are served from a previously uploaded file (e.g. Character Seperated Values, "CSV"). We recommend using tabs for separating values, but an export from Excel should work fine as well.

Please note:

  • files must be ASCII or UTF-8 encoded
  • all lines must have the same number of columns

After uploading a structured file, you can define names for columns which you can use as reference in your test cases.

Usage

  1. Upload a new data source file and select CSV/structured as file type. You can also choose a delimiter and whether to parse the first row as field names
  2. Provide a unique name for later reference in your test cases
  3. Optionally change/update the field names for later reference in your test cases

Given a CSV file with the following contents, the name users.csv and field names id, email and password parsed from the CSV.

id,email,password
1,user1@example.com,super-secret
2,user2@example.com,funwithflags

To fetch a random user row for a login in your test case you can use the following code snippet:

definition.session("login only", function(session) {
  var users = session.ds.loadStructured("users.csv");
  var user = session.ds.pickFrom(users);

  session.post("/user/login", {
    payload: JSON.stringify({
      email: user.get("email"),
      password: user.get("password"),
    })
  });
});

Raw Files

We also support raw files e.g. to for file uploads.

Usage

  1. Upload a file and select Raw as file type.
  2. Provid a unique name for the uploaded file.

To use the raw file for an upload you need to load and set the raw file as a payload_from_file parameter in your request options:

session.put("/user/profile_picture", {
  payload_from_file: session.ds.getRawFile("picture.png")
});

Migration

Starting in 2018 we introduced a new way to manage your file-based data sources. Also the API in your test cases has been changed to generate random data from data sources.

For the time being you can still run tests based on test cases using the old API. But you won't be able to update your fixtures before you migrate to the new API.

Before this change you would define a data source in a global configuration block like this:

definition.setDataSources({
  nonce: {
    type: "random_number", range: [1, 100],
  },

  user: {
    type: "file",
    source: "authentication/admin_users.csv",
    fields: ["id", "username", "password"],
    delimiter: ","
  },
});

definition.session("user registraion", function(session) {
  // ...
});

And you could use the defined data sources like this:

definition.session("user registraion", function(session) {
  var user = session.pick("user");

  session.post("/user/login", {
    payload: JSON.stringify({
      login: user.username(),
      password: user.password(),
      nonce: session.pick("nonce")
    })
  });

});

The new API works a bit different. Instead of defining fields and delimiter in your test case you can now define this per uploaded structured file. It is no longer required to define all data sources outside of your sessions beforehand. This makes it simpler to copy and move session definitions around for example.

Using these new structured data sources works like this:

definition.session("user registraion", function(session) {
  // create a reference to the uploaded data source
  var users = session.ds.loadStructured("authentication/admin_users.csv");

  // pick a random entry
  var user = session.ds.pickFrom(users);

  session.post("/user/login", {
    payload: JSON.stringify({

      // reference fields by their defined aliases
      email: user.get("email"),
      password: user.get("password"),

      // generate a random number ad-hoc
      nonce: session.ds.generate("random_number", { range: [1, 100] })
    })
  });

});
Get started icon

Get Started

New to the StormForger?
With these guides you’ll be up and running in no time!

FAQ icon

FAQ

Already took a look at our FAQs?

Support icon

Support

Are you stuck? Talk to us! We're humans.

We are using cookies to give you the best online experience. If you continue to use this site, you agree to our use of cookies. By declining we will disable all but strictly required cookies. Please see our privacy policy for more details.


Accept Decline