Score 100% on the quiz to continue
Visiting web pages
Use cy.visit() to navigate to the application under test.
When writing automated tests for web applications, the first step is to visit the URL of the application under test.
For this, Cypress offers the cy.visit() command.
To visit a web page with Cypress, simply pass the desired URL as a string to the cy.visit() command.
For example: cy.visit('https://google.com').
A good place to put the cy.visit() command is inside the callback function of the beforeEach() hook.
For example:
beforeEach(() => {
cy.visit('https://example.com')
})
This way, you don't need to repeat the cy.visit() command inside each test case, as it will be executed before each it block.
Commands overview 📖
cy.visit()
Navigates to a URL and waits for the page to fully load before continuing.
Syntax:
cy.visit(url)
cy.visit(url, options)Example:
cy.visit('https://example.com')👨🏫 If you set abaseUrlincypress.config.js, you can pass a relative path tocy.visit()(e.g.,cy.visit('/')) instead of repeating the full URL in every test. This makes your tests shorter and easier to point at a different environment.
Suggested content 📚
Exercise 🎯
Try updating your test suite to visit the Cypress Playground URL before each test case.
🙊 Put thecy.visit()call inside abeforeEach()hook so it runs before everyitblock automatically.
From the Cypress App, run the tests to ensure your implementation works.
If the Cypress App isn't open, runnpm run cy:opento open it.
Show the world what you have learned 🌎
To show your professional network what you have learned in this lesson, post the following on LinkedIn.
I am taking the "Cypress Testing Lab" course by @Walmyr Lima e Silva Filho at the @Talking About Testing school, where I learned how to visit web pages with Cypress, as well as how to avoid repetitive code using thebeforeEachhook. #TalkingAboutTesting #TATSchool #CypressTestingLab #CypressPlayground #Cypress
👨🏫 Remember to tag me in your post. Here is my LinkedIn profile.
Quiz
What is the recommended place to put the `cy.visit()` command to avoid repeating it in each test case?
Score 100% on the quiz to continue