运行 Cypress 并在 Cypress 中使用 xpath,我正在 .js 文件中尝试以下代码。
/// <reference types = "cypress" />
describe ("Test Contact us form",()=>{
it("Should be able to submit the form", ()=>{
cy.visit('some url');
cy.xpath('//a[contains (@href, "contact")]').click();
});
})
这就是我的 xpath node_modules 目录路径的样子
\Projects\node_modules\xpath
这是我的index.js
// Alternatively you can use CommonJS syntax:
// require('./commands')
require('xpath')
这是我的 package.json
{
"name": "projects",
"version": "1.0.0",
"description": "test",
"main": "index.js",
"scripts": {
"test": "Thisistest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"cypress": "^5.2.0",
"xpath": "0.0.29"
}
}
这是 package-lock.json 的片段
"xpath": {
"version": "0.0.29",
"resolved": "https://registry.npmjs.org/xpath/-/xpath-0.0.29.tgz",
"integrity": "some key",
"dev": true
},
运行测试后,我收到以下编译错误。
它是一个类型错误。
cy.xpath is not a function
似乎是一个小配置问题。但是,请按照 https://github.com/cypress-io/cypress-xpath#readme 上给出的确切步骤进行操作。
最佳答案
我通过 git bash 使用 npm 删除并重新设置了 cypress 和 xpath,它成功了。
以前,我使用 node.js 命令提示符进行设置。使用相同的npm命令安装xpath后,xpath已成功下载,但是node_modules内的目录名称只是xpath而不是cypress-xpath。现在,即使我在index.json文件下有require('xpath'),它仍然无法检测到xpath。
关于Cypress 无法识别 Xpath 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64069758/