2

I am doing an automation testing of one application and I am facing one scenario to verify Check-box are checked or not using automation code

my web application is developed in angular js and developer used Checked attribute

I tried with .isselected but it was not working for me.

1
  • Can you post your html code for the check box? Commented Apr 2, 2016 at 5:26

4 Answers 4

1

You could use a selector that includes the ".ng-not-empty" or "ng-empty" class:

List<WebElement> eltsUnchecked = driver.findElements(By.cssSelector("input[ng-model='checkboxModel.value1'].ng-empty"));
bool isNotCheched = eltsUnchecked.size() > 0;
if (isNotCheched) {
  // check the checkbox
  eltsUnchecked.get(0).click();
}

List<WebElement> eltsChecked = driver.findElements(By.cssSelector("input[ng-model='checkboxModel.value1'].ng-not-empty"));
bool isCheched = eltsChecked.size() > 0;
if (isCheched) {
  // uncheck the checkbox
  eltsChecked.get(0).click();
}

Or by checking the presence of ".ng-not-empty" or "ng-empty" in the class attribute :

WebElement checkbox = driver.findElement(By.cssSelector("input[ng-model='checkboxModel.value1']"));
bool isCheched = checkbox.getAttribute("class").contains("ng-not-empty");
bool isNotChecked = checkbox.getAttribute("class").contains("ng-empty");
if (isCheched) {
  // uncheck the checkbox
  checkbox.click();
} else if (isNotChecked) {
  // check the checkbox
  checkbox.click();
}

But if you are using the "checked" attribute instead then:

WebElement checkbox = driver.findElement(By.cssSelector("input[ng-model='checkboxModel.value1']"));
bool isCheched = checkbox.getAttribute("checked").length > 0;
bool isNotChecked = checkbox.getAttribute("checked").length == 0;
Sign up to request clarification or add additional context in comments.

Comments

0

Hi when you want to verify if a Check-box is selected or not then please pay attention that any input tag with type checkbox has a hidden attribute value known as selected if a Check-box is selected then its value is = True and if not then its value is = null,hence on the basis of this you can easily identify which Check-box is selcted or not .below find a working example for the same :

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("C:\\Users\\rajnish\\Desktop\\myCheckox.html");

// working with check boxes
// first take all the check boxes inside a list like below
List<WebElement> mycheckBox = driver.findElements(By.xpath("//*[@type='checkbox']"));

// apply the for loop to identify/ verify if a check boxes is selected or not
for(int i=0;i<mycheckBox.size();i++){
    System.out.println("attribut value Selected of Check-box is : " + mycheckBox .get(i).getAttribute("selected"));
    // if Check-box is selected then value of selected attribute is True else null
    if( mycheckBox.get(i).getAttribute("selected").equals("null")){
        // if loop will only run when value of selected attribute is null
        // i.e only when  Check-box is not selected 
        mycheckBox .get(i).click();
    }
}

Comments

0

Use is selected function

WebElement el = driver.findElement(By.id("element Id"));
el.click()
el.isSelected()

2 Comments

I think it's pretty clear that OP said isSelected is NOT working for him.
Yes, in that case need html source code,might be that is customized dropdown using js
0

Use isSelected instead of isselected as shown in the following code.

Assert.assertEquals(wd.findElement(By.[your element locater])).isSelected(),true);

I have successfully used it in my code and its working fine. as below:

Assert.assertEquals(wd.findElement(By.xpath("//input[@id='chkremember']")).isSelected(),true);

1 Comment

isSelected didn't work for the OP. I think that it works for me is not a valid answer here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.