5
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

Gilad Shnoor
  • 362
  • 2
  • 12
  • 1
    Hey Gilad! Well, glad you found a fix for your issue. You don't ***have*** to use `sync: true` flag, in your scenario the problem seams to be you clicking on an `ELEMENT` object (`telm` value), which of course will trigger a `telm.click is not a function` TypeError. I'd put a `browser.debug()` after the `const telm = await t(elm);` statement and use the result to click the elem ( *hint, hint! :)* ). Cheers! – iamdanchiv Dec 26 '18 at 06:55

1 Answers1

-1

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

Laura
  • 5,088
  • 3
  • 26
  • 37
dmle
  • 3,056
  • 1
  • 10
  • 20
  • I am trying to use the click() function (as documented the click.js example in http://webdriver.io/api/action/click.htm) of the element I am getting after passing it to t. Before passing it to t it has a click function and after getting it back it does not. – Gilad Shnoor Dec 23 '18 at 08:39
  • your function has incorrect implementation, maybe you want smth like https://stackoverflow.com/a/45855452/4103101 – dmle Dec 24 '18 at 10:17