Q7 of 48 · Cypress

What does cy.visit() do and what URL does it default to?

CypressJuniorcypresscy-visitfundamentals

Short answer

Short answer: `cy.visit(url)` navigates to the URL and waits for the page `load` event. Relative URLs are resolved against `baseUrl` from `cypress.config.ts`. Without a baseUrl set, you must pass a fully qualified URL.

Detail

cy.visit is the typical entry point of an E2E test. It performs a real browser navigation, waits for the load event, and asserts a 2xx/3xx response. A 4xx or 5xx fails the test by default — pass { failOnStatusCode: false } to override (rarely a good idea).

The relative-URL behaviour is the gotcha. If baseUrl is set in config, cy.visit('/login') resolves to https://staging.example.com/login. Without a baseUrl, the same call throws because Cypress doesn't know where to anchor it. Setting baseUrl is recommended even for single-environment projects — it makes specs portable across staging/prod.

Useful options: { method: 'POST', body: ... } for POST navigations, { headers: { Authorization: ... } } to seed a request, { onBeforeLoad(win) {} } to stub browser APIs before page scripts run, { timeout: 30000 } for slow pages.

Behind the scenes, cy.visit clears the page state but not cy.session caches or test isolation context — that's a separate mechanism.

// EXAMPLE

login.cy.ts

// cypress.config.ts has: baseUrl: 'https://staging.example.com'

describe('Login', () => {
  it('navigates with relative URL', () => {
    cy.visit('/login');                              // → https://staging.example.com/login
    cy.get('[data-test=email]').should('be.visible');
  });

  it('stubs window.fetch before page scripts run', () => {
    cy.visit('/login', {
      onBeforeLoad(win) {
        cy.stub(win, 'fetch').as('fetch');
      },
    });
  });
});

// WHAT INTERVIEWERS LOOK FOR

Knowing relative URLs depend on `baseUrl`, that 4xx/5xx fails by default, and that `onBeforeLoad` runs before app scripts.

// COMMON PITFALL

Using `cy.visit('https://...')` everywhere instead of setting `baseUrl` — makes specs un-portable across environments.