curriculum/challenges/english/blocks/quality-assurance-and-testing-with-chai/587d8250367417b2b2512c5d.md
As a reminder, this project is being built upon the following starter project cloned from <a href="https://github.com/freeCodeCamp/boilerplate-mochachai/" target="_blank" rel="noopener noreferrer nofollow">GitHub</a>.
On the page there's an input form. It sends data to the PUT /travellers endpoint as an AJAX request.
When the request successfully completes, the client code appends a <div> containing the information in the response to the DOM.
Here's an example of how to use Zombie.js to interact with the form:
test('Submit the surname "Polo" in the HTML form', function (done) {
browser.fill('surname', 'Polo').then(() => {
browser.pressButton('submit', () => {
browser.assert.success();
browser.assert.text('span#name', 'Marco');
browser.assert.text('span#surname', 'Polo');
browser.assert.elements('span#dates', 1);
done();
});
});
});
First, the fill method of the browser object fills the surname field of the form with the value 'Polo'. fill returns a promise, so then is chained off of it.
Within the then callback, the pressButton method of the browser object is used to invoke the form's submit event listener. The pressButton method is asynchronous.
Then, once a response is received from the AJAX request, a few assertions are made confirming:
200<span id='name'></span> element matches 'Marco'<span id='surname'></span> element matches 'Polo'1 <span id='dates'></span> element.Finally, the done callback is invoked, which is needed due to the asynchronous test.
Within tests/2_functional-tests.js, in the 'Submit the surname "Colombo" in the HTML form' test (// #5), automate the following:
ColomboAnd within the pressButton callback:
200span#name is 'Cristoforo'span#surname is 'Colombo'span#dates exist and their count is 1Do not forget to remove the assert.fail() call.
All tests should pass.
const response = await fetch(code + '/_api/get-tests?type=functional&n=5');
if (!response.ok) {
throw Error(await response.text());
}
const data = await response.json();
assert.equal(data.state, 'passed');
You should assert that the headless browser request succeeded.
const response = await fetch(code + '/_api/get-tests?type=functional&n=5');
if (!response.ok) {
throw Error(await response.text());
}
const data = await response.json();
assert.equal(data.assertions[0].method, 'browser.success');
You should assert that the text inside the element span#name is 'Cristoforo'.
const response = await fetch(code + '/_api/get-tests?type=functional&n=5');
if (!response.ok) {
throw Error(await response.text());
}
const data = await response.json();
assert.equal(data.assertions[1].method, 'browser.text');
assert.match(data.assertions[1].args[0], /('|")span#name\1/);
assert.match(data.assertions[1].args[1], /('|")Cristoforo\1/);
You should assert that the text inside the element span#surname is 'Colombo'.
const response = await fetch(code + '/_api/get-tests?type=functional&n=5');
if (!response.ok) {
throw Error(await response.text());
}
const data = await response.json();
assert.equal(data.assertions[2].method, 'browser.text');
assert.match(data.assertions[2].args[0], /('|")span#surname\1/);
assert.match(data.assertions[2].args[1], /('|")Colombo\1/);
You should assert that the element span#dates exist and its count is 1.
const response = await fetch(code + '/_api/get-tests?type=functional&n=5');
if (!response.ok) {
throw Error(await response.text());
}
const data = await response.json();
assert.equal(data.assertions[3].method, 'browser.elements');
assert.match(data.assertions[3].args[0], /('|")span#dates\1/);
assert.equal(data.assertions[3].args[1], 1);