0

I am struggling with browser.addCommand(), I use WebDriverIO Version 6 + typescript, and when I try to add a command to wdio.conf.js and run the test it fails with error "Unable to compile TypeScript:"

my ts.confg:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "*": [
        "./*"
      ],
      "src/*": [
        "./src/*"
      ],
      "test/*": [
        "./test/*"
      ]
    }, 
    "sourceMap": false,
    "target": "es6",                         
    "module": "commonjs",  
    "typeRoots": ["./types"],                       
    "types": [
      "node",
      "@wdio/sync",
      "@wdio/jasmine-framework"
   ],                           

  "include": ["./test/**/*.ts","./types/wdio.d.ts"],
  "exclude": [
    "./node_modules"
  ],
}

wdio.d.ts file:

declare module WebdriverIO {
  interface Element {
    waitAndClick: () => void;
  }
}

wdio.conf.js file:

before: function (capabilities, specs) {
        browser.addCommand("waitAndClick", function () {
        this.waitForDisplayed({timeout: 5000})
        this.click()
     }, true)
 }

in Page Object:

$('.classname').waitAndClick();

I am able to see the method in page object as in the example above. When I trying to run it fails with error "Unable to compile TypeScript: error TS2339: Property 'waitAndClick' does not exist on type 'Element'."

Alex
  • 11
  • 3
  • Typescript likely does not know that function exists, and you will need to create a definition for it. However, you need to share the error you are getting in your question. Use the edit button to amend it. – Evert Aug 04 '20 at 20:21

2 Answers2

0

I have the same problem. wdio.d.ts does not change anything at run time, I get the TS2339.

Only (bad) solution I found so far is plucking '@ts-ignore' each time I call such a method.

Chrism
  • 11
  • 1
0

I finally have a solution, you need to add wdio.d.ts to types property in tsconfig.json.

-      "types": ["node", "@wdio/sync", "@wdio/jasmine-framework"]
+      "types": ["node", "@wdio/sync", "@wdio/jasmine-framework", "./wdio"]
Alex
  • 11
  • 3