Interacting with the environment

In addition to screenshot-based test-actions, Sakuli is able to utilize your keyboard, clipboard and environment variables. All these features are incorporated in the Environment class.

Keyboard actions

The following snippet shows a possible use-case for native keyboard actions. When initiating the download in Firefox, a native file download dialog opens. To start the download, we need to accept the file dialog, which is not possible with the capabilities of Selenium. With Sakuli it is possible to work around this problem, just like a real user would do. We can accept the file dialog by simply pressing the Enter key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
(async () => {
    const testCase = new TestCase("test");
    const url = "https://nodejs.org/en/";
    const env = new Environment();
    try {
        await _navigateTo(url);
        await _click(_link("ABOUT"));
        await _click(_link("Releases"));
        await _click(_link("Dubnium"));
        await _click(_link("node-v10.16.0.tar.gz"));
        await env.keyDown(Key.ENTER);
        await env.keyUp(Key.ENTER);
    } catch (e) {
        await testCase.handleException(e);
    } finally {
        testCase.saveResult();
    }
})().then(done);

It is even possible to change the download location dynamically, by entering a new save path via

1
await env.type("/new/path/to/file");

Secrets

Many E2E tests require some kind of login. While there is no problem in general, it still requires some mechanism to handle credentials. Providing credentials via environment variable is a common practice, but it still is cumbersome when deploying the testcase to another system, since every single environment variable has to be migratet too.

Sakuli comes with a built-in mechanism to deal with sensitive data in testcases. It uses a single masterkey, generated by Sakuli, and uses this key to de- / encrypt secrets to be used in testcases.

Running

1
npx sakuli create masterkey

will generate a new masterkey, which should be exported as an environment variable SAKULI_ENCRYPTION_KEY.

Once the masterkey has been exported, secrets can be encrypted by running

1
npx sakuli encrypt "super secret string"

These encrypted secrets can now safely be stored inside your testfile, because the Environment class provides methods to decrypt these secrets during test execution.

1
2
3
await env.typeAndDecrypt("$ENCRYPTED_SECRET");
// alternatively, via clipboard
await env.pasteAndDecrypt("$ENCRYPTED_SECRET");