@cypress/xpath

中文文档
2.0.3发布于 2022/10/27MITTypeScriptCommonJS零依赖近 30 天下载 11.9万

Adds XPath command to Cypress.io test runner

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

原始使用文档
文档目录5 节
  1. Install with npm
  2. Install with Yarn
  3. Use
  4. Beware the XPath // trap
  5. License

@cypress/xpath

Adds XPath command to Cypress.io test runner

Install with npm

npm install -D @cypress/xpath

Install with Yarn

yarn add @cypress/xpath --dev

Then include in your project's support file

require('@cypress/xpath');

Use

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.

See cypress/e2e/spec.cy.js

Beware the XPath // trap

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');
});

For more, see Intelligent Code Completion

License

This project is licensed under the terms of the MIT license.

版本与发布记录

@cypress/xpath 共发布 2 个版本,首次发布于 2022/09/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.3latest2022/10/274 年前
2.0.22022/09/304 年前

关于 @cypress/xpath 的常见问题

@cypress/xpath 是什么?

Adds XPath command to Cypress.io test runner注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 @cypress/xpath?

在项目目录运行 npm install @cypress/xpath,或使用 pnpm add / yarn add / bun add。

@cypress/xpath 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cypress/xpath 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cypress/xpath 可以商用吗?

@cypress/xpath 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cypress/xpath 的下载量有多少?

根据 npm 官方下载统计,@cypress/xpath 在 2026/10/11 – 2026/11/09 这 30 天内被下载 118,673 次,日均约 3,956 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cypress/xpath 还在维护吗?

最新版本 2.0.3 发布于 2022/10/27(4 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/09/30。作者已将其标记为弃用,新项目建议寻找替代方案。