0

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:

enter image description here

Joshua
  • 3,055
  • 3
  • 22
  • 37
tkj167487
  • 11
  • 5
  • Newbie question: What do I do to get formatting in the code like in https://stackoverflow.com/questions/56790303/cy-url-and-or-cy-locationhref-does-not-return-a-string ? – tkj167487 Mar 13 '20 at 01:08

1 Answers1

1

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
})
tkj167487
  • 11
  • 5
  • Newbie question: What do I do to get formatting in the code (the colours) like in https://stackoverflow.com/questions/56790303/cy-url-and-or-cy-locationhref-does-not-return-a-string ? – tkj167487 Mar 13 '20 at 01:09
  • You can enable the JS syntax highlight by adding a JS tag to your question. – Joshua Nov 09 '20 at 03:32