I'm checking the presence of a button with assertTrue().
Assert.assertTrue(isElementPresent(By.cssSelector(or.getProperty("addCustomerButton"))));
addCustomerButton contains the locator of the button.
isElementPresent() code is:
public boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch(NoSuchElementException e) {
return false;
}
}
When the findElement() in the try block of isElementPresent() method doesn't find the element, findElement() must throw the exception NoSuchElementException. I caught the exception in the catch block. If the isElementPresent() returns true, the assertion is true and there will be no AssertionError thrown. If the isElementPresent() return false, then the assertion is false and the AssertionError should be thrown, right?
But in my script above, the AssertionError is not being thrown. the test is getting marked as PASSED.
Update:
After repeated modifications and executions, AssertionError is thrown.
Test — failed
com.datadriven.testcases.BankManagerLoginTest loginAsBankManager 1542342446849 10126
Test
com.datadriven.testcases.BankManagerLoginTest#loginAsBankManager
Exception
java.lang.AssertionError: expected [true] but found [false]
at com.datadriven.testcases.BankManagerLoginTest.loginAsBankManager(BankManagerLoginTest.java:17)
... Removed 29 stack frames
I've changed the locator from button[ng-click='addCust()'] to button[ng-click='addCus']