2018-10-10 22:03:03 +00:00
|
|
|
---
|
2021-02-06 04:42:36 +00:00
|
|
|
id: 5f8884f4c46685731aabfc41
|
2021-03-22 13:52:28 +00:00
|
|
|
title: 使用 无头浏览器 II 运行功能测试
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 2
|
2020-09-17 10:50:23 +00:00
|
|
|
forumTopicId: 301594
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: run-functional-tests-using-a-headless-browser-ii
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-05-12 15:55:58 +00:00
|
|
|
请注意,本项目在[这个 Replit 项目](https://replit.com/github/freeCodeCamp/boilerplate-mochachai)的基础上进行开发。你也可以从 [GitHub](https://repl.it/github/freeCodeCamp/boilerplate-mochachai) 上克隆。
|
2020-09-17 10:50:23 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
在 `tests/2_functional-tests.js` 中,在 `'Submit the surname "Vespucci" in the HTML form'` 测试(`// #5`),自动执行以下操作:
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
1. 在表格中填写姓氏 `Vespucci`。
|
|
|
|
2. 点击提交按钮
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
在 `pressButton` 回调中:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
1. 断言状态是正常的 `200`。
|
|
|
|
2. 断言元素 `span#name` 中的文本是 `'Amerigo'`。
|
|
|
|
3. 断言元素 `span#surname` 元素中的文本是 `'Vespucci'`。
|
|
|
|
4. 断言有 `span#dates` 元素,它们的计数是 `1`。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
不要忘记删除 `assert.fail()` 调用。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
应通过所有测试。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
(getUserInput) =>
|
2021-09-03 08:11:45 +00:00
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=6').then(
|
2020-12-16 07:37:30 +00:00
|
|
|
(data) => {
|
|
|
|
assert.equal(data.state, 'passed');
|
|
|
|
},
|
|
|
|
(xhr) => {
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
}
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
应断言无头浏览器成功执行请求。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
(getUserInput) =>
|
2021-09-03 08:11:45 +00:00
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=6').then(
|
2020-12-16 07:37:30 +00:00
|
|
|
(data) => {
|
|
|
|
assert.equal(data.assertions[0].method, 'browser.success');
|
|
|
|
},
|
|
|
|
(xhr) => {
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-09-03 08:11:45 +00:00
|
|
|
应断言元素 `span#name` 中的文本是 `'Amerigo'`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
(getUserInput) =>
|
2021-09-03 08:11:45 +00:00
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=6').then(
|
2020-12-16 07:37:30 +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-09-03 08:11:45 +00:00
|
|
|
应断言元素 `span#surname` 中的文本是 `'Vespucci'`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
(getUserInput) =>
|
2021-09-03 08:11:45 +00:00
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=6').then(
|
2020-12-16 07:37:30 +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-09-03 08:11:45 +00:00
|
|
|
应该断言元素 `span#dates` 存在,且它的值为 1。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
(getUserInput) =>
|
2021-09-03 08:11:45 +00:00
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=6').then(
|
2020-12-16 07:37:30 +00:00
|
|
|
(data) => {
|
2021-05-24 07:42:08 +00:00
|
|
|
assert.equal(data.assertions[3].method, 'browser.elements');
|
2020-12-16 07:37:30 +00: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-10 22:03:03 +00:00
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```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.
|
|
|
|
*/
|
|
|
|
```
|