How to fix flakiness in test cases

I am working on Samsung tab, through appium on intellij, using Java on windows machine. I have incorporated Testng. I have automated end to end workflow, comprising of a huge scenario. This test case executes for 18 iterations, where each iteration comprises of multiple test data for one work flow.

My issue is that, during each complete execution, one or the other iteration fails, at a random step, where it fails to click on a particular element, or find that element. This same step passes in the next iteartion, without fail. A random iteration would fail in one execution, while pass in another, and so on.

I have also incorporated a (supposedly robust) waitforvisibility() method for each element, as follows:

public void waitForVisibility(WebElement e){
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TestUtils.WAIT));
int retries = 3;
while (retries > 0) {
try {
wait.until(ExpectedConditions.visibilityOf(e));
break;
} catch (StaleElementReferenceException ex) {
retries–;
log.warn("Stale element reference exception caught, retrying now. Retries remaining: " + retries);
}
}
}

But these random issues are not allowing my framework to be robust. How to make it automation error free?

  1. How you looking for elements?
  2. Change your wait function to work with locator instead of element
1 Like