Verify the homepage loads and displays the main heading
Preconditions (2)
✅ Expected Result: The homepage loads without errors and the main heading displays the text 'Welcome to Our Site'
describe('First Cypress Test - Homepage', () => { it('should load the homepage and display the main heading', () => { cy.visit('http://localhost:3000'); cy.url().should('eq', 'http://localhost:3000/'); cy.get('h1').should('exist').and('have.text', 'Welcome to Our Site'); }); });
This test suite named First Cypress Test - Homepage contains one test case.
We use cy.visit() to open the homepage URL.
Then, cy.url().should('eq', ...) checks that the browser is on the correct page.
Next, cy.get('h1') finds the main heading element. The should('exist') assertion confirms it is present.
Finally, and('have.text', 'Welcome to Our Site') verifies the heading text matches exactly.
This approach uses clear selectors and assertions, making the test easy to read and maintain.
Now add data-driven testing to check the homepage heading for three different URLs with expected headings.
cy.visit() command in a Cypress test?cy.visit() usagecy.visit() command is used to open a webpage URL in the Cypress test browser.cy.visit() opens page [OK]cy.visit() with assertionsdescribe blocks to group tests and it blocks for individual tests, both with arrow functions.describe with an it inside using arrow functions. Others use invalid or unsupported syntax.#welcome is visible?describe('Visibility test', () => {
it('checks welcome message', () => {
cy.visit('https://example.com')
cy.get('#welcome').should('be.visible')
})
})#welcome is visible using should('be.visible').cy.visit() is present.cy.visit()should is wrong syntaxdescribe('My Test', () => {
it('opens page', () => {
cy.visit('https://example.com')
cy.get('.button').should('visible')
})
})should('visible') is incorrect. The correct form is should('be.visible') to check visibility..button is correct for class, and it block is present. So only the assertion keyword is wrong.https://example.com, clicks a button with class .submit-btn, and then checks if a success message with id #success-msg is visible. Which of the following test codes correctly implements this?.submit-btn for class and #success-msg for id. It uses cy.get().click() correctly.should('be.visible'), which is the correct assertion for visibility. Other options have wrong selectors or incorrect assertion keywords.Pass the test and this pattern moves unknown