Testim Documentation

Welcome to the Testim documentation. You'll find comprehensive guides to help you get started quickly, example code snippets, and tips for being more productive. You will also find details about advanced features that allow you to customize Testim to fit your environment or special testing conditions. Don't worry, we'll support you if you get stuck. Let's jump right in!

Guides    Changelog

Working with Environment Variables

Working with environment variables in Testim Development Kit

🚧

This is a TDK feature

This is not supported in Testim's visual editor

This document will explain how to use environment variables in Testim Development Kit.

This is useful for many cases - like running tests on a different URL in the CI or running your site with a different config on different machines. TDK makes this simple and easy to do.

TDK does not simply copy all environment variables to your base URL for several reasons but the biggest one is Security - we do not want to copy sensitive data by accident and test code may be sent to remote machines to be executed or logged.

📘

Note: cliAction

Code that always runs in the context of your Node code (like cliAction) can always access the full environment. You do not need for example to copy your database password under a TDK_ environment variable in order to query it in a cliAction. In fact you are encouraged not to do so.

Setup

Let's say we have a test that checks the text of a title of a page. You might recognise this demo from the examples folder of your TDK project:

"use strict";
const { go, text, test, l, Locator } = require('testim');
const { expect } = require('chai');
Locator.set(require('./locators/locators.js'));

test('Simple text validation', async () => {
  
  await go("http://demo.testim.io");
  const title = await text(l("SPACE_&_BEYOND"));
  expect(title).to.eq('Space & Beyond');
}); // end of test

This test is fine - but what if we want to test demo.testim.io deployments? This code always checks the same web address the same way which is not flexible.

Let's see how we can fix that.

1. Extract the navigation URL and other data from the envrionment

"use strict";
const { go, text, test, l, Locator } = require('testim');
const { expect } = require('chai');
Locator.set(require('./locators/locators.js'));

let baseUrl = process.env.BASE_URL ?? "http://demo.testim.io";
let websiteTitle = process.env.TDK_WEBSITE_TITLE || 'Space & Beyond';
test('Simple text validation', async () => {
  
  await go(baseUrl);
  const title = await text(l("SPACE_&_BEYOND"));
  expect(title).to.eq(websiteTitle);
}); // end of test

📘

Consider Placing Config in a file

For brevity - we do all the configuration inline here however in real code consider placing all your configuration in a configuration file and isolating environment variable handling there.

2. Pass the environment variables via the CLI

When running your TDK code - pass the environment variables

# before 
$ testim run yourTest.js 
# after
$ TDK_WEBSITE_TITLE='Space & Beyond' npx testim run yourTest.js
# or not inline
$ export TDK_WEBSITE_TITLE='Space & Beyond'
$ npx testim run yourTest.js 
# passing base URL
$ export BASE_URL='http://demo.testim.io'
$ npx testim run yourTest.js 
# passing base URL as an argument
$ npx testim run --base-url='http://demo.testim.io' yourTest.js

Updated 4 months ago

Working with Environment Variables


Working with environment variables in Testim Development Kit

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.