Webdriverio TypeError: element.click is not a function

Viewed 7897
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);
}

I am trying to automate a android app with appium and webdriverio and I am having a very odd bug. I use the $ function (it happens with the element function as well) of webdriver to locate an element which I then pass to the function t. When I get it back it is a different obj.

I tried to add a delay between the first and second line in getByResourceId to make sure that it wasn't a timing bug:

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)
        }

That didn't work. The thing that breaks elm is t returning a promise. Does anyone have any ideas how to get this to work?

edit: I found this https://stackoverflow.com/a/47176108/10816010 to be very helpful. apparently I had to use the synchronous approach (using the WDIO test-runner) and let the WDIO test-runner control the synchronizing instead of using async await in order to get the functionality I wanted.

edit 2: this is not relevant in version 5 of webdriverio

1 Answers

Assuming you start driver like:

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

You can use async-retry:

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
  })
}

And you can check wdio examples here

Related