
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
cypress-xpath
Advanced tools
The cypress-xpath package is an extension for Cypress that allows you to use XPath selectors in your Cypress tests. This can be particularly useful for selecting elements in complex DOM structures where CSS selectors might be cumbersome or insufficient.
XPath Selector
This feature allows you to use XPath expressions to select elements in the DOM. In this example, the XPath expression selects a button element with the text 'Submit' and performs a click action on it.
cy.xpath('//button[text()="Submit"]').click();
Chaining with Cypress Commands
You can chain XPath selectors with other Cypress commands. In this example, the XPath expression selects a div element with the class 'container', and then the find method is used to locate a button within that container and click it.
cy.xpath('//div[@class="container"]').find('button').click();
XPath with Variables
You can use variables within your XPath expressions. This example demonstrates how to use a variable to dynamically select a button based on its text content.
const buttonText = 'Submit';
cy.xpath(`//button[text()="${buttonText}"]`).click();
The cypress-selectors package provides a way to use custom selectors in Cypress tests, including XPath. It offers a more flexible approach to element selection compared to cypress-xpath, allowing for the use of both CSS and XPath selectors interchangeably.
The cypress-testing-library package extends Cypress's capabilities by providing a set of utilities for querying the DOM in a way that resembles how users interact with it. While it does not support XPath, it offers a more user-centric approach to element selection, which can be more intuitive for certain types of tests.
Adds XPath command to Cypress.io test runner
npm install -D cypress-xpath
yarn add cypress-xpath --dev
Then include in your project's support file
require('cypress-xpath');
After installation your cy
object will have xpath
command.
it('finds list items', () => {
cy.xpath('//ul[@class="todo-list"]//li').should('have.length', 3);
});
You can also chain xpath
off of another command.
it('finds list items', () => {
cy.xpath('//ul[@class="todo-list"]').xpath('./li').should('have.length', 3);
});
As with other cy commands, it is scoped by cy.within()
.
it('finds list items', () => {
cy.xpath('//ul[@class="todo-list"]').within(() => {
cy.xpath('./li').should('have.length', 3);
});
});
note: you can test XPath expressions from DevTools console using $x(...)
function, for example $x('//div')
to find all divs.
In XPath the expression // means something very specific, and it might not be what you think. Contrary to common belief, // means "anywhere in the document" not "anywhere in the current context". As an example:
cy.xpath('//body').xpath('//script');
You might expect this to find all script tags in the body, but actually, it finds all script tags in the entire document, not only those in the body! What you're looking for is the .// expression which means "any descendant of the current node":
cy.xpath('//body').xpath('.//script');
The same thing goes for within:
cy.xpath('//body').within(() => {
cy.xpath('.//script');
});
This explanation was shamelessly copied from teamcapybara/capybara.
To properly load the types for cy.xpath
command, add to your spec file the following comment.
/// <reference types="cypress-xpath" />
If using TypeScript, add cypress-xpath
to the list of types to be loaded in tsconfig.json
{
"compilerOptions": {
"types": ["cypress", "cypress-xpath"]
}
}
For more, see Intelligent Code Completion
This project is licensed under the terms of the MIT license.
To update the Cypress version badge in this README file run
$ npm run badges
FAQs
Adds XPath command to Cypress test runner
The npm package cypress-xpath receives a total of 286,008 weekly downloads. As such, cypress-xpath popularity was classified as popular.
We found that cypress-xpath demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.