English 中文(简体)
我怎样才能拦截窗口. 位置. href change and change what down?
原标题:how can I intercept a window.location.href change and change what happens next?
  • 时间:2022-12-30 03:07:33
  •  标签:
  • cypress
  • href
我有一个窗口. place.href change 正在登录的点击点上的点击事件上的按钮中发生。 它会将用户发送到不同的子域, 然后它们会被重定向到第三方 OAuth 登录流中, 我想在点击按钮时用模拟值处理用户登录在 cypress 中, 但我如何能阻止页面击中我真正的端点? cy. interwic 似乎没有为这种事情工作 en. v. BACKBone 包含( / 授权) 进程 en. v. bACKBone: ps. sitentry_ 窗口. place. herf=url= () () { { let url = ${ proach.env. API}/oauth'如果( ops. Entry) = $ proprocs. siteEntry. includings( / ) en. v. BACKBON: ps. siteness__Eness_ sheystelock_ shy
问题回答
You should be able to stub out the window.location as per variation of Deal with window.location.replace. It s a bit of a monkey-patch on your app, but there is no way to "properly" stub window.location methods or properties because it s protected. cy.on( window:before:load , (win) => { win.__location = { href: null } }) cy.intercept( GET , / , (req) => { req.continue(res => { res.body = res.body.replaceAll( window.location.href , window.__location.href ) }) }).as( index ) cy.visit( / ) // during page load, window.location is stubbed cy.wait( @index )
Encountered this issue today and was also searching for an answer. Found it after checking on ways to modify my code to use window.open(), and passing _self as an additional argument instead of setting the href property of the location. Documentation can be found here: https://www.w3schools.com/jsref/met_win_open.asp Switching to window.open( URL_HERE , _self ) allowed me to mock the behavior of window.open() and provide an alias in my cypress test like this: cy.window().then((win) => { cy.stub(win, open ).as( windowOpen ); }); Then later on in my cypress test I could verify the behavior of window.open(). For example to check what URL was used, you can do a get on the alias, and check what it was called with: cy.get( @windowOpen ).should( be.calledWith , URL_HERE );




相关问题
jquery tab load with dynamic href s

I m not sure if this has already been answered, but here goes I have 4 tabs, two of which I load through controller calls; the remainder I d like to vary the hrefs of and load automatically/on demand....

Alternatives for using "#" in href attribute [duplicate]

The <a> tag is used to create hyperlinks but in this age of jQuery and Ajax we are using it to load HTML into <div>s in the same page as the <a> tags. That said, we set the href ...

newspaper articles link href

How do I change the link href to css, js code for each article without manually doing it? Each article I write is seperated into different folders and I start each article with one template that links ...

Javascript problem with location.href

I have a textbox and whenever the user presses a key it s checked to see if the user pressed enter. If the enter key is pressed, i want to add all the info in the textbox and transfer the user to a ...

Call href from JavaScript

This is the same question as THIS ONE, I can t answer that anymore, so I m re-posting it with my account. Sorry for the mess. I need a Greasemonkey script that on a page load activates a href link ...

引自 Java本

我为Greasemonkey编写一本书,但我不敢试图通过 Java文启动链接。

How to remove "href" with Jquery?

<a id="a$id" onclick="check($id,1)" href="javascript:void(0)" class="black">Qualify</a> After "href" is removed, is "Qualify" still clickable?

热门标签