2018-10-04 13:37:37 +00:00
---
2020-12-16 01:17:25 +00:00
id: 587d8250367417b2b2512c5d
2018-10-04 13:37:37 +00:00
title: Run Functional Tests using a Headless Browser
challengeType: 2
2019-08-05 16:17:33 +00:00
forumTopicId: 301595
2018-10-04 13:37:37 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2020-11-26 18:05:42 +00:00
2020-11-27 18:02:05 +00:00
As a reminder, this project is being built upon the following starter project on [Repl.it ](https://repl.it/github/freeCodeCamp/boilerplate-mochachai ), or cloned from [GitHub ](https://github.com/freeCodeCamp/boilerplate-mochachai/ ).
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
In the HTML main view we provided a input form. It sends data to the `PUT /travellers` endpoint that we used above with an Ajax request. When the request successfully completes, the client code appends a `<div>` containing the info returned by the call to the DOM. Here is an example of how to interact with this form:
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
```js
test('#test - submit the input "surname" : "Polo"', function (done) {
browser.fill('surname', 'Polo').pressButton('submit', function () {
browser.assert.success();
browser.assert.text('span#name', 'Marco');
browser.assert.text('span#surname', 'Polo');
browser.assert.element('span#dates', 1);
done();
});
}
```
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
First, the `fill` method of the `browser` object fills the `surname` field of the form with the value `'Polo'` . Immediately after, the `pressButton` method invokes the `submit` event listener of the form. The `pressButton` method is asynchronous.
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
Then, once a response is received from the AJAX request, a few assertions are made confirming:
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
1. The status of the response is `200`
2. The text within the `<span id='name'></span>` element matches `'Marco'`
3. The text within the `<span id='surname'></span>` element matches `'Polo'`
4. The there is `1` `<span id='dates'></span>` element.
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
Finally, the `done` callback is invoked, which is needed due to the asynchronous test.
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
# --instructions--
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
Within `tests/2_functional-tests.js` , in the `'submit "surname" : "Colombo" - write your e2e test...'` test (`// #5 `), automate filling-in and submitting the form:
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
1. Fill in the form
2. Submit it pressing `'submit'` button.
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
Within the callback:
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
1. assert that status is OK `200`
2. assert that the text inside the element `span#name` is `'Cristoforo'`
3. assert that the text inside the element `span#surname` is `'Colombo'`
4. assert that the element(s) `span#dates` exist and their count is `1`
2020-11-26 18:05:42 +00:00
2020-12-16 01:17:25 +00:00
Do not forget to to remove the `assert.fail()` call.
2020-11-26 18:05:42 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
All tests should pass.
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=4').then(
(data) => {
assert.equal(data.state, 'passed');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
You should assert that the headless browser request succeeded.
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=4').then(
(data) => {
assert.equal(data.assertions[0].method, 'browser.success');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2020-11-26 18:05:42 +00:00
2020-11-27 18:02:05 +00:00
You should assert that the text inside the element 'span#name' is 'Cristoforo'.
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=4').then(
(data) => {
assert.equal(data.assertions[1].method, 'browser.text');
assert.match(data.assertions[1].args[0], /('|")span#name\1/);
2020-12-16 01:17:25 +00:00
assert.match(data.assertions[1].args[1], /('|")Cristoforo\1/);
2020-11-27 18:02:05 +00:00
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
2018-10-04 13:37:37 +00:00
```
2020-11-27 18:02:05 +00:00
You should assert that the text inside the element 'span#surname' is 'Colombo'.
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=4').then(
(data) => {
assert.equal(data.assertions[2].method, 'browser.text');
assert.match(data.assertions[2].args[0], /('|")span#surname\1/);
2020-12-16 01:17:25 +00:00
assert.match(data.assertions[2].args[1], /('|")Colombo\1/);
2020-11-27 18:02:05 +00:00
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2020-11-26 18:05:42 +00:00
2020-11-27 18:02:05 +00:00
You should assert that the element 'span#dates' exist and its count is 1.
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
(getUserInput) =>
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=4').then(
(data) => {
assert.equal(data.assertions[3].method, 'browser.element');
assert.match(data.assertions[3].args[0], /('|")span#dates\1/);
assert.equal(data.assertions[3].args[1], 1);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
2020-11-26 18:05:42 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
2018-10-04 13:37:37 +00:00
```js
2019-10-14 15:30:42 +00:00
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/
2018-10-04 13:37:37 +00:00
```