Testcafe how to read json response and pick first item and pass it to selector

Rat*_*ehl 3 javascript json automated-tests e2e-testing testcafe

Read JSON response

1) When I visit this URL 'http:example.com/fruits', it hits an endpoint 'http:example.com/v1/collections/fruits' and I see JSON in the browser network response:

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

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

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

      },

   ]
}
Run Code Online (Sandbox Code Playgroud)

2) I want to pick up the first title - Apple and pass it to a selector and click on it

Ali*_*ich 5

在您的应用中,您可以创建“ 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))
Run Code Online (Sandbox Code Playgroud)

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