Page 1 of 1

How to check the state of a checkbox

Posted: Fri Apr 25, 2014 2:21 pm
by houseofcutler
Hi - I know that Googling returns a lot of results for this but I have not been able to fully answer my specific issue.

I am aware of the 'Set Value' function which can be used to set the value of a check box to True or False. I have used this in some of my test scripts.

Because of the way that this webpage works it is necessary to actually click on a check box in order to trigger an information window to appear (this does not happen when simply changing the status).

If the status of a check box was checked and I set the value to false and then click on the check box - The page does not behave in the correct way as it never really registered that the check box was false (only the attribute was changed)

This means that to achieve the desired results I must be able to validate whether a check box is checked and depending on this then perform the appropriate actions. Using an If Statement in C# ideally.

I am struggling to find the best way to do this. When I have tried I get errors because boolean cannot be changed to string and visa versa etc. I found the following code example in the user guide. but am not sure exactly how to use this with my ranorex path elements
// Search for checkbox within .NET application
CheckBox checkBox = "/form[@controlname='TestedApp']
/checkbox[@controlname='checkBox1']";
// Read the current state of a checkbox
Console.WriteLine(checkBox.Text + " State: " +checkBox.CheckState.ToString());
// Check if the checkbox is not checked
if (!checkBox.Checked)
checkBox.Checked = true;
If someone could provide me with the simplest way to do an IF statement that will actually be able to tell if an element is checked that would be brilliant.

Thanks you VERY much

Ben

Re: How to check the state of a checkbox

Posted: Fri Apr 25, 2014 3:34 pm
by mzperix
Hi houseofcutler,

If you want to use the element in repository, then here is what you should do:
- get the checkbox based on repository
- look on the state of the checkbox
- perform proper action

If you use user code, then the repository instance is available if you type in "repo".
//create the checkbox from the repository
Ranorex.CheckBox chkbox = repo.somepage.checkboxname;

//Show the checked state in report
Report.Info(chkbox.Checked.ToString());
            
//if checkbox is checked, the click on it, else double click on it
if (chkbox.Checked)
{
    chkbox.Click();
}else{
    chkbox.DoubleClick();
}

//doing the same with CheckState property
if (chkbox.CheckState == System.Windows.Forms.CheckState.Checked)
{
    chkbox.Click();
}else{
    chkbox.DoubleClick();
}
Here are some information about using repository in code: http://www.ranorex.com/support/user-gui ... html#c3196

One note: in your example code the
if (!checkBox.Checked)
checkBox.Checked = true;
does the following: if the checkbox is NOT checked, then check the checkbox. The "!" is the NOT operator. checkbox.Chekced is a boolean property, which is eiter true or false, so no need to compare to anything.

I used the Ranorex API to see the what Checkbox class can do: http://www.ranorex.com/Documentation/Ra ... eckBox.htm

Note, that CheckState property is part of .NET, that's why the System.Windows.Forms namespace and not Ranorex namespace. And CheckState is not a boolean, that's why I had to use the comparing "==" operator.

Hope this helps. Have a good weekend,

Kind Regards,
Zoltan

Re: How to check the state of a checkbox

Posted: Fri Apr 25, 2014 4:30 pm
by houseofcutler
Hi Zoltan,

Thank you for your reply - was just what I needed. However it turns out what I was calling a check box was in fact an Input with a type of checkbox. Sorry about that.

I had to change your script around a little to cope with this. I was getting errors that
it was not possible to implicitly convert a boolean to a string (or visa versa cant remember which) So I had to use checked.equals(true)
public void Mouse_Click_chk_Students()
        {      
            //create the checkbox from the repository
            Ranorex.InputTag varInput = repo.GroupcallMessengerRoot.SendMessage.Recipients.Menu_Groups.chk_Students;
            
            //Show the checked state in report
            Report.Info(varInput.Checked.ToString());
            
            //if checkbox is checked, the click on it, else double click on it
            if (varInput.Checked.Equals(true))
			{  
    			varInput.Click();  
			}
			else
			{
    			varInput.Click();
    			Delay.Duration(3000, false);
    			varInput.Click();
			}  
         }
Anyway this is now working as required - thnks very much