You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Overview

In this tutorial, we will create some E2E tests in JavaScript for Node.js, using TestCafé.

Requirements

  • TestCafé
  • testcafe-reporter-xunit

Description

The following examples are taken from TestCafé's documentation; the first one implements a test .

 

Simple message validation after submission of input


test1.js
import { Selector } from 'testcafe'; // first import testcafe selectors

fixture `Getting Started`// declare the fixture
    .page `https://devexpress.github.io/testcafe/example`;  // specify the start page


//then create a test and place your code there
test('My first test', async t => {
    await t
        .typeText('#developer-name', 'John Smith')
        .click('#submit-button')

        // Use the assertion to check if the actual header text is equal to the expected one
        .expect(Selector('#article-header').innerText).eql('Thank you, John Smith!');
});


Implementing tests using page object pattern

In this scenario, taken from TestCafé's documentation, we start by defining a page object.


page-model.js
import { Selector } from 'testcafe';

const label = Selector('label');

class Feature {
    constructor (text) {
        this.label    = label.withText(text);
        this.checkbox = this.label.find('input[type=checkbox]');
    }
}

class OperatingSystem {
    constructor (text) {
        this.label       = label.withText(text);
        this.radioButton = this.label.find('input[type=radio]');
    }
}

export default class Page {
    constructor () {
        this.nameInput             = Selector('#developer-name');
        this.triedTestCafeCheckbox = Selector('#tried-test-cafe');
        this.populateButton        = Selector('#populate');
        this.submitButton          = Selector('#submit-button');
        this.results               = Selector('.result-content');
        this.commentsTextArea      = Selector('#comments');

        this.featureList = [
            new Feature('Support for testing on remote devices'),
            new Feature('Re-using existing JavaScript code for testing'),
            new Feature('Running tests in background and/or in parallel in multiple browsers'),
            new Feature('Easy embedding into a Continuous integration system'),
            new Feature('Advanced traffic and markup analysis')
        ];

        this.osList = [
            new OperatingSystem('Windows'),
            new OperatingSystem('MacOS'),
            new OperatingSystem('Linux')
        ];

        this.slider = {
            handle: Selector('.ui-slider-handle'),
            tick:   Selector('.slider-value')
        };

        this.interfaceSelect       = Selector('#preferred-interface');
        this.interfaceSelectOption = this.interfaceSelect.find('option');
    }
}


Two tests can easily be implemented using the previous page object, one of them iterating over a bunch of checkboxes.


test2.js
import Page from './page-model';

fixture `My fixture`
    .page `https://devexpress.github.io/testcafe/example/`;

const page = new Page();

test('Text typing basics', async t => {
    await t
        .typeText(page.nameInput, 'Peter')
        .typeText(page.nameInput, 'Paker', { replace: true })
        .typeText(page.nameInput, 'r', { caretPos: 2 })
        .expect(page.nameInput.value).eql('Parker');
});

test('Click check boxes and then verify their state', async t => {
    for (const feature of page.featureList) {
        await t
            .click(feature.label)
            .expect(feature.checkbox.checked).ok();
    }
});


Running the tests

In this example, we'll run the tests using Chrome browser. It is also possible to perform headless testing (e.g. use "chrome:headless" as the browser name, for example).

testcafe chrome test1.js test2.js --reporter xunit > results.xml


After running the tests and generating the JUnit XML reports (e.g., results.xml), they can be imported to Xray (either by the REST API or through the Import Execution Results action within the Test Execution).


JUnit's Test Case is mapped to a Generic Test in Jira, and the Generic Test Definition field contains the value of the "it" concatenated with the several "describe"' that make up the test case.

The Execution Details of the Generic Test contains information about the Test Suite, which in this case corresponds to the concatenation of the test's "describe".


References


  • No labels