English 中文(简体)
抗震功能造成的气候植被价值
原标题:Cypress-get value out of callback function

Hi I am trying to automate pagination api using cypress. This api takes 2 parameters as pageNo and pageSize , pageNo means on which page and pageSize means total records returned by server (at max 15 on 1 page).

Problem:- I want to search for a particular fileName that is returned by this api, i don t know on which pageNo it appears, as soon as i found it i have to come out of the both the loops (pageNo and pageSize) Here is my code:-

   describe( Check particular value , function() {

it( Check record , ()=>{

for (let index = 1; index < 7; index++) {    
    cy.request({
        method:"GET",
        url:"https://mydomainName.com/api/v1/searchFile/getFileList",
        qs:{"pageNo":index,"pageSize":15},
        headers:{"authorization": "jwt token "}
    }).then(function(response){
       
        for (let j = 0; j < response.body.data.length; j++) {
         
        if (response.body.data[j].fileName.includes( file_2021_08_20_04_31.txt )) {
                     
            break;            
        }        
           
        }


    });

 // how can i come out of this parent loop

 
    
}


        
    });
    
});

Advance!

问题回答

是否属于一项职能。 在发现项目时,该功能恢复,或者在下页重复。

如果它没有发现就进入第7页,那么测试就会失败(或退回信息)。

const findFile = (expected, pageSize, maxPages, pageNo = 1) => {

  if (pageNo === maxPages) {
    throw `"${expected} was not found`
  }

  return cy.request({
    ...
    qs:{ pageNo ,pageSize },
    ...
  }).then(response => {

    const files = response.body.data
    const foundFiles = files.filter(file => file.fileName === expected)

    if (!foundFiles.length) {  // not on this page, try next
      pageNo = pageNo + 1
      findFile(expected, pageSize, maxPages, pageNo)
    } else {
      return foundFiles  // if you want the matching objects
    }
  })
})

it( finds a file , () => {

  findFile( file123.txt , 15, 7).then(foundFiles => {
    // example further assertion on found files
    foundFiles.forEach(file => expect(file.fileName).to.eq( file123.txt ))
  })
})




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签