javascript - Testcafe 如何读取 json 响应并选择第一项并将其传递给选择器

标签 javascript json automated-tests e2e-testing testcafe

读取 JSON 响应

1) 当我访问这个 URL“http:example.com/fruits”时,它会访问一个端点“http:example.com/v1/collections/fruits”,我在浏览器网络响应中看到 JSON:

{
   "total":3,
   "page":1,
   "pageSize":24,
   "rows":[
      {
         "id":19,
         "title":"Apple"

      },
      {
         "id":21,
         "title":"Grape",

      },
      {
         "id":6,
         "title":"Orange",

      },

   ]
}

2) 我想选择第一个标题 - Apple 并将其传递给选择器并单击它

最佳答案

在您的应用中,您可以创建“GET”方法,例如:

fetch('http:example.com/v1/collections/fruits', {
  method: 'GET'
})
.then(response => response.json())
.then(data => {
  console.log(data.rows[0]['title']) // should return 'Apple'
})
.catch(error => console.error(error))

然后,您可以将它传递给您的选择器。

关于javascript - Testcafe 如何读取 json 响应并选择第一项并将其传递给选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56556487/

相关文章:

javascript - Node.js automapper-ts 包不映射嵌套对象/属性

ruby-on-rails - 使用 ActiveRecord 更新 Postgres JSON 字段

css - 元素的完整 iframe 路径

javascript - 如何在文本输入字段中实现没有字母间距的最后一个字母?

javascript - 在 D3 v4 中使用 zoom.translateExtent 限制 map 平移

jquery - 使用 Jquery Ajax 将模型从 View 传递到 Controller

c++ - 从 JSON 数组获取天气

java - 如何在 TestNG 报告中包含 Log4j2 消息

testing - 是否可以执行使用 JMeter 记录的 http 脚本作为 https?

javascript - 使用 useState 更改列表中的事件状态