1

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.

Community
  • 1
  • 1
some_other_guy
  • 3,364
  • 4
  • 37
  • 55
  • Look at the answer below. As for why this happens, you are catching the exception - so the test runner will not realise one was raised and thus believe it passed. There are several assertions methods, including assertTrue and assertFalse: http://junit.sourceforge.net/javadoc/org/junit/Assert.html – Arran Jul 23 '12 at 21:06

2 Answers2

4

Answer 1: To use assert true or false you should use if else instead and then call the function by assert e.g.

public boolean compareWidthPixels(By by, String expected) {
    System.out.println(driver.findElement(by).getCssValue("width"));
    if (driver.findElement(by).getCssValue("width").equals(expected)){
        System.out.println("Width as expected");
        return true;
    } 
    System.out.println("Width incorrect");
    return false;
}

Then use compareWidthPixels as 'AssertTrue(compareWidthPixels(by, expected))' in your test

Similarly for 2nd question you can use following

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}

using is element in assertions.

Prashant Shukla
  • 1,391
  • 9
  • 18
  • Is it not better to check whether the element is displayed or not??? Something like - boolean visible = driver.findElement(By.xpath(locator)).isDisplayed(); – some_other_guy Oct 01 '12 at 09:30
  • @Maitreya, boolean visible = driver.findElement(By.xpath(locator)).isDisplayed(); is also a way to check element availability. – Anand Somani Dec 26 '12 at 17:41
  • @Prashant, Is it a good way to have Assert statement in testcase? or we can have assert statement in functions. Can you please tell which is best practice. – Anand Somani Dec 26 '12 at 17:42
0

Add Assert.fail(); in your catch block.

Example:

try {
    ----- your code -----

} catch(Exception e) {
    Assert.fail();
}

Hope this will solve your problem

Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41