【发布时间】:2020-05-16 22:06:53
【问题描述】:
我有一个问题似乎在任何地方都没有得到回答。
我正在我的 Express.js api 中运行测试。我设置了一个页面,该页面有一个按钮和一个字段来输入要在 testcafe 测试期间使用的关键字。我设置的端点是/testcafe。但是在向/testcafe 发送了一个帖子请求后,测试运行有很长的延迟,所以我的问题是除了挂起之外最好的下一步是什么?
另外,我的post request body,包含关键字,可以直接用在这样的测试中吗?请记住这是这种模式:
前端 -> POST 请求 -> Express 服务器 -> /testcafe 端点 - 测试
我的问题是在它达到测试后,我目前正在尝试从请求记录器中调用 fetch。是这样吗?
import { ClientFunction, Selector } from 'testcafe';
import { RequestLogger, RequestHook } from 'testcafe';
import zlib from 'zlib';
import fetch from 'isomorphic-unfetch';
const url = 'https://www.mysitetesturl.com/page';
class MyRequestHook extends RequestHook {
constructor (requestFilterRules, responseEventConfigureOpts) {
super(requestFilterRules, responseEventConfigureOpts);
}
onRequest (e) {
console.log('in onRequest!')
console.log('========================')
console.log('Request Body')
let buf = e._requestContext.reqBody
console.log(buf.toLocaleString())
}
onResponse (e) {
let buf = Buffer(e.body)
let unzippedBody = Buffer(zlib.gunzipSync(buf))
let payload = unzippedBody.toLocaleString()
fetch('http://myapiipaddress/api/testcafe',
method: 'PUT',
body: JSON.stringify(payload)
)
.then((err, doc) => {
if(err) {
console.log(err)
} else {
console.log(doc)
}
})
}
}
const myRequestHook = new MyRequestHook({
url: url,
method:'get'},
{
includeHeaders: true,
includeBody: true
}
);
fetch('http://myapiipaddress/api/testcafe',
method: 'GET'
)
.then((err, doc) => {
if(err) {
console.log(err)
} else {
fixture`myfixture`
.page(doc.url)
.requestHooks(myRequestHook);
test(`mytest`, async t => {
const inputField = Selector('input');
await t
await t
.wait(5000)
.typeText(inputField, doc.text)
.wait(5000)
}
);
}
})
【问题讨论】:
标签: node.js express testing mongoose testcafe