Reputation: 11
I am writing an automation test (more like a script), where I go to a url and then click a button that takes me to a page with a dynamic url. Now I need to go to a pre-defined url and do something there after which I need to go back to the previously saved dynamic url and verify something there. Based on the answer in this stackoverflow question, I tried below, but it did not work:
it('Saves current url, goes to a new url, goes back to previously saved url', () => {
let currentUrl = null
cy.url().then(tempVar => {
currentUrl = tempVar
})
cy.visit('https://a-new-url')
cy.pause() // To pause execution and see the url changing
cy.visit(currentUrl)
})
That results in error text:
No overload matches this call. Overload 1 of 2, '(url: string, options?: Partial | undefined): Chainable', gave the following error. Argument of type 'null' is not assignable to parameter of type 'string'. Overload 2 of 2, '(options: Partial & { url: string; }): Chainable', gave the following error. Argument of type 'null' is not assignable to parameter of type 'Partial & { url: string; }'. Type 'null' is not assignable to type 'Partial'.ts(2769)
screenshot of error:
Upvotes: 0
Views: 1969
Reputation: 11
One of the devs in my team helped me out with below code that works perfectly well:
it('Saves current url, goes to a new url, goes back to previously saved url', async () => {
let currentUrl = await new Promise<string>(resolve => cy.url().then(tempVar => {
resolve(tempVar)
}))
cy.visit('https://a-new-url')
cy.pause() // So that you can pause execution and see the url changing
cy.visit(currentUrl) // Successfully takes you back to the old url
})
Upvotes: 1