I am back with more silly questions
1) How to fail a test if AssertEquals is false?
I have this code –
public boolean compareWidthPixels(By by, String expected) {
System.out.println(driver.findElement(by).getCssValue("width"));
try {
assertEquals(expected, driver.findElement(by).getCssValue("width"));
System.out.println("Width as expected");
return true;
} catch (Error e) {
verificationErrors.append(e.toString());
System.out.println("Width incorrect");
return false;
}
This code displays “Width incorrect” when the width does not match the expected value but the test case passes. I want the test to fail if the width are unequal.
2) How to assert/verify an Element is NOT present?
As a novice I tried many things I found in Google and here in Stack Overflow –
Assert that a WebElement is not present using Selenium WebDriver with java, Selenium WebDriver – Test if element is present, etc. But none of them worked. I am working with JUnit4 and need a function that should pass if the element is not present.
Thanks
Maitreya
P.S: Please feel free edit this question if it looks confusing or disoriented.
Answer 1: To use assert true or false you should use if else instead and then call the function by assert e.g.
Then use compareWidthPixels as ‘
AssertTrue(compareWidthPixels(by, expected))‘ in your testSimilarly for 2nd question you can use following
using is element in assertions.