2021-06-15 07:49:18 +00:00
---
id: 5f8884f4c46685731aabfc41
2021-07-16 05:33:16 +00:00
title: Eseguire test funzionali usando un headless browser II
2021-06-15 07:49:18 +00:00
challengeType: 2
forumTopicId: 301594
dashedName: run-functional-tests-using-a-headless-browser-ii
---
# --description--
2022-06-27 13:55:35 +00:00
Come promemoria, questo progetto verrà costruito a partire dalla seguente bozza su < a href = "https://replit.com/github/freeCodeCamp/boilerplate-mochachai" target = "_blank" rel = "noopener noreferrer nofollow" > Replit< / a > , o clonato da < a href = "https://github.com/freeCodeCamp/boilerplate-mochachai/" target = "_blank" rel = "noopener noreferrer nofollow" > GitHub< / a > .
2021-06-15 07:49:18 +00:00
# --instructions--
2022-07-22 18:25:37 +00:00
All'interno di `tests/2_functional-tests.js` , nel test `'Submit the surname "Vespucci" in the HTML form'` (`// #6 `), automatizza quanto segue:
2021-06-15 07:49:18 +00:00
2021-08-25 16:12:11 +00:00
1. Compila il modulo con il cognome `Vespucci`
2. Premi il pulsante submit
2021-06-15 07:49:18 +00:00
2021-08-25 16:12:11 +00:00
E all'interno della callback `pressButton` :
2021-06-15 07:49:18 +00:00
2021-08-25 16:12:11 +00:00
1. Asserisci che lo stato sia OK `200`
2. Asserisci che il testo all'interno dell'elemento `span#name` è `'Amerigo'`
3. Asserisci che il testo all'interno dell'elemento `span#surname` è `'Vespucci'`
4. Asserisci che gli elementi `span#dates` esistono e il loro conteggio è `1`
2021-06-15 07:49:18 +00:00
2021-07-16 05:33:16 +00:00
Non dimenticare di rimuovere la chiamata `assert.fail()` .
2021-06-15 07:49:18 +00:00
# --hints--
2021-07-16 05:33:16 +00:00
Tutti i test dovrebbero essere superati.
2021-06-15 07:49:18 +00:00
```js
(getUserInput) =>
2021-08-25 16:12:11 +00:00
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=6').then(
2021-06-15 07:49:18 +00:00
(data) => {
assert.equal(data.state, 'passed');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-07-16 05:33:16 +00:00
Dovresti asserire che la richiesta dell'headless browser sia riuscita.
2021-06-15 07:49:18 +00:00
```js
(getUserInput) =>
2021-08-25 16:12:11 +00:00
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=6').then(
2021-06-15 07:49:18 +00:00
(data) => {
assert.equal(data.assertions[0].method, 'browser.success');
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-08-25 16:12:11 +00:00
Dovresti controllare che il testo all'interno dell'elemento `span#name` è `'Amerigo'` .
2021-06-15 07:49:18 +00:00
```js
(getUserInput) =>
2021-08-25 16:12:11 +00:00
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=6').then(
2021-06-15 07:49:18 +00:00
(data) => {
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], /('|")Amerigo\1/);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-08-25 16:12:11 +00:00
Dovresti verificare che il testo all'interno dell'elemento `span#surname` è `'Vespucci'` .
2021-06-15 07:49:18 +00:00
```js
(getUserInput) =>
2021-08-25 16:12:11 +00:00
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=6').then(
2021-06-15 07:49:18 +00:00
(data) => {
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], /('|")Vespucci\1/);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
2021-08-25 16:12:11 +00:00
Dovresti affermare che l'elemento `span#dates` esiste e il suo conteggio è 1.
2021-06-15 07:49:18 +00:00
```js
(getUserInput) =>
2021-08-25 16:12:11 +00:00
$.get(getUserInput('url') + '/_api/get-tests?type=functional& n=6').then(
2021-06-15 07:49:18 +00:00
(data) => {
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);
},
(xhr) => {
throw new Error(xhr.responseText);
}
);
```
# --solutions--
```js
/**
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.
*/
```