6

here is my test:

describe('settings page test', () =>{
    it('tests navigation to settings page from login', () =>{
        console.log(Cypress.config().baseUrl)
        cy.visit(Cypress.config().baseurl)

the console.log(Cypress.config().baseUrl) sure enough is containing the value my baseUrl is set as.

{
  "baseUrl": "https://superniftyurltho.com",
  "env": {

  }
}

and here is the error

    cy.visit() must be called with a url 
or an options object containing a url as its 1st argumentLearn more

anyone know what might be going on?

48326tho
  • 61
  • 1
  • 1
  • 2
  • 1
    There is a difference in upper case "U" - `cy.visit(Cypress.config().baseurl)` should be `cy.visit(Cypress.config().baseUrl)` –  May 05 '21 at 03:16

3 Answers3

7

If you have the baseUrl defined in your cypress.json you can use the cy.visit() in your tests as:

cy.visit('/')

OR,

If you want to use the Cypress.config() method to access the baseUrl from your cypress.json you have to use:

cy.visit(Cypress.config('baseUrl'))

OR,

With your example, the 'u' in the baseurl is in lower case, it should be in upper case. Thanks, @Barmy Fotheringay-Phipps and @Aloysius Parker for pointing it out.

cy.visit(Cypress.config().baseUrl)
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
  • `Cypress.config('baseUrl')` returns exactly the same thing as `Cypress.config().baseUrl` –  May 05 '21 at 03:19
  • 1
    Also, you don't ***have*** to use `cy.visit('/')` if baseUrl is defined. –  May 05 '21 at 03:23
  • If you visit the `Cypress.config()` docs https://docs.cypress.io/api/cypress-api/config#Test-Configuration, I cannot see any examples for `Cypress.config().baseUrl` – Alapan Das May 05 '21 at 04:40
  • Also if you visit https://docs.cypress.io/api/commands/visit#Usage it states that to visit baseUrl you have to use `cy.visit(‘/‘)` – Alapan Das May 05 '21 at 05:16
  • Incorrect, it does not say it's mandatory. You can still use `cy.visit(Cypress.config().baseUrl)` and it will correctly go to the baseUrl. –  May 05 '21 at 05:49
  • @AloysiusParker I am unaware of that, can you share the link to the cypress docs where some example is given where I can access `cypress.json` content using `Cypress.config().someKey` ? – Alapan Das May 05 '21 at 05:51
  • When no arguments are passed in to `Cypress.config()` it returns an object and you can reference properties with dot notation. –  May 05 '21 at 05:52
  • Alapan, it's really easy to confirm - just test it! –  May 05 '21 at 05:52
  • Thanks, @AloysiusParker I just ran it and it worked. Learned something new today. Updated my answer as well. – Alapan Das May 05 '21 at 05:59
2

Just to clarify, Javascript is case sensitive so accessing the baseUrl property of your config object must use an exact case match,

Cypress.config().baseurl === undefined

so

cy.visit(undefined) 

causes the error cy.visit() must be called with a url...

0

If someone has the same issue with the new updates just make sure to add the base url in your cypress.config.js file, then enter the base url inside e2e:

module.exports = defineConfig({ e2e: { baseUrl: 'http://localhost:8484', }, })