Navigate to the homepage using cy.visit()
Preconditions (1)
✅ Expected Result: The browser navigates to the homepage URL and the page shows the header text 'Welcome to Our Site'
describe('Homepage Navigation Test', () => { it('should navigate to homepage and verify header', () => { cy.visit('http://localhost:3000'); cy.url().should('eq', 'http://localhost:3000/'); cy.contains('h1', 'Welcome to Our Site').should('be.visible'); }); });
This test uses cy.visit() to open the homepage URL. Then it checks the URL with cy.url().should('eq', ...) to confirm navigation succeeded. Finally, it looks for an h1 header containing the exact text 'Welcome to Our Site' and asserts it is visible on the page. Cypress automatically waits for elements to appear, so no manual waits are needed.
Now add data-driven testing to navigate to three different URLs and verify their headers
cy.visit() in Cypress testing?cy.visit()cy.visit() is used to open a web page URL in the browser for testing.cy.visit().cy.visit() with the URL as a string argument.cy.visit('https://example.com') matches the correct Cypress syntax. Others are invalid commands.cy.visit('https://example.com', { timeout: 10000 })timeout option sets how long Cypress waits for the page to load before failing.cy.visit('https://example.com')
cy.get('#submit').click()cy.get(), the element may not exist yet causing failure.cy.visit() returns a chainable, you can use .then() to run assertions after the page loads.url() directly on cy.visit(), which is invalid. cy.visit('/dashboard', { onLoad: () => cy.url().should('include', 'dashboard') }) uses a non-existent onLoad option. cy.visit('/dashboard').should('contain', 'dashboard') tries to assert page content with should('contain') on cy.visit(), which is invalid. cy.visit('/dashboard').then(() => cy.url().should('include', 'dashboard')) correctly waits for visit to complete, then asserts URL includes 'dashboard'.Pass the test and this pattern moves unknown