visit

Improve this doc

Visit a remote URL.

Best Practice

We recommend setting a baseUrl when using cy.visit().

Read about best practices here.

Syntax

cy.visit(url)
cy.visit(url, options)

Usage

Correct Usage

cy.visit('http://localhost:3000')    // Yields the window of the remote page

Arguments

url (String)

The URL to visit.

Cypress will prefix the URL with the baseUrl configured in your network options if you’ve set one.

options (Object)

Pass in an options object to change the default behavior of cy.visit().

Option Default Description
log true Displays the command in the Command log
auth null Adds Basic Authorization headers
failOnStatusCode true Whether to fail on response codes other than 2xx and 3xx
onBeforeLoad function Called before your page has loaded all of its resources.
onLoad function Called once your page has fired its load event.
timeout pageLoadTimeout Time to wait for cy.visit() to resolve before timing out

You can also set all cy.visit() commands’ pageLoadTimeout and baseUrl globally in configuration.

Yields

  • cy.visit() yields the window object after the page finishes loading.

Examples

Url

Visit a local server running on http://localhost:8000

cy.visit() resolves when the remote page fires its load event.

cy.visit('http://localhost:8000')

Options

Change the default timeout

// Wait 30 seconds for page 'load' event
cy.visit('/index.html', { timeout: 30000 })

Adding Basic Auth Headers

Cypress will automatically apply the right authorization headers if you’re attempting to visit an application that requires Basic Authentication.

Simply provide the username and password in the auth object. Then all subsequent requests matching the origin you’re testing will have these attached at the network level.

cy.visit('https://www.acme.com/', {
  auth: {
    username: 'wile',
    password: 'coyote'
  }
})

You can also provide the username and password directly in the URL.

// this is the same thing as providing the auth object
cy.visit('https://wile:[email protected]')

Cypress will automatically attach this header at the network proxy level, outside of the browser. Therefore you will not see this header in the Dev Tools.

Provide an onBeforeLoad callback function

onBeforeLoad is called as soon as possible, before your page has loaded all of its resources. Your scripts will not be ready at this point, but it’s a great hook to potentially manipulate the page.

cy.visit('http://localhost:3000/#dashboard', {
  onBeforeLoad: (contentWindow) => {
    // contentWindow is the remote page's window object
  }
})

Using onBeforeLoad

Check out our example recipes using cy.visit()‘s onBeforeLoad option to:

Provide an onLoad callback function

onLoad is called once your page has fired its load event. All of the scripts, stylesheets, html and other resources are guaranteed to be available at this point.

cy.visit('http://localhost:3000/#/users', {
  onLoad: (contentWindow) => {
    // contentWindow is the remote page's window object
    if (contentWindow.angular) {
      // do something
    }
  }
})

Notes

Redirects

Visit will automatically follow redirects

// we aren't logged in, so our web server
// redirected us to /login
cy.visit('http://localhost:3000/admin')
cy.url().should('match', /login/)

Protocol

Protocol can be omitted from common hosts

Cypress automatically prepends the http:// protocol to common hosts. If you’re not using one of these 3 hosts, then make sure to provide the protocol.

cy.visit('localhost:3000') // Visits http://localhost:3000
cy.visit('0.0.0.0:3000')   // Visits http://0.0.0.0:3000
cy.visit('127.0.0.1:3000') // Visits http://127.0.0.1:3000

Web Server

Cypress can optionally act as your web server

Cypress will automatically attempt to serve your files if you don’t provide a host. The path should be relative to your project’s root folder (where cypress.json is located).

Having Cypress serve your files is useful in simple projects and example apps, but isn’t recommended for real apps. It is always better to run your own server and provide the url to Cypress.

cy.visit('app/index.html')

Prefixes

Visit is automatically prefixed with baseUrl.

Configure baseUrl in the cypress.json file to prevent repeating yourself in every single cy.visit() command. Read more about configuration.

{
  "baseUrl": "http://localhost:3000/#/"
}
cy.visit('dashboard') // Visits http://localhost:3000/#/dashboard

Window

Visit will always yield the remote page’s window object when it resolves

cy.visit('index.html').then((contentWindow) => {
  // contentWindow is the remote page's window object
})

Routing

Preventing XHR / Ajax requests before a remote page initially loads

One common scenario Cypress supports is visiting a remote page and also preventing any Ajax requests from immediately going out.

You may think this works:

// this code may not work depending on implementation
cy.visit('http://localhost:8000/#/app')
cy.server()
cy.route('/users/**', 'fx:users')

But if your app makes a request upon being initialized, the above code will not work. cy.visit() will resolve once its load event fires. The cy.server() and cy.route() commands are not processed until after cy.visit() resolves.

Many applications will have already begun routing, initialization, and requests by the time the cy.visit() in the above code resolves. Therefore creating a cy.server() will happen too late, and Cypress will not process the requests.

Luckily Cypress supports this use case. Simply reverse the order of the commands:

// this code is probably what you want
cy.server()
cy.route('/users/**', {...})
cy.visit('http://localhost:8000/#/app')

Cypress will automatically apply the server and routes to the very next cy.visit() and does so immediately before any of your application code runs.

Rules

Requirements

  • cy.visit() requires being chained off of cy.

  • cy.visit() requires the response to be content-type: text/html.

  • cy.visit() requires the response code to be 2xx after following redirects.

  • cy.visit() requires the load load event to eventually fire.

Assertions

  • cy.visit() will automatically wait for assertions you've chained to pass.

Timeouts

  • cy.visit() can time out waiting for the page to fire its load event.

  • cy.visit() can time out waiting for assertions you've added to pass.

Command Log

Visit example application in a beforeEach

beforeEach(function () {
  cy.visit('https://example.cypress.io/commands/viewport')
})

The commands above will display in the Command Log as:

Command Log visit

When clicking on visit within the command log, the console outputs the following:

Console log visit

See also