Base URL configuration helps you set a main website address once. This way, you don't have to write the full address every time in your tests.
Base URL configuration in Cypress
Introduction
Syntax
Cypress
In cypress.config.js or cypress.config.ts: module.exports = { e2e: { baseUrl: 'https://example.com' } };
The baseUrl is the main address for your website.
Use cy.visit('/') to go to the base URL in your tests.
Examples
Cypress
module.exports = { e2e: { baseUrl: 'https://myapp.com' } };
Cypress
cy.visit('/')Cypress
cy.visit('/login')Sample Program
This test uses the base URL set in the config. The first test visits the homepage. The second test visits a specific page using a relative path.
Cypress
// cypress.config.js module.exports = { e2e: { baseUrl: 'https://example.cypress.io' } }; // In a test file: example_spec.js describe('Base URL Test', () => { it('Visits the base URL homepage', () => { cy.visit('/'); cy.contains('Kitchen Sink').should('be.visible'); }); it('Visits a page using relative URL', () => { cy.visit('/commands/actions'); cy.url().should('include', '/commands/actions'); }); });
Important Notes
Always set baseUrl in your Cypress config to avoid repeating full URLs.
You can override baseUrl in specific tests by giving a full URL to cy.visit().
Using baseUrl makes switching environments easier by changing one place.
Summary
Base URL saves time by setting the main website address once.
Use relative paths in cy.visit() to navigate pages.
It helps keep tests clean and easy to update.
Practice
1. What is the main benefit of setting a
baseUrl in Cypress configuration?easy
Solution
Step 1: Understand baseUrl purpose
ThebaseUrlis used to set the main website address once in Cypress config.Step 2: How baseUrl affects
Whency.visit()baseUrlis set, you can use relative paths incy.visit()instead of full URLs.Final Answer:
It allows using relative URLs incy.visit()to simplify test code. -> Option CQuick Check:
baseUrl simplifies URLs in tests = C [OK]
Hint: Remember baseUrl lets you skip typing full URLs in tests [OK]
Common Mistakes:
- Thinking baseUrl logs in users automatically
- Believing baseUrl blocks other network requests
- Confusing baseUrl with browser settings
2. Which of the following is the correct way to set
baseUrl in cypress.config.js?easy
Solution
Step 1: Identify correct syntax for Cypress config
Cypress config uses CommonJS export withmodule.exports = {}.Step 2: Check property name and assignment
The property must bebaseUrl(camelCase) and assigned with colon, not equals.Final Answer:
module.exports = { baseUrl: 'https://example.com' } -> Option DQuick Check:
Correct config syntax uses module.exports and colon = A [OK]
Hint: Use module.exports with colon for config objects [OK]
Common Mistakes:
- Using equals (=) instead of colon (:) in object
- Writing base_url instead of baseUrl
- Using ES module syntax without transpiling
3. Given
baseUrl is set to https://shop.com, what URL will Cypress visit with cy.visit('/cart')?medium
Solution
Step 1: Understand how baseUrl combines with relative paths
IfbaseUrlis set, relative paths incy.visit()append to it.Step 2: Combine baseUrl and path
Appending/carttohttps://shop.comresults inhttps://shop.com/cart.Final Answer:
https://shop.com/cart -> Option AQuick Check:
baseUrl + relative path = full URL [OK]
Hint: Relative path in visit appends to baseUrl [OK]
Common Mistakes:
- Expecting double slashes in URL
- Thinking cy.visit uses baseUrl only for root path
- Confusing relative path with absolute URL
4. You set
baseUrl to https://app.test but cy.visit('login') fails with a 404 error. What is the likely cause?medium
Solution
Step 1: Check how relative paths combine with baseUrl
Relative paths should start with a slash to append correctly to baseUrl.Step 2: Analyze given path 'login'
Without leading slash, 'login' appends incorrectly, causing a wrong URL and 404 error.Final Answer:
Missing leading slash incy.visit()path causes wrong URL. -> Option BQuick Check:
Always start relative paths with '/' in cy.visit [OK]
Hint: Start relative paths with '/' in cy.visit to avoid errors [OK]
Common Mistakes:
- Assuming baseUrl needs trailing slash
- Thinking Cypress disallows relative URLs
- Setting baseUrl inside test files instead of config
5. You want to run tests on two environments:
https://dev.site and https://prod.site. How can you configure Cypress to switch baseUrl easily?hard
Solution
Step 1: Understand environment switching need
Tests must run on different URLs without changing code manually.Step 2: Use environment variables in config
SetbaseUrlincypress.config.jsusing environment variables to switch easily.Final Answer:
Use environment variables and setbaseUrldynamically incypress.config.js. -> Option AQuick Check:
Dynamic baseUrl via env vars = flexible testing [OK]
Hint: Use env vars to switch baseUrl without code changes [OK]
Common Mistakes:
- Editing config manually each time
- Avoiding baseUrl and writing full URLs everywhere
- Ignoring environment differences in tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
