Search code examples
javaseleniumappium

Assert Fails Even java.lang.AssertionError: expected [true] but found [false]


I get this error when i run it. I'm trying to run it and I changed return true and return false later. Do you know why it happens?

public static boolean elementIsPresent(MobileElement element) {
    try {
        element.isDisplayed();
    } catch (org.openqa.selenium.NoSuchElementException e) {
        return true;
    }
    return false;
}

public void checkbox() {
    try {
        Assert.assertTrue(elementIsPresent(this.CimonCheckBox));
        Log.log(driver).info("Passes matches Cimon Name");
        Assert.assertTrue(elementIsPresent(this.KurwaCheckbox));
        Log.log(driver).info("Passes matches names");
    } catch (Exception e) {
        Assert.fail("CheckBox: " + e.getMessage());
    }
}

Solution

  • The logic in your if statement is backwards. You're returning true if you get a NoSuchElementException and false otherwise. If you want to consider "is displayed" as "present" then I think your method should be:

    public static boolean elementIsPresent(MobileElement element) {
        try {
            return element.isDisplayed();
        } catch (org.openqa.selenium.NoSuchElementException e) {
            return false;
        }
    }
    

    or if you simply want to return true if it's present (regardless of whether it is displayed or not) then it can be:

     public static boolean elementIsPresent(MobileElement element) {
            try {
                element.isDisplayed();
            } catch (org.openqa.selenium.NoSuchElementException e) {
                return false;
            }
            return true;
        }