/// cypress.config.js export default { e2e: { baseUrl: 'https://example.com', }, }; // test.spec.js describe('Home page test', () => { it('Visits the base URL', () => { cy.visit('/'); }); });
The baseUrl is set to 'https://example.com' in the configuration. When cy.visit('/') is called, it navigates to 'https://example.com/' (with trailing slash).
cy.visit('/'); // Which assertion below is correct?
Using should('include', 'https://myapp.test') verifies the current URL contains the base URL. Exact equality may fail if there are trailing slashes or query parameters.
/// cypress.config.js export default { e2e: { // baseUrl is missing }, }; // test.spec.js describe('Test without baseUrl', () => { it('Visits root path', () => { cy.visit('/'); }); });
When baseUrl is not set, cy.visit('/') cannot build a full URL and throws an 'Invalid URL' error. You must set baseUrl or use a full URL in cy.visit.
Setting baseUrl lets you write tests with relative paths like cy.visit('/login'), making tests cleaner and easier to switch between environments (dev, staging, prod) by changing one config.
/// cypress.config.js export default { e2e: { baseUrl: 'https://default.com', }, }; // test.spec.js describe('Override baseUrl test', () => { it('Visits a different domain', () => { // What code below correctly overrides baseUrl for this test? }); });
Passing a full URL string to cy.visit overrides the baseUrl for that visit. Options like { baseUrl: ... } are not supported in cy.visit. Changing Cypress.config at runtime is not recommended.