我在用browser.addCommand()挣扎,我使用的是WebDriverIO版本6 +打字稿,当我尝试向wdio.conf.js添加命令并运行测试时,它失败,并显示错误“无法编译打字稿:”。
我的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文件:
declare module WebdriverIO {
interface Element {
waitAndClick: () => void;
}
}
wdio.conf.js文件:
before: function (capabilities, specs) {
browser.addCommand("waitAndClick", function () {
this.waitForDisplayed({timeout: 5000})
this.click()
}, true)
}
在页面对象中:
$('.classname').waitAndClick();
我能够看到页面对象中的方法,如上例所示。当我尝试运行时,它失败并显示错误“无法编译TypeScript:错误TS2339:类型'Element'上不存在属性'waitAndClick'。”
答案 0 :(得分:0)
我有同样的问题。 wdio.d.ts在运行时没有任何更改,我得到了TS2339。
每次调用这种方法时,到目前为止,我发现的唯一(坏的)解决方案就是“ @ ts-ignore”。
答案 1 :(得分:0)
我终于有了解决方法,您需要将wdio.d.ts添加到tsconfig.json中的types属性。
- "types": ["node", "@wdio/sync", "@wdio/jasmine-framework"]
+ "types": ["node", "@wdio/sync", "@wdio/jasmine-framework", "./wdio"]