1

How can I print Cypress base Url in the Git bash CLI console while running the Cypress test ? Could someone please advise ?

soccerway
  • 10,371
  • 19
  • 67
  • 132

2 Answers2

2

When running headless, the browser logs don't show in the terminal, but those from the Cypress node process (aka cy.task()) do show up.

I assume the baseUrl is changing during the test, here is an example that does that and logs the current value using a task.

The configured value is http://localhost:3000, the test changes it to http://localhost:3001.

cypress.config.js

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {

      console.log('Printing baseUrl - during setup', config.baseUrl)

      on('task', {
        logBaseUrl(baseUrl) {
          console.log('Printing baseUrl - from task', baseUrl)
          return null
        }
      })
    },
    baseUrl: 'http://localhost:3000'
  },
})

test


it('logs baseUrl to the terminal in run mode', () => {

  console.log('Printing baseUrl - directly from test', Cypress.config('baseUrl'))  // no show

  Cypress.config('baseUrl', 'http://localhost:3001')                     
  
  cy.task('logBaseUrl', Cypress.config('baseUrl'))
})

terminal

Printing baseUrl - during setup http://localhost:3000

====================================================================================================
  Running:  log-baseurl.cy.js                                                               (1 of 1)

Printing baseUrl - from task http://localhost:3001
  √ logs baseUrl to the terminal in run mode (73ms)

  1 passing (115ms)

  (Results)
  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Tests:        1                                                                                │
  │ Passing:      1                                                                                │
  │ Failing:      0                                                                                │
  │ Pending:      0                                                                                │
  │ Skipped:      0                                                                                │
  │ Screenshots:  0                                                                                │
  │ Video:        true                                                                             │
  │ Duration:     0 seconds                                                                        │
  │ Spec Ran:     log-baseurl.cy.js                                                                │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘
====================================================================================================

  (Run Finished)

       Spec                                              Tests  Passing  Failing  Pending  Skipped
  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ √  log-baseurl.cy.js                        108ms        1        1        -        -        - │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘
    √  All specs passed!                        108ms        1        1        -        -        -

Done in 18.49s.
TesterDick
  • 3,830
  • 5
  • 18
  • That seems more precise than my answer. Upvoted. – VonC Nov 13 '22 at 07:11
  • @TesterDick Thank you, will be more helpful for people who got two environments like prod, staging to test. So the log will be helpful in which environment the test is pointing while running in headless mode in local or jenkins or docker etc – soccerway Nov 13 '22 at 07:36
  • Upvoted and accepted the answer. – soccerway Nov 13 '22 at 08:05
  • @TesterDick Are you familiar with this .env.local file settings in latest cypress version ?>> https://stackoverflow.com/questions/74440801/unable-to-read-values-from-env-local-file-in-latest-cypress-version – soccerway Nov 15 '22 at 05:24
0

You can use a nodejs command console.log(Cypress.config().baseUrl).

That does not require Git Bash though, only nodejs installed on Your Windows.

Or you can add in your tests cy.config().baseUrl.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Sorry I am running my test in headless mode and looking to print something in cli log and not in browser console ! – soccerway Nov 13 '22 at 02:43