Webdriverio TypeError:element.click不是函数

Gil*_*oor 5 node.js webdriver-io

async function t(e){
    return e;
}

async getByResourceId(id, wait= 5000){
        const elm = this.driver.$('android=new UiSelector().resourceId("'+id+'")');
        const telm = await t(elm);
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用appium和webdriverio使android应用程序自动化,但我遇到了一个非常奇怪的错误。我使用webdriver的$函数(也发生在element函数中)来查找元素,然后将其传递给函数t。当我找回来的时候,它是一个不同的对象。

我试图在getByResourceId的第一行和第二行之间添加延迟,以确保它不是计时错误:

async getByResourceId(id, wait= 5000){
            const elm = this.driver.$('android=new UiSelector().resourceId("'+id+'")');
            await _setTimeout(5000);
            //elm still OK (aka elm.click works)
            const telm = await t(elm);
            //telm is broken (aka getting TypeError: telm.click is not a function)
        }
Run Code Online (Sandbox Code Playgroud)

那没用。打破榆树的事情是没有兑现诺言。有谁知道如何使它起作用?

编辑:我发现这个/sf/answers/3302327591/非常有用。显然,我必须使用同步方法(使用WDIO测试运行程序),并且让WDIO测试运行程序控制同步,而不是使用异步等待,以便获得所需的功能。

编辑2:这与webdriverio的版本5不相关

小智 -2

假设您启动驱动程序如下:

const driver = await remote({
   port: 4723,
   logLevel: 'debug',
   desiredCapabilities: {
     // your caps here
   }
})
Run Code Online (Sandbox Code Playgroud)

您可以使用异步重试

async getByResourceId(id, wait=5000){
  return await retry(async bail => {
    const el = await driver.element(`android=new UiSelector().resourceId("${id}")`)
    return el;
  }, {
    retries: 3,
    minTimeout: wait,
    driver: driver
  })
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里查看 wdio 示例