Search code examples
appiumui-automationwebdriver-io

WebdriverIO: Have waitForDisplayed() not fail a test if it returns false


I'm using WebdriverIO and Appium in Javascript to test Android/iOS apps.

I have a welcome screen that sometimes shows up after a loading screen. The following code is what I'm using at the moment to skip past the welcome screen.

if(welcomeScreenTitle.waitForDisplayed()){
   skipWelcomeScreenButton.click();
}

The problem that I'm having is that if the waitForDisplayed() times out (meaning that the screen hasn't showed up this time), it fails the test. Is there a way that I can do this?

I have tried using

browser.wait(10000);
if(welcomeScreenTitle.isDisplayed()){
   skipWelcomeScreenButton.click();
}

But the loading screen time is different depending on the speed of the connection (so might be much longer), and if the welcome screen shows up before 10 seconds, I don't want to wait the full 10 seconds (since most of the time it does show up).


Solution

  • One easier way of doing this is adding a try catch block around your code so that you can suppress the error thrown and continue the execution.

    try {
    browser.waitForDisaplyed(10000);
    if(welcomeScreenTitle.isDisplayed()){
       skipWelcomeScreenButton.click();
    }
    } catch (error) {
    console.log('Welcomescreen is not displayed.')
    }