我想添加一个test咖啡馆步骤,其中我调用了一个API GET,并将来自JSON响应体的东西作为一个输入测试输入到一个字段中。
如何在TestCafe中实现此功能?
我试图使用来自RequestLogger的https://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/logging-http-requests.html,它没有工作,也没有提出任何请求。
下面是在控制台中使用的fetch:
var SMScodeField = document.querySelector(`input#code`)
fetch(
//call the API to get the SMS code
`https://app.website.com/api/test-helper/getLastSMS`)
.then(response => response.json())
//use the code value as input into pre-defined field SMScodeField
.then(data => {SMScodeField.value = data.sms[0].code})我也有来自Cypress的工作代码
cy.request(`https://app.website.com/api/test-helper/getLastSMS`)
.then((response) => {
let body = JSON.parse(response.body)
cy.get(`input#code`).type(body._embedded.sms[0].code)
})如何在TestCafe中实现此行为?我只需要在调用code时从JSON响应中获取https://app.website.com/api/test-helper/getLastSMS,并将其用作字段SMScodeField的输入
发布于 2019-10-22 11:40:15
您可以在TestCafe测试中使用标准nodejs代码。只需使用axios或节点获取数据,并在稍后的测试中使用它们。
npm i -D axios @types/axiosimport axios from 'axios';
axios.get('https://app.website.com/api/test-helper/getLastSMS')
.then(response => console.log(response));参考文献:
发布于 2019-10-22 11:53:10
发布于 2022-07-19 13:11:39
在TestCafe v1.20.0和更新版本中,您可以使用t.request method发送请求并在测试中检查它们的响应。请查看以下测试--它发送一个请求,并在typeText操作中使用来自响应的JSON数据。
fixture `Test API`;
test('Simple Test', async t => {
const body = await t.request('https://jsonplaceholder.typicode.com/comments').body;
console.log(body[0].email); //-> Eliseo@gardner.biz
await t.typeText('change-this-selector', body[0].email);
});https://stackoverflow.com/questions/58502566
复制相似问题