cypress,stub窗口函数并获取调用参数或返回值

时间:2020-08-24 10:15:59

标签: javascript cypress sinon stub

我有一个带有按钮的网页,当我单击按钮时,它会执行window.location.href = some_url_on_s3

所以我在窗口window.changeLocation = (url)=>window.location.assing(url)上创建了函数

然后我在cypress中为此功能创建了一个存根。

这个想法是获取s3网址,使用cy.request()下载它,然后将request.body与我期望的内容进行比较。

第一种方法是将 cy.request 放入存根,并期望 cy.request “然后” 。 它由于某种原因而无法工作,并且 cy.request 的“ then ”从不触发。

第二种方法是设置一些变量,将其设置在存根上,然后在 request 中使用。像这样的东西:

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)

但是cypress给我一个错误,提示 cy.request url 参数不能为空...因此,我可能无法理解cypress的一些变数 >

我的最后一种方法是从存根(URL)获取调用参数 柏树桩是锡农桩。因此根据sinon文档,它应该具有 getCall 方法。

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)

但是cypress给我一个错误 cy.get(...)。getCall不是一个函数

我再也没有想法了,内心陷入僵局,想改变职业;)请帮忙

ps。我也尝试过:

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

      }) 

但是stub.getCall(0)返回null并调用了stub,可以在cypress日志中看到它: cypress logs with null styb.getCall(0)

0 个答案:

没有答案
相关问题