loading...
Cover image for E2E Testing with TestCafe | Part 2

E2E Testing with TestCafe | Part 2

chrisvasqm profile image Christian Vasquez Updated on ・2 min read

Previously in "E2E Testing with TestCafe"...

We covered the following topics:

  • What E2E and TestCafe are.
  • Installing NodeJS, TestCafe, IDE/editor.
  • Setup the project.
  • Making our first test.
  • Running our first test in a single browser.

This time we will learn about

Running tests in parallel

This can be achieved by using a command like this:

testcafe -c NUMBER-OF-BROWSERS BROWSER PATH-TO-TEST-FILE

But (there's always a but!) if we run it now something weird will happen...

Our "Check founder's names" test is not going to fail, but we will be able to see that the second Chrome browser will do nothing.

What a lazy browser, you might think.

This is actually quite an interesting way of doing parallel execution.

Since we only have 1 test method in our devto.js file, the second browser is not being lazy, it just has nothing to do. Why would he waste his time doing the same test as the first browser?

Don't believe me? Let's add another test:

(I'll give you the entire test method so you can go ahead and copy-pasta into your editor to quickly run the tests again)

test("Filter articles by discuss tag", async (t) => {
    const discussTag = Selector('span').withText('#discuss');
    const discussTitle = Selector('h1').withText('Discussion');

    await t
        .click(discussTag)
        .expect(discussTitle.exists).ok();
});

Now use this command:

testcafe -c 2 chrome tests/devto.js

Make sure to separate each Chrome window so you can see how they both work at the same time.

Cool, but what happens if we have more tests? Should we pass the same -c # as the amount of tests we have? Probably not. You would be using a lot more resources if you try to open too many browsers at once.

What it will actually do is that if we have 3 tests and we give it a -c 2, whichever browser finishes it's test first will take the 3rd one. And so happens if we had 4, 5, or more tests.

This is super useful after our test suite has grown later on in development. This will help you cut the time of E2E tests in half, or even more!

In Part 3 we will be running multiple browsers and then multiple instances of each browsers.

Discussion

markdown guide