Cypress provides various functions and commands for writing automated tests effectively. Below is a categorized list of the most commonly used Cypress functions with explanations and examples.
1. Visiting and Navigating Pages
cy.visit(url)
Navigates to a specific URL.
- Can also pass options like authentication headers.
cy.go()
Navigates forward or backward in browser history.
cy.reload()
Reloads the current page.
2. Selecting Elements
cy.get(selector)
Selects elements using a CSS selector.
cy.contains(text)
Finds elements containing specific text.
cy.find(selector)
Finds a child element inside a parent.
cy.within()
Runs commands inside a specific DOM element.
3. Interacting with Elements
cy.type(text)
Types text into an input field.
cy.clear()
Clears the input field.
cy.click()
Clicks on an element.
cy.check()
/ cy.uncheck()
Checks and unchecks checkboxes or radio buttons.
cy.select()
Selects a dropdown option.
4. Assertions (Validations)
cy.should()
Asserts conditions on elements.
cy.expect()
General assertion function.
cy.should('not.exist')
Checks if an element does NOT exist.
5. Handling Network Requests
cy.intercept(method, url)
Intercepts API calls and mocks responses.
cy.wait(alias)
Waits for an intercepted API request to finish.
6. Handling Cookies and Local Storage
cy.setCookie(name, value)
Sets a browser cookie.
cy.getCookie(name)
Gets a cookie value.
cy.clearCookies()
Clears all cookies.
cy.setLocalStorage(key, value)
Sets a value in local storage.
cy.getLocalStorage(key)
Gets a value from local storage.
7. Debugging & Logs
cy.pause()
Pauses test execution at a specific point.
cy.debug()
Prints debugging information.
cy.log(message)
Logs custom messages to the Cypress command log.
8. Running Tests in CI/CD
cy.screenshot()
Takes a screenshot of the current state.
cy.task()
Runs Node.js functions within Cypress.
9. Running Cypress Tests
Run Cypress in interactive mode:
Run tests headlessly:
Run specific test file:
Conclusion
Cypress provides a comprehensive API for testing web applications efficiently. By using its built-in commands for navigation, element selection, interactions, assertions, and debugging, you can write reliable automated tests.
Comments
Post a Comment