Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I'm using WebDriver with Junit 4.11 and I want to assert that a checkbox is not selected by default, and to do that I'm unsure which method to choose.

The following is from the DOM before the checkbox is selected:

<input type="checkbox" id="c234" name="instantAd" value="true" class="t-checkbox-A">

Then, once the checkbox becomes selected a 'checked' is added, like so:

<input type="checkbox" id="c234" name="instantAd" value="true" checked="" class="t-checkbox-A">

I have tried the following:

WebElement checkBox = chrome.findElement(By.cssSelector("input.t-checkbox-A[name=\"instantAd\"]"));

    new WebDriverWait(chrome, 5).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.t-checkbox-A[name=\"instantAd\"]")));

    Assert.assertEquals("null",checkBox.getAttribute("checked"));

    checkBox.click();

    Assert.assertEquals("true",checkBox.getAttribute("checked"));

The first assertion fails. Perhaps this is because the 'checked' attribute isn't visible in the DOM yet, at a guess.

The stacktrace is displaying:

java.lang.AssertionError: expected: java.lang.String but was: null

I've searched many different posts but none offer me the answer Im looking for, and when checking http://junit.sourceforge.net/javadoc/org/junit/Assert.html for info and guidance (as being new to test automation, Im finding it difficult to work out what I need in my constructor.

Any help would be most appreciated.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

From the stacktrace it looks to me like the issue is assertEquals can't compare two different types, in this case a string and a null. I would suggest removing the quotes from "null" as that's what's causing it to be cast as a string.

Alternatively you could switch the assert to

Assert.assertNull(checkBox.getAttribute("checked"));

Also, in Selenium checkboxes are considered selected, you could also try asserting on that being false

Assert.assertFalse(checkBox.isSelected());
share|improve this answer
1  
Many thanks for the advice @Francis - by simply changing 'Assert.assertEquals("null",checkBox.getAttribute("checked"));' to your suggested 'assertNull method worked a treat. Brilliant! –  Django_Tester Dec 12 '13 at 10:08

You can create a small method to wrap around the getAttribute call
(Java-ish)

Bool isChecked(Webelement checkbox)
{
    if checkBox.getAttribute("checked") != Null:
        return True
    return False
}

OR
(python)

def isChecked(checkbox):
    return True if checkbox.getAttribute("checked") else False

And then you can use the Assert in almost the similar way that you are using

Assert.assertEquals(True,isChecked(checkbox))
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.