gpt4 book ai didi

json - Testcafe - 如何使用 JSON 响应数据作为变量

转载 作者:行者123 更新时间:2023-11-28 20:19:29 26 4
gpt4 key购买 nike

我想添加一个 testcafe 步骤,在该步骤中我调用 API GET 并将 JSON 响应正文中的内容用作字段中的输入测试。

如何在 TestCafe 中实现此功能?

我试图使用来自 https://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/logging-http-requests.htmlRequestLogger那没有用,它没有提出任何请求。

这是在控制台中使用时有效的提取:

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 中实现此行为?我只需要在调用 https://app.website.com/api/test-helper/getLastSMS 时从 JSON 响应中获取 code并将其用作字段 SMScodeField 的输入

最佳答案

您可以在 TestCafe 测试中使用标准的 nodejs 代码。只需使用 axios 或 node-fetch 来获取数据并在稍后的测试中使用它们。

npm i -D axios @types/axios
import axios from 'axios';

axios.get('https://app.website.com/api/test-helper/getLastSMS')
.then(response => console.log(response));

引用资料:

关于json - Testcafe - 如何使用 JSON 响应数据作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58502566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com