2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
2021-02-06 04:42:36 +00:00
|
|
|
|
id: 587d8250367417b2b2512c5d
|
2021-03-22 07:52:28 -06:00
|
|
|
|
title: 使用无头浏览器运行功能测试
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 2
|
2020-09-17 03:50:23 -07:00
|
|
|
|
forumTopicId: 301595
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: run-functional-tests-using-a-headless-browser
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-05-12 21:25:58 +05:30
|
|
|
|
请注意,本项目在[这个 Replit 项目](https://replit.com/github/freeCodeCamp/boilerplate-mochachai)的基础上进行开发。你也可以从 [GitHub](https://repl.it/github/freeCodeCamp/boilerplate-mochachai) 上克隆。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
在页面上有一个输入表单。 它将数据作为 AJAX 请求发送到 `PUT /travellers` 端点。
|
|
|
|
|
|
|
|
|
|
当请求成功完成后,客户端代码将一个包含响应信息的 `<div>` 附加到 DOM 中。
|
|
|
|
|
|
|
|
|
|
下面是一个如何使用 Zombie.js 与表单互动的例子。
|
2020-09-17 03:50:23 -07:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
```js
|
2021-09-03 01:11:45 -07:00
|
|
|
|
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();
|
|
|
|
|
});
|
2021-02-06 04:42:36 +00:00
|
|
|
|
});
|
2021-09-03 01:11:45 -07:00
|
|
|
|
});
|
2021-02-06 04:42:36 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
首先, `browser` 对象的 `fill` 方法在表格的 `surname` 字段中填入值 `'Polo'`。 `fill` 返回一个 promise,所以 `then` 被链接起来。
|
|
|
|
|
|
|
|
|
|
在 `then` 回调中,`browser` 对象的 `pressButton` 方法用于调用表单的 `submit` 的事件侦听器。 `pressButton` 方法是异步的。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
然后,一旦收到来自 AJAX 请求的响应,就会做出一些断言来确认:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
1. 响应状态是 `200`
|
|
|
|
|
2. `<span id='name'></span>` 元素的文本是 `'Marco'`
|
|
|
|
|
3. `<span id='surname'></span>` 元素的文本是 `'Polo'`
|
|
|
|
|
4. 有 `1` 个 `<span id='dates'></span>` 元素。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
最后,执行 `done`,这是异步测试所必需的。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
在 `tests/2_functional-tests.js` 中,在 `'Submit the surname "Colombo" in the HTML form'` 测试(`// #5`),自动执行以下操作:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
1. 在表格中填写姓氏 `Colombo`。
|
|
|
|
|
2. 点击提交按钮
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
在 `pressButton` 回调中:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
1. 断言状态是正常的 `200`。
|
|
|
|
|
2. 断言元素 `span#name` 中的文本是 `'Cristoforo'`。
|
|
|
|
|
3. 断言元素 `span#surname` 中的文本是 `'Colombo'`。
|
|
|
|
|
4. 断言有 `span#dates` 元素,它们的计数是 `1`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
不要忘记删除 `assert.fail()` 调用。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-03-22 07:52:28 -06:00
|
|
|
|
应通过所有测试。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
(getUserInput) =>
|
2021-09-03 01:11:45 -07:00
|
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=5').then(
|
2020-12-16 00:37:30 -07:00
|
|
|
|
(data) => {
|
|
|
|
|
assert.equal(data.state, 'passed');
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
应断言无头浏览器成功执行请求。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
(getUserInput) =>
|
2021-09-03 01:11:45 -07:00
|
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=5').then(
|
2020-12-16 00:37:30 -07:00
|
|
|
|
(data) => {
|
|
|
|
|
assert.equal(data.assertions[0].method, 'browser.success');
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
应断言元素 `span#name` 中的文本是 `'Cristoforo'`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
(getUserInput) =>
|
2021-09-03 01:11:45 -07:00
|
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=5').then(
|
2020-12-16 00:37:30 -07: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], /('|")Cristoforo\1/);
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
应断言元素 `span#surname` 中的文本是 `'Colombo'`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
(getUserInput) =>
|
2021-09-03 01:11:45 -07:00
|
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=5').then(
|
2020-12-16 00:37:30 -07: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], /('|")Colombo\1/);
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.responseText);
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-09-03 01:11:45 -07:00
|
|
|
|
应该断言元素 `span#dates` 存在,且它的值为 1。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
(getUserInput) =>
|
2021-09-03 01:11:45 -07:00
|
|
|
|
$.get(getUserInput('url') + '/_api/get-tests?type=functional&n=5').then(
|
2020-12-16 00:37:30 -07:00
|
|
|
|
(data) => {
|
2021-05-12 21:25:58 +05:30
|
|
|
|
assert.equal(data.assertions[3].method, 'browser.elements');
|
2020-12-16 00:37:30 -07: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);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01: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.
|
|
|
|
|
*/
|
|
|
|
|
```
|