2

I'm using the following code to run a simple test. The same code is working sometimes and returning error sometimes. The error it will return is "Not able to find UI Element".

This is my code:

package example;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.AppiumDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.File;
import java.net.URL;

public class CalculatorTest {
AppiumDriver<WebElement> driver;

@BeforeClass
public void setUp() throws Exception {

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("deviceName","Android");
    capabilities.setCapability("platformVersion", "6.0");
    capabilities.setCapability("app", "C:/Users/rraghunath/Downloads/Apk/Calculator_com.google.android.calculator.apk");
    capabilities.setCapability("appPackage", "com.google.android.calculator");
    capabilities.setCapability("appActivity", "com.android.calculator2.CalculatorGoogle");

    driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}

@AfterClass
public void tearDown() throws Exception {
    driver.quit();
}

/**
* Test for Calculator
*/
@Test
public void test_CalculatorTest(){

   driver.findElement(By.id("digit_1")).click();
   driver.findElement(By.id("digit_0")).click();
   driver.findElement(By.id("digit_1")).click();
   driver.findElement(By.id("del")).click();
   driver.findElement(By.id("del")).click();
   driver.findElement(By.id("del")).click();
   driver.findElement(By.id("digit_1")).click();
   driver.findElement(By.id("dec_point")).click();
   driver.findElement(By.id("digit_0")).click();
   driver.findElement(By.id("digit_2")).click();
   driver.findElement(By.id("digit_3")).click();
   driver.findElement(By.id("op_mul")).click();
   driver.findElement(By.id("digit_3")).click();
   driver.findElement(By.id("dec_point")).click();
   driver.findElement(By.id("digit_0")).click();
   driver.findElement(By.id("digit_1")).click();
   driver.findElement(By.id("digit_2")).click();
   driver.findElement(By.id("eq")).click();

}

}

5 Answers 5

1

You need to wait for the elements to be visible or clickable before clicking on them. It is possible that the application is sometimes a bit slower with drawing and that the element is just not there yet when it tries to click.

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("digit_1"));
driver.findElement(By.id("digit_1")).click();

This will wait for 30 seconds until the digit_1 element is clickable. Often waiting for the first element on the page is enough, for each transition to a new page use a new wait.

Code sample from: https://stackoverflow.com/questions/28799420/how-to-wait-to-activity-using-appium-on-begin-and-during-test-itself

0

Agree - wait should be introduce before any action. Best practices will be to use implicit wait

  • Each time it wont happen that element will be Clickable

  • I'll suggest to use "ExpectedConditions.visibility" instead of "ExpectedConditions.elementToBeClickable"

  • This solution will work for each and every scenario irrespective of action

    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.visibilityOf(By.id("digit_1"));
    driver.findElement(By.id("digit_1")).click();
    
6
  • The best practise is to NOT use implicit wait, but explicit. implicit has all kinds of issues. sqa.stackexchange.com/questions/12583/… Dec 9, 2016 at 9:19
  • Using element visibility is also not a good idea when you want to click it. It could be visible but not clickable, because some animation is still happening. This has been the cause of a lot of flaky tests in the past in my experience. Dec 9, 2016 at 9:20
  • Implicit wait : Its having issues is different case, dont you think? It becomes bad practices when one is keeping max. seconds. But what those who are using smaller delays? Dec 9, 2016 at 9:58
  • For this question : Anywhere you seen "click" is failing is addressed? What I am concern about is "ExpectedConditions.visibility" is more generalize and can be use many time for all elements instead of "ExpectedConditions.elementToBeClickable" Dec 9, 2016 at 10:00
  • Implicit has way more issues: stackoverflow.com/questions/10404160/… In the end it always about choise, pick what works for you, but be aware of possible consquences. Dec 9, 2016 at 10:16
0

FluentWait wait= new FluentWait (d); Wait.pollingEvery (200,TimeUnit.MILLISECONDS); wait.withTimeouts (5,TimeUnit.SECONDS); wait.ignoring (noSuchElementException.class); wait.until (ExpectedCondtions.visibilityOf (By.id ("digit_1"));

-1

If that program is all about waiting statement. I would suggest u should go for implicit wait E.g-

driver.manage().timeouts ().implicitlyWait (20,TIMEUNIT.SECONDS);
5
  • This is a bad practise, maybe easier to implement, but this will leads to issues, like slower test runs. sqa.stackexchange.com/questions/12583/… Dec 9, 2016 at 9:21
  • Then you should go for
    – satyajeet
    Dec 9, 2016 at 9:56
  • Then you should go for FLUENT WAIT
    – satyajeet
    Dec 9, 2016 at 9:57
  • If you think the syntax is to verbose, it is also what the linked question suggests Dec 9, 2016 at 10:14
  • FluentWait <webDriver> wait= new FluentWait <webDriver>(d);
    – satyajeet
    Dec 9, 2016 at 10:43
-2

What about adding Thread.Slepp(<time from milliseconds >) before starting the find elemant commands.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.