Trouble selecting a checkbox with usercode

Class library usage, coding and language questions.
AdamMackintosh
Posts: 19
Joined: Mon Jun 08, 2015 6:21 am

Trouble selecting a checkbox with usercode

Post by AdamMackintosh » Mon Jun 08, 2015 6:43 am

First, I have some decent intuition with coding, but I've never learned C# and I am in the process of learning how to use Ranorex. So if my code cannot be fixed just by looking at it and telling me what is wrong, I will need to know exactly why it isn't working. By that, I mean something a little more "spelled out" for someone that has never worked with C# before. I am pretty sure it is a simple syntax error, but I haven't figured out why.

Before I start posting my snapshots and whatnot, I am just wondering if someone can help me with my syntax just by viewing the snippet:
Host.Local.TryFindSingle("/dom[@domain='127.0.0.1']/body/table[2]//form[@id='pform']/table/?/?/tr[@id='IDSpecifyIPSpace']/?/?/div[@id='ipdiv']/?/?/iframe[@id='ipframe']//td[@innertext='*.domain.com']/.//input", out CheckBox); 
CheckBox.checked = "True";
I am getting error:
; expected (CS1002) - \\yvcdc03\UserHome\amackintosh\My Documents\Ranorex\RanorexStudio Projects\Volo\Volo\test1.UserCode.cs:45,32
Invalid expression term '=' (CS1525) - \\yvcdc03\UserHome\amackintosh\My Documents\Ranorex\RanorexStudio Projects\Volo\Volo\test1.UserCode.cs:45,30
{ expected (CS1514) - \\yvcdc03\UserHome\amackintosh\My Documents\Ranorex\RanorexStudio Projects\Volo\Volo\test1.UserCode.cs:45,30
Identifier expected; 'checked' is a keyword (CS1041) - \\yvcdc03\UserHome\amackintosh\My Documents\Ranorex\RanorexStudio Projects\Volo\Volo\test1.UserCode.cs:45,22

Line 45 is CheckBox.checked = "True";

The first line correctly identifies the checkbox. I see it when I highlight the element in Spy. I have also correctly used element.checkbox = true before in a previous trial and error test I was doing, but with something like repo.VoloMP.SelectDomains.CheckBox = "True";.

I suspect my issue starts with the first line "out CheckBox" and my syntax is simply incorrect for CheckBox.checked = "True";

In this case, I cannot put the element in here from the repo because I need that domain to be exposed for a ForEach loop to select a variable number of domains from a comma separated list within a cell from an XLS file.

Thanks for any help,
Adam

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

Re: Trouble selecting a checkbox with usercode

Post by krstcs » Mon Jun 08, 2015 3:48 pm

First, welcome to the forums!

Second, please include the declaration of the variables as well when posting code. It might be that the variable is not declared correctly.

Also, please indicate what you are trying to accomplish with the code in question. You don't say if you are trying to validate the checkbox or actually set it without using the mouse (if you are setting it you SHOULD use the mouse and click on it.


My suspicion though is that it has to do with your second line. The 'checked' property is a boolean, but you are passing in a string without converting it.

In addition, I try to stay away from output parameters. Try using the Host.Local.Find<input>(RXPath).

Try this:

Code: Select all

InputTag checkbox = Host.Local.Find<inputtag>("/dom[@domain='127.0.0.1']/body/table[2]//form[@id='pform']/table/?/?/tr[@id='IDSpecifyIPSpace']/?/?/div[@id='ipdiv']/?/?/iframe[@id='ipframe']//td[@innertext='*.domain.com']/.//input");
CheckBox.checked = true;  //lower case - no quotes...   NOTE: This SETS the checked state.
Shortcuts usually aren't...

AdamMackintosh
Posts: 19
Joined: Mon Jun 08, 2015 6:21 am

Re: Trouble selecting a checkbox with usercode

Post by AdamMackintosh » Mon Jun 08, 2015 5:51 pm

Thanks for the reply. Last night, I ended up scavenging enough material off Google that I was able to get it functional. I still don't know what the syntax error was, but there was something.

Here is my functional one:
Ranorex.InputTag selectCheckBoxVar = Host.Local.FindSingle<Ranorex.InputTag>("/dom[@domain='127.0.0.1']/body/table[2]//form[@id='pform']/table/?/?/tr[@id='IDSpecifyIPSpace']/?/?/div[@id='ipdiv']/?/?/iframe[@id='ipframe']//td[@innertext='*."+domain+"']/inputtag", 10000);  
selectCheckBoxVar.PerformClick();
TryFindSingle returns the checkbox that I am looking for, and the second line performs click on it. It looks like you, krstcs, were essentially saying this is what I should be doing.

For sake of completeness and to maybe help someone like myself in the future, here is my complete user code for this action:
public void SelectDomains(string varDomains)
        {
        	string[] separators = {","};
      		string value = varDomains;
      		string[] domains = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);
      		foreach (var domain in domains)
      		{
        		Report.Log(ReportLevel.Info, "Domains", "Selecting Domain: "+domain+"", null);
            	Ranorex.InputTag selectCheckBoxVar = Host.Local.FindSingle<Ranorex.InputTag>("/dom[@domain='127.0.0.1']/body/table[2]//form[@id='pform']/table/?/?/tr[@id='IDSpecifyIPSpace']/?/?/div[@id='ipdiv']/?/?/iframe[@id='ipframe']//td[@innertext='*."+domain+"']/inputtag", 10000);  
            	selectCheckBoxVar.PerformClick();
      		}
        }
What it does is grab a comma separated list of email domains (as many as you want) and set them into an array. The ForEach loop selects the checkbox for each domain in the list. This list of domains I refer to is a table on a webpage that outputs a row with a checkbox and the domain name in the adjacent cell, and depending on the test I am running, I desire to select any number of specific domains. This code will be extremely beneficial for anyone looking to stick to 1 row in an XLS file during a data-driven recording and perform multiple checkbox/radiobox selections based on comma separated objects from a variable (or really any action with those comma separated values).

I am loving Ranorex so far though. The automation I can achieve as an amateur coder is simply amazing. I am thoroughly impressed and will never hesitate to recommend it to anyone looking to achieve more reliable test coverage for really any software.

Thanks,
Adam

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

Re: Trouble selecting a checkbox with usercode

Post by krstcs » Mon Jun 08, 2015 6:40 pm

Yeah, you can do it the other way with the TryFindSingle(XPath, out Element), but I PREFER to do it the simpler way. Output variables are handy when you need them, but if you don't I see them as added complexity for little benefit. But, again, that's me. :D


Ranorex is a great tool, and the fact that it is essential creating .NET executables on top of the .NET runtime is a huge bonus. You can do anything with Ranorex that you can do in .NET.


Glad you got it working! Let us know if you have any more questions!
Shortcuts usually aren't...

AdamMackintosh
Posts: 19
Joined: Mon Jun 08, 2015 6:21 am

Re: Trouble selecting a checkbox with usercode

Post by AdamMackintosh » Wed Jun 10, 2015 7:07 am

I'm having a new issue now that I cant seem to solve with intense Google scavenging.

I have a hyperlink that basically generates a table with dynamic number of rows and static number of cells within each row.

I'm trying to iterate through the table and for each row in which cell 1 contains text "abc123", I would like to click the delete button in cell 2.

Can anyone help me adapt my code to achieve this?
var text2find = "nhd6mid.BG6uvr7i.%%$_sdomain%%";
var rows = Host.Local.Find<TrTag>("/dom[@domain='127.0.0.1']//table[#'trackTable']/tbody/tr");
int rowNum = 1;  
  
foreach (var row in rows)  
{  
    Report.Log(ReportLevel.Info, "Validation", "===========================", repo.VoloMP.TrackTableInfo);
    Report.Log(ReportLevel.Info, "Validation", "Row: " + rowNum, repo.VoloMP.TrackTableInfo);
    Report.Log(ReportLevel.Info, "Test", "Row: "+row.TagValue, repo.VoloMP.TrackTableInfo); 
    var cells = row.FindDescendants<TdTag>();  
                  
    foreach (var cell in cells)  
    {  
        Report.Log(ReportLevel.Info, "Validation", "Cell: " + cell.InnerText, repo.VoloMP.TrackTableInfo);
    }  
    rowNum++;                 
}
This code seems to loop through and I can tell it is observing the correct number of rows because rowNum increments all 11 of them. I'm in a debug mode right now trying to simply output the plain text contents of each cell but I cant see to do that. My report log seems to be dumping null for both of those foreach loops.

If anyone can help me turn my code into something that clicks the button a[@id='deleteMe'] if the row contains the text in variable /input[@tagvalue=text2find] (that variable is set in line 1 in my above sample)

The path to the input field text in row 1 is:
/dom[@domain='127.0.0.1']/body/table[2]//form[@id='pform']/table/?/?/tr[@id='IDTrackedLinks']/?/?/table[@id='trackTable']//td[@innertext='Link #: ']/input[@tagvalue='nhd6mid.BG6uvr7i.%%$_sdomain%%']
The path to the button in row 1 is:
/dom[@domain='127.0.0.1']/body/table[2]//form[@id='pform']/table/?/?/tr[@id='IDTrackedLinks']/?/?/table[@id='trackTable']/tbody/tr[1]/?/?/a[@id='deleteMe']
I hope I've given enough info for someone to whip up a sample for me. I'm having all kinds of trouble understanding the C# syntax to return the stuff I want. I can link some snapshot or give more info if needed, but I have a feeling I've given enough to work with. I know my code is close; it's just missing the part inside each foreach loop (i think). You can probably shorten my code to like 1-2 lines inside the first foreach loop too I suspect.

Thanks!

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Trouble selecting a checkbox with usercode

Post by odklizec » Wed Jun 10, 2015 8:45 am

Hi,

Could you please post a Ranorex snapshot of the table in question? It's hard to suggest something reliable without seeing the snapshot.

Basically speaking, I think the reason why you getting "null" is most probably because the pointed TR and TD tags have no InnerText or TagValues. Instead, as I understand it, the TD elements contains "input" or "button" elements? So you need to access these elements instead of just TR/TD.

I'm using below code to extract values from list of unknown elements (input tags and select tags)...

Code: Select all

...
			//create IList of all elements, available to given repo element
			IList<Ranorex.Unknown> allElementsList = repoElement.CreateAdapters<Ranorex.Unknown>();
			//go through list of all elements and save obtained values to CSV connector file
			foreach (Ranorex.Unknown unknownTagElement in allElementsList)
			{
				GetAttributeValuesFromHTMLElements(unknownTagElement, out tagName, out tagValue, out tagType);
			...
		private static Adapter GetAttributeValuesFromHTMLElements(Ranorex.Unknown unknownTagElement, out string tagName, out string tagValue, out string tagType)  
		{ 
			//get preferred capability of selected element
			tagType = unknownTagElement.Element.PreferredCapability.DisplayName.ToString();
			if (tagType == "InputTag")  
			{  
				Ranorex.InputTag tagElement = unknownTagElement.Element;
				//get TagName
				tagName = tagElement.Name.ToString();
				if (tagElement.Type=="radio" | tagElement.Type=="checkbox")
				{
					//get attribute value
					tagValue = tagElement.Element.GetAttributeValueText("Checked");
				}
				else
				{
					//get attribute value
					tagValue = tagElement.Element.GetAttributeValueText("TagValue");
				}
				return tagElement;
			} 
			else if (tagType == "SelectTag")  
			{  
				//create adapter with correct capability
				Ranorex.SelectTag tagElement = unknownTagElement.Element;
				//get TagName
				tagName = tagElement.Name.ToString();
				//get TagValue
				tagValue = tagElement.Element.GetAttributeValueText("TagValue");
				return tagElement;
			}
		}
Last edited by odklizec on Thu Nov 12, 2015 8:12 am, edited 1 time in total.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

AdamMackintosh
Posts: 19
Joined: Mon Jun 08, 2015 6:21 am

Re: Trouble selecting a checkbox with usercode

Post by AdamMackintosh » Wed Jun 10, 2015 8:32 pm

At face value, I find that above code very confusing and it seems way more complicated than what I need which is hopefully just a simple iteration through this table and clicking the hyperlink in the row if the row contains a regex match to the content of a string variable.

I dont even know what you can see in the snapshot, but what I need is to examine the input field that contains my string and click the respective delete button if text in the input field regex-matches my string.

foreach row in rows {
if InputTag.TagValue contains $varTrackedLink { PerformClick on respective delete button }
}

That's kind of the plain english version of what I need, lol, thanks.

I cant attach a snapshot because Ranorex saves a 9MB snapshot for the table, and the site limits to 1MB. I dont know how to even begin fixing that. I do know that often, it takes FOREVER for Ranorex to update Browser & Results and Path Editor if I change something. Often, it finds about 5500 elements, so thats probably why it's 9 MB.
Last edited by AdamMackintosh on Wed Jun 10, 2015 8:49 pm, edited 2 times in total.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Trouble selecting a checkbox with usercode

Post by odklizec » Wed Jun 10, 2015 8:42 pm

Hi,

Could it be that the table in question is too big? Eventually, the slowdown could be caused by saving entire ancestor tree. Go to General Settings and uncheck 'Let snapshot contain complete ancestor subtree' option...
http://www.ranorex.com/support/user-gui ... html#c5890
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

AdamMackintosh
Posts: 19
Joined: Mon Jun 08, 2015 6:21 am

Re: Trouble selecting a checkbox with usercode

Post by AdamMackintosh » Wed Jun 10, 2015 8:52 pm

Ok, that solved the snapshot size issue. Thanks.

Am I safe to leave that option unchecked, or should I keep it? This isn't my normal project and I don't have that issue with my other one as there are no elements with such depth.

See attached.
You do not have the required permissions to view the files attached to this post.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Trouble selecting a checkbox with usercode

Post by odklizec » Thu Jun 11, 2015 8:34 am

Hi,

OK, here are some initial observations.

At first, this path cannot find the input element:

Code: Select all

/dom[@domain='127.0.0.1']/body/table[2]//form[@id='pform']/table/?/?/tr[@id='IDTrackedLinks']/?/?/table[@id='trackTable']//td[@innertext='Link #: ']/input[@tagvalue='nhd6mid.BG6uvr7i.%%$_sdomain%%']  
Simply, the tag value of Input in question contains much longer string:

Code: Select all

http://nhd6mid.BG6uvr7i.%%$_sdomain%%/t78yhjoituryFaghaehe4haGIufydtyfugihjo?msgid=%%$msgid%%&email=%%rotx%$email%%
But your xpath expression searches for exact string match...

Code: Select all

/input[@tagvalue='nhd6mid.BG6uvr7i.%%$_sdomain%%']
So to be able to find input based of the substring, you will have to use regex expression like this:

Code: Select all

/input[@tagvalue~'nhd6mid\.BG6uvr7i\.%%\$_sdomain%%'] 
Whole path:

Code: Select all

/dom[@domain='208.86.26.40']//form[@id='pform']/table/?/?/tr[@id='IDTrackedLinks']/?/?/table[@id='trackTable']//td[@innertext='Link #: ']/input[@tagvalue~'nhd6mid\.BG6uvr7i\.%%\$_sdomain%%']
And here comes second problem. The substring you want to use is not unique for this table! The above path finds 3 inputs containing this substring:
FindInputs.png
And finally, as I've expected, the TD tags does not contain any TagValue/InnerText.

I can't create the code you want right now, but I think it should be relative simple to achieve what you want with cleverly constructed xpath of DeleteButton. For example, this xpath finds you all relevant "deleteMe" buttons...

Code: Select all

/dom[@domain='208.86.26.40']//table[#'trackTable']//input[@tagvalue~'nhd6mid\.BG6uvr7i\.%%\$_sdomain%%']/../..//a[#deleteMe]
Now what you need is to use Find method (or similar) to find all instances of the above path (either using repository element or directly the above xpath), and then go through all found elements stored in ilist (using foreach) and invoke click for each found element. It should be quite easy? ;)
You do not have the required permissions to view the files attached to this post.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Trouble selecting a checkbox with usercode

Post by odklizec » Thu Jun 11, 2015 8:56 am

Actually, the code was even simpler than I thought of ;)
IList<Ranorex.ATag> foundTags = refTable.Find<Ranorex.ATag>(@".//input[@tagvalue~'nhd6mid\.BG6uvr7i\.%%\$_sdomain%%']/../..//a[#deleteMe]");
	foreach (ATag curATag in foundTags) 
	{
		curATag.PerformClick();
	}
Where "refTable" refers to repo element pointing to the table in question.
BTW, don't forget to use "@" character in front of the xpath, as I did in the above sample, otherwise Ranorex will throw a compile error ("unexpected escape sequence $").
Hope this helps?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

AdamMackintosh
Posts: 19
Joined: Mon Jun 08, 2015 6:21 am

Re: Trouble selecting a checkbox with usercode

Post by AdamMackintosh » Thu Jun 11, 2015 5:58 pm

Thanks for the reply. Your first reply was on a tangent but your second post looked like it was 100% accurate.

I replaced refTable with the repo item, repo.VoloMP.TrackTable, but when I run the code, it says:
Item 'VoloRepository.VoloMP.TrackTable' is no Table.
The element does not support the required capability 'table'.
I changed my path to the repo item a few times, but here is my favourite RanoreXPath to it:
.//table[@id='trackTable']
Any idea why it thinks the table isnt a table?

if i put /tbody on the end of that path, the code runs but it doesnt do anything and doesnt appear to be entering the foreach loop.

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

Re: Trouble selecting a checkbox with usercode

Post by krstcs » Thu Jun 11, 2015 6:13 pm

You might try changing your declaration to a tabletag object instead of table. It should work either way, but technically a table is a native code table object and a tabletag is an HTML element object, so you might try it. I've found that there are times when Ranorex can still get confused over which one you mean, even though it may be clear to use looking at it.

Also, tabletag objects have different abilities than table objects so that could be why your test is not liking the declaration. It might be that you are trying to do something with the tabletag that is only allowed on table.
Shortcuts usually aren't...

AdamMackintosh
Posts: 19
Joined: Mon Jun 08, 2015 6:21 am

Re: Trouble selecting a checkbox with usercode

Post by AdamMackintosh » Thu Jun 11, 2015 6:34 pm

Thanks, I changed it from /table to /tabletag and it got rid of the error again, but its not entering into the foreach loop.

I'm not trying to do anything you didnt paste me, except display log info:
public void Validate_TrackTable()
        {
 			IList<Ranorex.ATag> foundTags = repo.VoloMP.TrackTable.Find<Ranorex.ATag>(@".//input[@tagvalue~'nhd6mid.BG6uvr7i']/../..//a[#deleteMe]");  
 			Report.Log(ReportLevel.Info, "Test", "Table grabbed.", repo.VoloMP.TrackTableInfo);
    		foreach (ATag curATag in foundTags)   
    		{  
		        curATag.PerformClick();  
		        Report.Log(ReportLevel.Info, "Test", "We should see 3 of these: "+curATag.InnerText, repo.VoloMP.TrackTableInfo);
	    	}  
        }
Path to table:
.//tabletag[@id='trackTable']
Take a look at my Report.Log lines. When I run that code, my log shows "Table grabbed." and it clearly doesnt enter the foreach loop because I dont get any "We should see 3 of these" stuff.

As a logical individual, I suspected it could be the part (@".//input[@tagvalue~'nhd6mid.BG6uvr7i']/../..//a[#deleteMe]")

I changed /input[@tagvalue~'nhd6mid.BG6uvr7i'] to /input[@tagvalue~'http'] which should match all 12 rows, and with this, I get one log output of "We should see 3 of these", so why is it only returning one instead of zero with a less specific regex match?

Lemme run that original fragment into spy. It finds 1 match for:
/dom[@domain='127.0.0.1']//tabletag[@id='trackTable']/.//input[@tagvalue~'nhd6mid.BG6uvr7i']/../..//a[#deleteMe]
There should be 3 matches.

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 7470
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Trouble selecting a checkbox with usercode

Post by odklizec » Thu Jun 11, 2015 6:58 pm

Hi,

I believe you forgot to escape "." in changed xpath. This code shoud work...
IList<Ranorex.ATag> foundTags = repo.VoloMP.TrackTable.Find<Ranorex.ATag>(@".//input[@tagvalue~'nhd6mid\.BG6uvr7i']/../..//a[#deleteMe]");
Last edited by odklizec on Thu Jul 30, 2015 11:22 am, edited 2 times in total.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration