Identifying style in a TR tag

Ranorex Spy, Recorder, and Studio.
AccidentReport
Posts: 76
Joined: Tue Dec 04, 2012 2:30 pm

Identifying style in a TR tag

Post by AccidentReport » Wed Mar 05, 2014 10:53 am

In my web application I have a table containing a number of rows of information. Most of these rows will be formatted normally but one will will shown in red text as it represents the currently active option in the list. You can see this in the cut down version of the table code below:

Code: Select all

<table class="Gridview">
	<tbody>
		<tr class="sortedHdr"><th scope="col">Id</th><th scope="col">Text</th></tr>
		<tr style="color: red;" class="odd"><td>1</td><td>LIVE</td></tr>
		<tr class="odd"><td>2</td><td>TEST</td></tr>
		<tr class="odd"><td>3</td><td>OFFLINE</td></tr>
	</tbody>
</table>
Now, what I need to do is create some code that can look at this and determine which option is currently selected - so in the example above you can see that LIVE is currently red. Previously when interrogating table I have been using code such as:

Code: Select all

TableTag myTable = myrepo.Website.Table;
IList<Ranorex.TrTag> myRows = myTable.FindDescendants<Ranorex.TrTag>();
This has always been my go to code and worked well. Unfortunately, the identifying piece of code in the table is contained within the TR tag and my go to code doesn't see it. I'm trying to work out if there's a way to see this style information contained within the TR tag using something similar but haven't been able to work out how yet.

Is there something obvious I'm missing or is there a better way to get the style information out whilst maintaining a reference to which table row and option I'm on?


At the end of the day if I can't use this and I need a new piece of code then so be it!

AccidentReport
Posts: 76
Joined: Tue Dec 04, 2012 2:30 pm

Re: Identifying style in a TR tag

Post by AccidentReport » Wed Mar 05, 2014 12:25 pm

OK. So I've found a way to do what I wanted. It may not be the neatest or best solution in the world but it does work so this is for anyone else who may have had a similar problem.

Code: Select all

//Define the table and tbody tags
TableTag myTable = myrepo.Website.Table;
TBodyTag myTBody = myTable.FindDescendant<Ranorex.TBodyTag>();

//extract the html from the tbody tag
string innerHTML = myTBody.GetInnerHtml();
        	
//create a match collection holding each instance of the tr tags
MatchCollection myTRTags = Regex.Matches(innerHTML, ".*<tr .*");
        	
//define a variable to use for the found instance
int foundIndex = -1;
        	
//loop through the match collection and attempt to match the style tag
for(int i = 0; i < myTRTags.Count; i++) {
	Match isRed = Regex.Match(myTRTags[i].ToString(), "style=\"color: red;\"");        		
	//when found set the foundIndex to match the current index value
	if(isRed.Success) {
		foundIndex = i;
		break;
	}
}
        	
if(switchBranchID.Equals(foundIndex.ToString())) {
	Report.Log(ReportLevel.Success, "Module", "Active branch found and matches expected.");
} else {
	Report.Log(ReportLevel.Failure, "Module", "No active branch marked in red was found");
}

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

Re: Identifying style in a TR tag

Post by Support Team » Thu Mar 06, 2014 11:51 am

Hi,

You could also directly search for the TR tag with the style attribute set to red as shown below:
var myTable  = repo.YourWebElement.YourTable;
        	
TrTag mySelectedTag = myTable.FindSingle(".//tr[@color='red']");

String str = mySelectedTag.GetStyle("color");
Report.Info("Color: "+str);
Regards,
Markus
.
Image

AccidentReport
Posts: 76
Joined: Tue Dec 04, 2012 2:30 pm

Re: Identifying style in a TR tag

Post by AccidentReport » Thu Mar 06, 2014 1:56 pm

Yes but what would I then use to identify which row it is?

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

Re: Identifying style in a TR tag

Post by Support Team » Fri Mar 07, 2014 12:55 pm

Hi,

What do you mean with "how to identify which row it is"?
Do you want to know the index?
If so, you can for instance use the following:
Report.Info("Row Index: "+mySelectedTag.Element.GetAttributeValueText("ChildIndex")); // starts with 0
Regards,
Markus
.
Image