Check/Uncheck Checkbox based on Element Class

Ask general questions here.
keenon
Posts: 14
Joined: Fri May 22, 2015 9:48 pm

Check/Uncheck Checkbox based on Element Class

Post by keenon » Wed Jul 29, 2015 8:00 pm

Hello,

c# noobie here.

I have a checkbox that does not contain the checkbox attribute and it appears the only difference between checked and unchecked is the element's class.

Checked
<a title="Click to switch between your retirement and pre-retirement views" class="RetirementToggleButton RetirementToggleButtonSprite RetirementSwitch_On" id="RetirementToggleSwitch" href="/lbs/Clix/Clix/UpdateRetirementToggleSwitch"></a>

Unchecked
<a title="Click to switch between your retirement and pre-retirement views" class="RetirementToggleButton RetirementToggleButtonSprite RetirementSwitch_Off" id="RetirementToggleSwitch" href="/lbs/Clix/Clix/UpdateRetirementToggleSwitch"></a>

I would like to get the value of the class and then take action based upon what class is passed through. I am sure I would have to write and IF statement, but I have been struggling to nail it down. I'm thinking this is much easier that I am making it seem to be. Any Suggestions?

Thanks

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Check/Uncheck Checkbox based on Element Class

Post by krstcs » Wed Jul 29, 2015 8:23 pm

Your path, assuming the id is unique on the page, could be this (not sure if you were having issues with the XPath, but here it is anyway):

Code: Select all

//a[#'RetirementToggleSwitch']
Then, in code, to get the value of the class, do this:

Code: Select all

string retirementClass = repo.RetirementToggleSwitch.Element.GetAttributeValueText("class");
To get whether the class contains the on/off you can do this:

Code: Select all

bool isOn = repo.RetirementToggleSwitch.Element.GetAttributeValueText("class").Contains("RetirementSwitch_On");
Change "repo.RetirementToggleSwitch" to whatever your actual repo item's folder structure and names are. You can also check for off specifically by changing "RetirementSwitch_On" to "RetirementSwitch_Off" (make sure to change the variable name to fit...).


The Ranorex API is located here: http://www.ranorex.com/Documentation/Ranorex/

Hope this helps!
Shortcuts usually aren't...

keenon
Posts: 14
Joined: Fri May 22, 2015 9:48 pm

Re: Check/Uncheck Checkbox based on Element Class

Post by keenon » Wed Jul 29, 2015 9:10 pm

You my friend are a life saver! I got it working exactly how I want it with your help. I appreciate all of your help!

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Check/Uncheck Checkbox based on Element Class

Post by krstcs » Thu Jul 30, 2015 2:46 pm

You're welcome!
Shortcuts usually aren't...