cypress Cypress.config.baseUrl is set correctly however getting error from cy.visit()
Asked Answered
A

3

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?

Achaean answered 4/5, 2021 at 19:58 Comment(1)
There is a difference in upper case "U" - cy.visit(Cypress.config().baseurl) should be cy.visit(Cypress.config().baseUrl)Chirrupy
B
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)
Boche answered 5/5, 2021 at 2:51 Comment(9)
Cypress.config('baseUrl') returns exactly the same thing as Cypress.config().baseUrlChirrupy
Also, you don't have to use cy.visit('/') if baseUrl is defined.Chirrupy
If you visit the Cypress.config() docs docs.cypress.io/api/cypress-api/config#Test-Configuration, I cannot see any examples for Cypress.config().baseUrlBoche
Also if you visit docs.cypress.io/api/commands/visit#Usage it states that to visit baseUrl you have to use cy.visit(‘/‘)Boche
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.Appulse
@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 ?Boche
When no arguments are passed in to Cypress.config() it returns an object and you can reference properties with dot notation.Appulse
Alapan, it's really easy to confirm - just test it!Appulse
Thanks, @AloysiusParker I just ran it and it worked. Learned something new today. Updated my answer as well.Boche
F
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...

Fondness answered 5/5, 2021 at 6:23 Comment(0)
P
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', }, })

Pedagogue answered 14/6, 2023 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.