I want to check the url without visiting the actual page, basically application is on angularJS and navigates using ng-click method ($state.go). I have seen too many documentations of cypress stub and intercept but haven't found any thing regarding this type of problem statement.
I have tried below
cy.window().then((win) => {
cy.stub(win, 'open').as('redirect');
});
cy.get('@redirect').should(
'be.calledWith',
'myUrl'
);
but this is use for new tab, i need it for same tab, can anyone help regarding this issue.
Thanks.
If you want to prevent the navigation, it does depend on how the app does it.
One example for stubbing window.location.replace is shown here
Deal with window.location.replace
Can also be used with window.location.href.
it('replaces', () => {
cy.on('window:before:load', (win) => {
win.__location = {
replace: cy.stub().as('replace')
}
})
cy.intercept('GET', 'index.html', (req) => {
req.continue(res => {
res.body = res.body.replaceAll(
'window.location.replace', 'window.__location.replace')
})
}).as('index')
cy.visit('index.html')
cy.wait('@index')
cy.contains('h1', 'First page')
cy.get('@replace').should('have.been.calledOnceWith', 'https://www.cypress.io')
})
This is more complicated than a simple stub, because window.location can't be stubbed (a security measure in the browser).
Gleb Bahmutov shows how to intercept the page load to get around the problem.