mycargus
mycargus

Reputation: 2618

Can I use TestCafe for non-browser testing?

I have a test suite that requires a browser for UI testing. I also have a non-browser API test suite (HTTP requests/responses).

I would like to use TestCafe as my test runner for both test suites, but I don't want TestCafe to use a browser when executing the API HTTP tests. Is this not possible?

I couldn't find any TestCafe documentation for this use case. Thanks in advance!

Upvotes: 3

Views: 1760

Answers (2)

Helen Dikareva
Helen Dikareva

Reputation: 1036

You can use the t.request method in TestCafe v1.20.0 and newer. There is no need to use third party modules. In this case, you don't even need to parse the response's body, because TestCafe formats the body property according to the content-type.

fixture `Github API`;

test(`Check DevExpress repositories`, async t => {
    const requestResult = await t.request('https://api.github.com/orgs/DevExpress/repos');
    const repos         = requestResult.body;

    console.log(repos);

    await t.expect(repos.some(repo => repo.name === 'testcafe')).eql(true);
});

Upvotes: 1

Andrey Belym
Andrey Belym

Reputation: 2903

It's possible to use TestCafe for running Node.js unit tests. You can use any Node.js API and require any Node.js module in TestCafe tests. The page directive is optional, so you don't have to specify a test page for unit tests. There is a minor problem that you still have to specify the browser argument and TestCafe will create a browser window when starting your test suite.

The following example uses the got module to access the GitHub API:

import got from 'got';

fixture `Github API`;

test(`Check DevExpress repositories`, async t => {
    const requestResult = await got('https://api.github.com/orgs/DevExpress/repos');
    const repos         = JSON.parse(requestResult.body);

    await t.expect(repos.some(repo => repo.name === 'testcafe')).eql(true);
});

You can run the example using the following command:

testcafe chrome:headless test.js

We have a suggestion to allow running TestCafe without starting a browser: https://github.com/DevExpress/testcafe/issues/1314. Until it is implemented, you can use the testcafe-browser-provider-nightmare browser provider or headless browsers to run tests without a visible browser window.

Upvotes: 6

Related Questions