0

first, i need to check if the element com.simplemobiletools.gallery:id/text_view is displayed within a timeframe of 2-3 seconds. Second, if it IS displayed, click the element android:id/button2, but if it ISNT displayed, keep running code.

What commands do I use to do this? Thank you in advance.

  • Possible duplicate of [What Are Some Best Practices When Asserting iOS Elements Are Displayed?](https://stackoverflow.com/questions/56008481/what-are-some-best-practices-when-asserting-ios-elements-are-displayed) – Suban Dhyako May 20 '19 at 09:13

2 Answers2

0

Create a method like following:

public boolean isElementDisplayed(MobileElement el){
     try{
        return el.isDisplayed();
     }catch(Exception e){
        return false;
     }
}

Then you can check if the element is displayed by calling above method:

MobileElement element = driver.findElementById('com.simplemobiletools.gallery:id/text_view');
boolean isElementVisible = isElementDisplayed(element);
if(isElementVisible){
   //click element
   driver.findElementById("android:id/button2").click();
}else{
   //element is not visible... continue Test

}

If you don't use try catch, then the exception will be thrown when element is not found.

Suban Dhyako
  • 2,436
  • 4
  • 16
  • 38
  • how the solution ensure that element is displayed in a span of 3 seconds as asked in first point of the question. – Amit Jain May 22 '19 at 17:09
0

a. wait for element for 3 sec with polling of 1 sec or less as required

b. if exception occured means element is not displayed in 3 sec

c. check if webelement is not null then only click it

try {
            WebElement textView = 
             driver.findElement(By.id("com.simplemobiletools.gallery:id/text_view"));
            new FluentWait<WebElement>(textView)
                .withTimeout(3, TimeUnit.SECONDS)
                .pollingEvery(1, TimeUnit.SECONDS)
                        .until(new Function<WebElement, Boolean>() {
                            @Override
                            public Boolean apply(WebElement element) {
                                return element.isDisplayed();
                            }
                        });
        }
catch(TimeoutException e){
 System.out.println("TimeoutException exception occured after 3 sec);
}

if (textView !=null){
// we do not bother about time here, 
// just in case findElement returned something we can click here
driver.findElement(By.id("android:id/button2")).click();
}
Amit Jain
  • 4,389
  • 2
  • 18
  • 21