TestProject Forum
Powered by leading experts in the test automation community

The controlled "issue" in a try catch appear in the report as a failed step

Hi all, im new in test project. When i run a test i need to check that some modals are not present in the app, and if its present i click on the close button, for this i use a try catch, because if the element is not present selenium throws a no such element exception, but this code in the try is showed in the report as a failed step, how i can avoid this problem?
Code:

public void handleRewards() {
	NewLogger.info("HOME_PAGE: Checking if Rewards are displayed");
	if (returnIfElementIsDisplayed(this.buttonRewards, 6)) {
		clickOnElement(this.buttonRewards, "Rewards");
	} else {
		NewLogger.info("HOME_PAGE: Rewards not displayed");
	}
}

public boolean returnIfElementIsDisplayed(MobileElement element, int time) {
	boolean visible = false;
	try {
		new WebDriverWait(ThreadLocalDriver.getTLDriver(), time)
				.until(ExpectedConditions.visibilityOfAllElements(element));
		NewLogger.info("Element displayed");
		visible = true;
	} catch (Exception e) {
		NewLogger.info("Element not visible");
	}
	return visible;
}

You can disable the command reports in this method. It won’t effect explicit reports, only the automatic reports

public boolean returnIfElementIsDisplayed(MobileElement element, int time) {
        ThreadLocalDriver.getTLDriver().report().disableCommandReports(true);
        boolean visible = false;
        try {
            new WebDriverWait(ThreadLocalDriver.getTLDriver(), time)
                    .until(ExpectedConditions.visibilityOfAllElements(element));
            NewLogger.info("Element displayed");
            visible = true;
        } catch (Exception e) {
            NewLogger.info("Element not visible");
        }
        finally {
            ThreadLocalDriver.getTLDriver().report().disableCommandReports(false);
        }
        return visible;
    }
1 Like

Thaanks! its works! :smiley: