Work with selection of multiple checkbox

Ranorex Studio, Spy, Recorder, and Driver.
varun
Posts: 110
Joined: Mon Jul 23, 2012 5:52 am

Work with selection of multiple checkbox

Post by varun » Tue Dec 04, 2012 1:51 pm

Hi Support,

I our app, on selection of radio option, 7 check-box appears. And user have to select any of these check-boxes.
For automation, I bind the variable after providing values in a cell of excel. For single value it works fine, but it's not working if more than one value is provided against check-boxes.
Can you please elaborate how can I specify and use values from Excel to work for multiple check-box selection ?

You can find attached files for your reference as,
1. MultipleCheckboxSelectionValues.png for format of values provided in excel.
2. MultipleCheckboxSelection.rxsnp as RX snapshot of fields.
3. MultipleCheckboxSelection.png as screenshot of fields.

Your response awaited! Let me know, if more clarification required.

Thanks,
Varun.
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Work with selection of multiple checkbox

Post by Support Team » Tue Dec 04, 2012 4:01 pm

Hello,

You would need to use String.IndexOf() to find a substring (e.g. 'Mon') within your Excel cell.
After that you could set the property 'checked' = true if a day (e.g. 'Mon') exists.
Please take a look at this example.

Regards,
Markus (T)

varun
Posts: 110
Joined: Mon Jul 23, 2012 5:52 am

Re: Work with selection of multiple checkbox

Post by varun » Wed Dec 05, 2012 5:51 am

Hi,

Thanks for your reply and example. But unfortunately it's not for my problem. It will drive RX to click only on one check box.
But I have to make click on multiple elements, i.e. multiple check-boxes. As you can see in snapshots, I have passed values in data source separated by comma and based on this, related options should be selected in application.
It will be nice if you go through RX snapshot.

Thanks & Regards,
Varun.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Work with selection of multiple checkbox

Post by Support Team » Wed Dec 05, 2012 12:55 pm

Hello,

You could use the code snippet below to use your checkbox:
Ranorex.InputTag checkbox;
Host.Local.TryFindSingle("/dom[@caption='AddOrUpdateSchedules']//table[#'lstDOW1']/tbody//label[@innertext='Mon']/../input", out checkbox);	
checkbox.Checked = "true";
Alternatively, you could access it as shown below:
// get a list of all checkboxes
IList<Ranorex.InputTag> checkboxList = Host.Local.Find<Ranorex.InputTag>("/dom[@caption='AddOrUpdateSchedules']//table[#'lstDOW1']//input",500);
foreach (InputTag checkbox in checkboxList)
{
	// add a condition by using the ID e.g.
	if checkbox.Id.Equals("lstDOW1_0")
		checkbox.Checked = "true";
}
Regards,
Markus (T)

varun
Posts: 110
Joined: Mon Jul 23, 2012 5:52 am

Re: Work with selection of multiple checkbox

Post by varun » Thu Dec 06, 2012 11:16 am

Hi Markus,

Thanks for the code snippet but still I couldn't make it. Your snippet, get all check-boxes form application and then select them.
But for my case, first we have to get values from excel, then split them from comma and at-last select those check-boxes for which values are provided in cell of excel. Please look into following snippet-

Code: Select all

if(VarDOW.Length > 0)                   //VarDow variable is bind with related cell of excel.
{
string[] val =  VarDOW.Split(new char[] {','});
foreach (string s in val)
{ 
if(s.Equals ("lstDOW1_0"))
{
Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'Schedules.LstDOW1Dollar5' at 10;10.", repo.Schedules.LstDOW1Dollar5Info);
repo.Schedules.LstDOW1Dollar5.Click("10;10");
}
				
if(s.Equals ( "lstDOW1_1"))
{
Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'Schedules.LstDOW1Dollar5' at 10;10.", repo.Schedules.LstDOW1Dollar5Info);
repo.Schedules.LstDOW1Dollar5.Click("10;10");
}
......
From this code, I am not able to drive RX to click on related check-boxes. To get the view of error log, PFA screenshot "ErrorLog_1206.png". Also excel format is changed now, you can have its idea from "CheckboxValues_1206.png".

Hope I have cleared very much with the practical scenario, kindly let me know if still need more clarification. Your response awaited.

Thanks,
Varun.
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Work with selection of multiple checkbox

Post by Support Team » Thu Dec 06, 2012 4:05 pm

Hello,

Please try out the following code:
//source represents the data in your excel file
string source = "Mon, Tue, Sat";
string[] days = source.Split(',');
InputTag checkbox;

foreach (string day in days)
{
	Host.Local.TryFindSingle("/dom[@caption='AddOrUpdateSchedules']//table[#'lstDOW1']/tbody//label[@innertext='"+day+"']/../input", out checkbox);
	checkbox.Checked = "true";
}
Regards,
Markus (T)

varun
Posts: 110
Joined: Mon Jul 23, 2012 5:52 am

Re: Work with selection of multiple checkbox

Post by varun » Fri Dec 07, 2012 7:15 am

Hi Markus,

Thank you for reply and code snippet. But afraid to say that this is not working, getting an error, kindly check in "ErrorLog.png" file.
I assume that value passed to variable "checkbox" is null will be the reason for that. Please review my code and error log and suggest accordingly.

Code: Select all

if(VarDOW.Length > 0)
{
string source = VarDOW;
string[] days = source.Split(',');
InputTag checkbox;
					
foreach (string day in days)
{
						Host.Local.TryFindSingle("/dom[@caption='AddOrUpdateSchedules']//table[#'lstDOW1']/tbody//label[@innertext='"+day+"']/../input", out checkbox);    //value pass to day is lstDOW1_1, seems to be correct.
checkbox.Checked = "true";   //Value pass to checkbox is null. "Jump to Item" refers here for the unexpected behavior.
						
}
}
Thanks in advance,
Varun.
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Work with selection of multiple checkbox

Post by Support Team » Fri Dec 07, 2012 1:09 pm

Hello,

I have assumed that you are using 'Mon,Tue' as values in your data source but it looks like that you are using 'lstDOW1_1,LstDOW1_5'. This would not work with my provided code example since it uses the 'InnerText' property of the 'Label' item. The value 'lstDOW1_1' is the 'Id' property of the checkbox.

Please take a look at the screenshot below:
Checkbox.JPG
In order to solve this issue please change your values in your Excel file as you had it before.

Regards,
Markus (T)
You do not have the required permissions to view the files attached to this post.

varun
Posts: 110
Joined: Mon Jul 23, 2012 5:52 am

Re: Work with selection of multiple checkbox

Post by varun » Fri Dec 07, 2012 1:22 pm

Hi Markus,

Thank you, You rocks. :)
Can you please elaborate that what this code snippet actually do and how it actually relates to value provided in excel ?

Code: Select all

Host.Local.TryFindSingle("/dom[@caption='AddOrUpdateSchedules']//table[#'lstDOW1']/tbody//label[@innertext='"+day+"']/../input", out checkbox);
It will just to enhance my knowledge on RX scripting. Thanks in advance.

Regards,
Varun.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Work with selection of multiple checkbox

Post by Support Team » Fri Dec 07, 2012 2:35 pm

Hello,

Thanks :-)

The method expression below is used to find single elements (e.g. a checkbox) within your application:

Code: Select all

Host.Local.TryFindSingle("<RanroeXPath>", out element)
You would need to define the RanoreXPath that is used as a path to the element. You could also define an output element by using 'out element'. The found object is stored in the variable 'element'.

RanoreXPath:

Code: Select all

/dom[@caption='AddOrUpdateSchedules']//table[#'lstDOW1']/tbody
defines in which dom object Ranorex should find an object. It searches for a table with the ID = 'lstDOW1'.
'//' is used to ignore all elements/containers in between.

The following expression is used to search for a label with a property 'InnerText' and a value day (variable from data source). Afterwards it uses its parent container (.. is used for that) and searches for an 'InputTag'.

Code: Select all

//label[@innertext='"+day+"']/../input
By using 'out checkbox' you get the Input element back from the method for further usage.
For further information please take a look at section RanoreXPath in our User Guide.

Regards,
Markus (T)

varun
Posts: 110
Joined: Mon Jul 23, 2012 5:52 am

Re: Work with selection of multiple checkbox

Post by varun » Mon Dec 10, 2012 7:05 am

Hi Markus,

Thank you for description.
I would like to know if the same snippet be used for list box or not?
In new scenario, I have to select multiple options from the list box. PFA "LayListBox_1210" for its snapshot and "LayListBoxValues_1210.png" for values provided in Excel.
Code Snippet I am trying for this scenario -

Code: Select all

string source = VarAvailableColumns;
String[] colmns = source.Split(',');
InputTag listoption;
        		
foreach(string colmn in colmns)
{
Host.Local.TryFindSingle("/dom[@caption='Maintain Layout']//table[#'listAvailableColumns']/tbody//label[@innertext='"+colmn+"']/../input", out listoption);  
listoption.PerformClick;
}
1. On debugging found the same issue I was facing with previous problem, "null" value in passed to variable listoption.
2. Another issue with this scenario is error for "listoption.PerformClick" action. Attachment "ListBoxError_1210" is for this.

Markus, It will be great if you please let me know where I am wrong and what should be done to resolve these problems ?

Thanks in advance,
Varun.
You do not have the required permissions to view the files attached to this post.

User avatar
Support Team
Site Admin
Site Admin
Posts: 12145
Joined: Fri Jul 07, 2006 4:30 pm
Location: Houston, Texas, USA
Contact:

Re: Work with selection of multiple checkbox

Post by Support Team » Mon Dec 10, 2012 4:42 pm

Hello,

You need to adjust the provided code snippet since it was written for InputTags.
Please try the following code:
string source = VarAvailableColumns;
String[] colmns = source.Split(',');
OptionTag listoption;
                  
foreach(string colmn in colmns)
{
	Host.Local.TryFindSingle("/dom[@caption='Maintain Layout']//select[#'listAvailableColumns']/option[@innertext='"+colmn+"']", out listoption); 
    listoption.PerformClick;
}
You should test your RxPath in Spy in order to find your element. If not you would need to change it.

Regards,
Markus (T)

varun
Posts: 110
Joined: Mon Jul 23, 2012 5:52 am

Re: Work with selection of multiple checkbox

Post by varun » Tue Dec 11, 2012 8:42 am

Hi Markus,

Thanks for your post and clearing my doubts.

Regards,
Varun.