简体   繁体   中英

cypress, stub window function and get call param or return value

I have web page with button, when I click on button it do window.location.href = some_url_on_s3

so i created function on window window.changeLocation = (url)=>window.location.assing(url)

and I create a stub in cypress for that function.

the idea was to get the s3 url, download it using cy.request() and compare request.body with what I expect to be there.

first approach was to put cy.request inside stub and expect in "then" of cy.request . it does not work for some reason and " then " of cy.request never fires .

second aproach was to make some variable , set it on stub and use later in request . something like this:

let myUrl =''
cy.window().then(window=>cy.stub(window, 'changeLocation', (url)=>myUrl=url).as('changeLocation'))  

cy.get(`div[data-testid=processed_data] button[title=⤓]`).click()   // click on download button

cy.log(myUrl) // it log right url, so it should be fine?
cy.request(myUrl).then(r=>cy.log(r.body))
cy.wait(1000)

but cypress giving me an error that cy.request url param cant be empty ... so there is probably some magic with cypress variables that I cant understand

my last approach was to get call param from stub (the url) cypress stubs are sinon stubs . so according to sinon docs it should have getCall method.

cy.window().then(window=>cy.stub(window, 'changeLocation').as('changeLocation'))  
        cy.get(`div[data-testid=processed_data] button[title=⤓]`).click()   
        cy.wait(1000)
       const firstCall = cy.get('@changeLocation').getCall(0)
        cy.wait(1000)

but cypress is giving me an error cy.get(...).getCall is not a function

and I have no more ideas, feel dead inside and want to change career ;) please help

ps. I also did try :

          const stub=cy.stub(window, 'changeLocation')
          cy.get(`div[data-testid=processed_data] button[title=⤓]`).click()   
          cy.wait(1000)
          cy.log(stub.getCall(0))

      }) 

but stub.getCall(0) returning null and stub was called, it can be seen in cypress logs: 带有 null styb.getCall(0) 的柏树日志

wrap this part with cy.then to let it be executed only after previous code is resolved and myUrl is updated with new value

cy.then(()=>{
    cy.log(myUrl) // it log right url, so it should be fine?
    cy.request(myUrl).then(r=>cy.log(r.body))
    cy.wait(1000)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM