Search code examples
typescriptxpathcypresscypress-cucumber-preprocessor

How to use xpath locator in Cypress with TypeScript project?


Initially, xpath plug-in was working good with my cypress project. Installed typescript plugin and Moved from javascript to Typescript. Now It started throwing an error on xpath

Property 'xpath' does not exist on type 'cy & EventEmitter'.ts(2339)

enter image description here

Please suggest , how to use xpath with typescript in Cypress project ? cypress-xpath": "^1.6.2", is included in package.json

package.json file is as below:

"devDependencies": {
    "@types/node": "^14.17.8",
    "add": "^2.0.6",
    "cypress-cucumber-preprocessor": "^4.3.0",
    "cypress-mochawesome-reporter": "^2.2.0",
    "cypress-xpath": "^1.6.2",
    "typescript": "^4.4.3",
    "yarn": "^1.22.10"
  },
  "cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": true
  }

Solution

  • For this to work you have to add this in your tsconfig.json file.

    {
      "compilerOptions": {
        "types": ["cypress", "cypress-xpath"]
      }
    }