Wednesday, May 18, 2011

When Strings don't match in WATIN

The last week while I was working with a co-worker, I ran into this strange problem with WATIN on attempting a string based constraint search on a web page.
The item in question was a Business Objects InfoView report on which the filtering capability was to be tested.
The filter was a normal drop-down list


There was nothing unusual about this element and it was rendered as an usual select list and it appeared like one of those usual select lists that could be identified by specifying a Constraint for the Title.


However, that wasn't the case. Since this was enclosed within a frame, we needed to identify the Frame object first. After having done that we tried a
We tried setting a title based constraint using the following code
SelectList uniqValueFilter = DrillBar.SelectList(t => t.Title.Contains("Drill filter on Asset Number"));
But this just would not work. An interesting thing that we found was, if we did a COPY from the Developer Toolbar window and pasted it into Visual Studio, the same line of code would work and the SelectList object would exist.
However, if we keyed it in, it would not. The copy - paste approach wasn't an option for us because we wanted to test for different reports and the SelectList would be identified using input from a XML file.

However, this provided us with a valuable clue that there was an inconsistency in the character format. We tried a few approaches that did not work, and then we decided to  inspect it further

string title = "Drill filter on Asset Number";
char[] titlefromDEVTOOLBAR = title.ToCharArray();
title = "Drill filter on Asset Number";
char[] titlewhenkeyedin = title.ToCharArray();

A good look at the character arrays inside a quick watch window gave us more details about the issue.

The "space" character when pasted from the Browser was a char 160 instead of a char 32 that was keyed in.

A simple workaround could be to replace the character 160 before doing a comparison.
SelectList uniqValueFilter = uniqValueFilter = DrillBar.SelectList(t => t.Title.Replace((char)160, ' ') == "Drill filter on Asset Number");


But an approach like this would have been a work around and not a proper solution. The character 160 was a nbsp that occurs in HTML. So, we decided to constraint the selection of the SelectList based on the presence of all the individual words that formed the string.

SelectList uniqValueFilter = DrillBar.SelectList(BuildTextConstraintforSpaces("Drill filter on Asset Number"));
private Constraint BuildTextConstraintforSpaces(string filterstring)
{
string[] tempArray = filterstring.Split(' ');
Constraint textby = Find.Any;
foreach (string temp in tempArray)
{
textby = textby.And(Find.ByText(new Regex(temp, RegexOptions.IgnoreCase)));
}
return textby;
}
This has worked as expected without causing any pain.

No comments: