0
0
Cypresstesting~5 mins

cy.visit() for page navigation in Cypress

Choose your learning style9 modes available
Introduction

We use cy.visit() to open a web page in our test. It helps us start testing from the right page.

When you want to open the homepage before testing features on it.
When you need to test a login page by opening its URL first.
When you want to check if a specific page loads correctly.
When you want to reset the test by visiting a fresh page.
When you want to test navigation by visiting different URLs.
Syntax
Cypress
cy.visit(url, options)

url is the web address you want to open, like '/login' or 'https://example.com'.

options is optional and lets you set extra things like headers or timeout.

Examples
Open the full URL https://example.com.
Cypress
cy.visit('https://example.com')
Open the /dashboard page relative to the base URL set in Cypress config.
Cypress
cy.visit('/dashboard')
Open the /profile page and wait up to 10 seconds for it to load.
Cypress
cy.visit('/profile', { timeout: 10000 })
Sample Program

This test opens the Cypress example homepage and checks if the text 'Kitchen Sink' is visible. This confirms the page loaded correctly.

Cypress
describe('Page Navigation Test', () => {
  it('should open the homepage', () => {
    cy.visit('https://example.cypress.io')
    cy.contains('Kitchen Sink').should('be.visible')
  })
})
OutputSuccess
Important Notes

Always use valid URLs or relative paths that your app supports.

If the page takes too long to load, increase the timeout option.

Use cy.visit() at the start of your test to set the right page context.

Summary

cy.visit() opens a web page for testing.

Use it to start tests on the correct page.

You can add options like timeout to control loading behavior.