Setting checkboxes to specific values

Class library usage, coding and language questions.
AndyW
Posts: 2
Joined: Tue Feb 07, 2012 4:52 pm

Setting checkboxes to specific values

Post by AndyW » Tue Feb 07, 2012 5:04 pm

Afternoon,

I've recently started our trial of Ranorex and after doing the first parts of the tutorial am bumping heads with how to deal with a pair checkboxes.
Short version is I have an app with checkboxes that default to the state they were last set to - so if the app was last run with them unchecked, they'll be unchecked on first load next time etc. I want my test case to run the app with both checked, so because of the defaulting recording a simple click isn't enough. Problem is I don't know how to conditionally change the value of my checkboxes to ensure they're always checked.

Can anyone help or point me in the right direction in the documentation?

Thanks.

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Setting checkboxes to specific values

Post by Ciege » Tue Feb 07, 2012 6:04 pm

Are you just doing record/playback or are you adding your own custom code?
I don't use record/playback so I cannot comment on that path, but with your own custom code you can write a method that checks checkboxes. Give the method two parameters, one, the checkbox you want to interact with and two, the state you want to set the checkbox.
Have the method check the current state then act accordingly. If you want it checked and it isn't check it, etc...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

AndyW
Posts: 2
Joined: Tue Feb 07, 2012 4:52 pm

Re: Setting checkboxes to specific values

Post by AndyW » Tue Feb 07, 2012 7:12 pm

I was trying the record/playback route based on the tutorial - do you have a link handy for doing your own codem in ranorex, or a snippet for how to deal with checkboxes?

I did find some code examples in the old (v2.x) user guide but that is prefaced with a warning to use the 3.x documentation instead - however I've yet to find the equivalent example in the latest user guide.

Apologies in advance if it's somewhere really obvious!

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Setting checkboxes to specific values

Post by Ciege » Tue Feb 07, 2012 7:30 pm

Here is my checkboxclick method. You can use it in whole or modified to help you along...

Code: Select all

public static int CheckBoxClick(Boolean SelectCheckBox, Ranorex.Form RanorexFormName, String CheckBoxName)
{
    int intResult = CheckBoxClick(SelectCheckBox, RanorexFormName, CheckBoxName, false);
    return intResult;
}
public static int CheckBoxClick(Boolean SelectCheckBox, Ranorex.Form RanorexFormName, String CheckBoxName, bool boolHideNotFoundError)
{
    /************************************************************************
     * Function         : CheckBoxClick(Boolean SelectCheckBox, Ranorex.Form RanorexFormName, String CheckBoxName)
     *
     * Description      : This method will search for and (if found)  
     *                  :  select or de-select a CheckBox on the form.
     *                         
     * Parameters       : SelectCheckBox    - TRUE = Select the CheckBox
     *                  :                   - FALSE = Deselect the CheckBox
     *                  : RanorexFormName   - The form object the the CheckBox lives on
     *                  : CheckBoxName      - Name of the CheckBox to click
     *                  : boolHideNotFoundError - FALSE (default) - reports that the item was not found - returns -1
     *                  :                       - TRUE - Hides the error that the item was not found - Still returns -1
     *                  :                       - This option basically transforms this method into a check if exists method
     *                  
     * Return Type      : Integer
     * 
     * Return Value     : 0 for success, -1 for failure
     * 
     * Revision History
     * Date       : Author                    : Reason/Change
     * ---------- : ------------------------- : ------------------------------
     * 03/05/2009 : Chris Gendreau            : Initial Creation 
     * 12/10/2009 : Chris Gendreau            : Added HideNotFoundError option
    ************************************************************************/

    Ranorex.CheckBox HDcheckbox;
    int error = 0;

    //Search for the CheckBox on the Form
    try
    {
        RanorexFormName.EnsureVisible();
        RanorexFormName.Activate();
        Thread.Sleep(500);
        HDcheckbox = RanorexFormName.FindSingle(".//checkbox[@controlname='" + CheckBoxName + "' or @text='" + CheckBoxName + "']", 30000);
    }

    catch (RanorexException e)
    {
        if (boolHideNotFoundError != true)
        {
            Report.Error("Unable to find CheckBox: " + CheckBoxName);
            Report.Error(e.ToString());
            Report.Screenshot();
        }
        error = -1;
        return error;
    }

    //Select or DeSelect a checkbox
    try
    {
        if (SelectCheckBox == true)
        {
            Report.Debug("Selecting checkbox: " + CheckBoxName);
            if (HDcheckbox.Checked != true)
            {
                Report.Debug("  Selected checkbox: " + CheckBoxName);
                HDcheckbox.Focus();
                Thread.Sleep(500);
                HDcheckbox.Click(Location.Center);
                if (HDcheckbox.Checked != true)
                {
                    HDcheckbox.Focus();
                    Thread.Sleep(500);
                    HDcheckbox.Click(Location.CenterLeft);
                }
            }
            else
            {
                Report.Debug("  " + CheckBoxName + " was already Selected");
            }
        }
        else
        {
            Report.Debug("DE-Selecting checkbox: " + CheckBoxName);
            if (HDcheckbox.Checked == true)
            {
                Report.Debug("  DE-Selected checkbox: " + CheckBoxName);
                HDcheckbox.Focus();
                Thread.Sleep(500);
                HDcheckbox.Click(Location.Center);
                if (HDcheckbox.Checked == true)
                {
                    HDcheckbox.Focus();
                    Thread.Sleep(500);
                    HDcheckbox.Click(Location.CenterLeft);
                }
            }
            else
            {
                Report.Debug("  " + CheckBoxName + " was already DE-Selected");
            }
        }
    }

    catch (RanorexException e)
    {
        Report.Error(e.ToString());
        Report.Screenshot();
        error = -1;
    }
    return error;
} //End CheckBoxClick
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Setting checkboxes to specific values

Post by Support Team » Tue Feb 07, 2012 8:52 pm

Depending on the technology of the element, you can also try to just set the "CheckState" or "Checked" attribute. You can do that easily using a "Set Value" action in the recorder (see this section in the Ranorex User Guide).
This approach might not work for some elements/technologies, then you have to rely on clicking the checkbox like Ciege proposed.

Regards,
Alex
Ranorex Team
.
Image

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: Setting checkboxes to specific values

Post by omayer » Tue Feb 07, 2012 9:28 pm

Hi Ciege , how to set the radiobttn value
thank you,
beginner
Tipu

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Setting checkboxes to specific values

Post by Ciege » Tue Feb 07, 2012 9:53 pm

omayer wrote:Hi Ciege , how to set the radiobttn value
thank you,
beginner
Basically the same way, except you use a Ranorex.RadioButton instead of a Ranorex.CheckBox.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: Setting checkboxes to specific values

Post by omayer » Wed Feb 08, 2012 3:34 am

thank you Ciege, how do you maintain these functions, do you keep it under one class or each function has independant class
Tipu

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: Setting checkboxes to specific values

Post by omayer » Wed Feb 08, 2012 3:46 am

one more - how do you get the text field from the page and enterring the value same for dropdowlist, list items, combo box,
Tipu

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Setting checkboxes to specific values

Post by Ciege » Wed Feb 08, 2012 4:01 pm

omayer wrote:thank you Ciege, how do you maintain these functions, do you keep it under one class or each function has independant class
I have a DLL that is my Ranorex Framework that contains all my re-usable methods. I then reference this DLL from each test scripts. One place to maintain code but re-usable from any test script I want.
omayer wrote:one more - how do you get the text field from the page and enterring the value same for dropdowlist, list items, combo box,
All very similar, you need to use the proper Ranorex type then query its proper attribute (such as the .text attribute of a Ranorex.Text).
For a combobox, you need to find the proper Ranorex.ComboBox then get its list of Ranorex.ListItem(s). Then when you open the combobox, click the listitem you desire.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: Setting checkboxes to specific values

Post by omayer » Wed Feb 08, 2012 4:09 pm

Thank you so much Ciege, this is very helpful.
Tipu

jos51e
Posts: 2
Joined: Fri Feb 10, 2012 1:20 pm

Re: Setting checkboxes to specific values

Post by jos51e » Fri Feb 10, 2012 1:24 pm

Thanks for the check box code. I am trying to implement the code which builds successfully when I enter it into the user code.

I’m guessing I could call the method from the cs file like so:
CheckBoxClick(true, repo.FormKill_Trinity, repo.FormKill_Trinity.CheckBox1);

But the cs file is overwritten when you run the test so where can you call the method with the correct attribules. Is there a way of entering it into the GUI test steps? I seem to be able to call a method this way but there is no place for the arguments.

thanks for any assistance

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Setting checkboxes to specific values

Post by Support Team » Fri Feb 10, 2012 3:42 pm

The Recording "*.cs" file is automatically generated, so all you changes will be reverted. All your user code should go into the "*.UserCode.cs" file. See following topic in the Ranorex User Guide:
http://www.ranorex.com/support/user-gui ... tions.html

You could create a user code action and in that method then call your CheckBoxClick method.

Alternatively, it might be sufficient (and much easier) to just set the Checked attribute of the Ranorex CheckBox element:
repo.FormKill_Trinity.CheckBox1.Checked = true;
If that works from code, you can also do that from the recorder UI directly using a "Set Value" action.

Regards,
Alex
Ranorex Team
.
Image

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: Setting checkboxes to specific values

Post by Ciege » Fri Feb 10, 2012 4:00 pm

Support Team wrote: Alternatively, it might be sufficient (and much easier) to just set the Checked attribute of the Ranorex CheckBox element:
repo.FormKill_Trinity.CheckBox1.Checked = true;
While I mostly agree with that, there are some potential pitfalls just setting the state of the element. One being that if there happens to be some sort of fire on click associated with the element, then just setting the attribute will not trigger that.
Further, I (personally) like having all the extra and standardized error checking and logging with using a custom method such as I have written. It might not be for everyone, but when I want to know what happened and (if an error happens) where and why an error occurs, I can track it down very easily with my method.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: Setting checkboxes to specific values

Post by omayer » Fri Feb 10, 2012 5:41 pm

Hi Ciege,
if page contains 10 different checkboxes and all 10 checkboxes has diff control name, so when i call the function clickcheck w/passing controlname as parameter, how does it work ...shouldn't i have to create 10 different functions to get the object name then passing to the caller, any input please.
Beginner
Tipu