简体   繁体   English

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

[英]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我有带有按钮的网页,当我单击按钮时,它会执行 window.location.href = some_url_on_s3

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

and I create a stub in cypress for that function.我在 cypress 中为该功能创建了一个存根。

the idea was to get the s3 url, download it using cy.request() and compare request.body with what I expect to be there.这个想法是获取 s3 url,使用 cy.request() 下载它,并将 request.body 与我期望的内容进行比较。

first approach was to put cy.request inside stub and expect in "then" of cy.request .第一种方法是将cy.request放在 stub 中,并期望在cy.request“then”中。 it does not work for some reason and " then " of cy.request never fires .由于某种原因它不起作用,并且cy.request的“然后”永远不会触发。

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但是赛普拉斯给了我一个错误,即cy.request url参数不能为空......所以赛普拉斯变量可能有一些我无法理解的魔力

my last approach was to get call param from stub (the url) cypress stubs are sinon stubs .我的最后一种方法是从 stub (url) cypress stubs are sinon stubs 中获取调用参数。 so according to sinon docs it should have getCall method.所以根据 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)

but cypress is giving me an error cy.get(...).getCall is not a function但赛普拉斯给我一个错误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:但是 stub.getCall(0) 返回 null 并且调用了 stub,可以在 cypress 日志中看到: 带有 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 包装这部分,使其仅在解析先前的代码并使用新值更新 myUrl 后执行

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)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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