2018-10-04 14:37:37 +01:00
---
2020-12-15 17:17:25 -08:00
id: 5f8884f4c46685731aabfc41
title: Run Functional Tests Using a Headless Browser II
2018-10-04 14:37:37 +01:00
challengeType: 2
2019-08-05 09:17:33 -07:00
forumTopicId: 301594
2021-01-13 03:31:00 +01:00
dashedName: run-functional-tests-using-a-headless-browser-ii
2018-10-04 14:37:37 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2020-11-26 18:05:42 +00:00
2021-04-29 06:13:38 -04:00
As a reminder, this project is being built upon the following starter project on [Replit ](https://replit.com/github/freeCodeCamp/boilerplate-mochachai ), or cloned from [GitHub ](https://github.com/freeCodeCamp/boilerplate-mochachai/ ).
2019-03-27 17:01:58 +05:30
2020-11-27 19:02:05 +01:00
# --instructions--
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
Within `tests/2_functional-tests.js` , in the `'Submit the surname "Vespucci" in the HTML form'` test (`// #5` ), automate the following:
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
1. Fill in the form with the surname `Vespucci`
2. Press the submit button
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
And within the `pressButton` callback:
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
1. Assert that status is OK `200`
2. Assert that the text inside the element `span#name` is `'Amerigo'`
3. Assert that the text inside the element `span#surname` is `'Vespucci'`
4. Assert that the element(s) `span#dates` exist and their count is `1`
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
Do not forget to remove the `assert.fail()` call.
2020-11-26 18:05:42 +00:00
2020-11-27 19:02:05 +01:00
# --hints--
All tests should pass.
```js
(getUserInput) =>
2021-08-11 01:45:12 +09:00
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=6').then(
2020-11-27 19:02:05 +01:00
(data) => {
assert.equal(data.state, 'passed');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
You should assert that the headless browser request succeeded.
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
2021-08-11 01:45:12 +09:00
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=6').then(
2020-11-27 19:02:05 +01:00
(data) => {
assert.equal(data.assertions[0].method, 'browser.success');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
You should assert that the text inside the element `span#name` is `'Amerigo'` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
2021-08-11 01:45:12 +09:00
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=6').then(
2020-11-27 19:02:05 +01:00
(data) => {
assert.equal(data.assertions[1].method, 'browser.text');
assert.match(data.assertions[1].args[0], /('|")span#name \1/);
2020-12-15 17:17:25 -08:00
assert.match(data.assertions[1].args[1], /('|")Amerigo\1/);
2020-11-27 19:02:05 +01:00
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
2018-10-04 14:37:37 +01:00
```
2021-08-11 01:45:12 +09:00
You should assert that the text inside the element `span#surname` is `'Vespucci'` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
2021-08-11 01:45:12 +09:00
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=6').then(
2020-11-27 19:02:05 +01:00
(data) => {
assert.equal(data.assertions[2].method, 'browser.text');
assert.match(data.assertions[2].args[0], /('|")span#surname \1/);
2020-12-15 17:17:25 -08:00
assert.match(data.assertions[2].args[1], /('|")Vespucci\1/);
2020-11-27 19:02:05 +01:00
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2020-11-26 18:05:42 +00:00
2021-08-11 01:45:12 +09:00
You should assert that the element `span#dates` exist and its count is 1.
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
(getUserInput) =>
2021-08-11 01:45:12 +09:00
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=6').then(
2020-11-27 19:02:05 +01:00
(data) => {
2021-05-22 10:58:35 +02:00
assert.equal(data.assertions[3].method, 'browser.elements');
2020-11-27 19:02:05 +01:00
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 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-10-04 14:37:37 +01:00
```js
2019-10-14 21:00:42 +05:30
/**
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 14:37:37 +01:00
```