I have a test case which opens a secondary window. From what I read online, you should prevent this second window opening and visit the url that should have been opened. However, in all the test example I saw, the second url is static. In my case, I need it to be dynamic. This is why I'm using a cy.stub() to a window opening and trying to get the url from it.
cy.visit('https://myUrl.com');
cy.window().then(win => {
cy.stub(win, 'open', newUrl => {
cy.wrap(newUrl).as('newUrl');
});
});
cy.get('#myButton').click(); // opens new window at new url with generated keys
cy.get('@newUrl').then(newUrl => {
cy.visit(newUrl);
});
However, the cy.wrap() inside the cy.stub() triggers this error:
Error: CypressError: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.
The command that returned the promise was:
> `cy.click()`
The cy command you invoked inside the promise was:
> `cy.wrap()`
Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.
Basically my question is how to capture the url in a stub for later use. Thanks in advance.