This is a migrated thread and some comments may be shown as answers.
Find.ByContent
6 Answers 78 Views
This is a migrated thread and some comments may be shown as answers.
SD
Top achievements
Rank 1
SD asked on 03 Oct 2011, 04:20 AM
Hi Guys, 

As relaying on elements' ID can be a risky business I've tried to use an alternative and tried 'Find.ByContent'

After running the following code
Console.WriteLine(Manager.ActiveBrowser.Find.ById<HtmlTable>("assessmentForm:permitsTable").InnerText);
I getting the attached result in Nunit. 
So I've figured out I could use which failed. 
Manager.ActiveBrowser.Find.ByContent<HtmlTable>("l:Permit", FindContentType.InnerText).IsVisible();
Variations of the string content (p: etc..) and FindContenctType failed as well...
Any idea ? 
ta. 



6 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 03 Oct 2011, 01:38 PM
Hello Sd,

I think the problem here is inappropriate usage of the IsVisible property . Try using this code instead:
HtmlTable table = Find.ByContent<HtmlTable>("l:Permit", FindContentType.InnerText);
Assert.IsTrue(table.IsVisible());

Also, please check this article on "Finding Page Elements" representing all available verification methods.   

Hope this helps!

Best wishes,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
SD
Top achievements
Rank 1
answered on 04 Oct 2011, 03:24 AM
Ta Plamen, 

The following works...
HtmlTable table = Find.ByContent<HtmlTable>("l:Permit Number", FindContentType.InnerText);
but obviously the aim is to try to read the data in the table such as the value '0004' by doing something like this which fails..
HtmlTable table = Find.ByContent<HtmlTable>("l:Permit Number", FindContentType.InnerText);
Console.WriteLine(table.Rows.ElementAt(0).Cells.ElementAt(1).InnerText);
I have tried you suggestion as well which failed as well..
HtmlTable table = Find.ByContent<HtmlTable>("l:Permit", FindContentType.InnerText);
Assert.IsTrue(table.IsVisible());
Pls refer to the attached doc. 

ta.

 
0
Plamen
Telerik team
answered on 04 Oct 2011, 02:27 PM
Hello Sd,

Unfortunately, I'm not sure the first code works as it should. If it works the second code should works too.
Here is what I think is happening: The framework locates the first element matching the given search criteria "Find.ByContent<HtmlTable>("l:Permit Number", FindContentType.InnerText", but there probably is another element that matches the search criteria and it is found before the target element. In this case if the control type of the found element does not match, the framework will return null. Please check this KB article explaining the situation. 

We can easily check if this is the case by using the following code:
HtmlTable table = Find.ByContent<HtmlTable>("l:Permit Number", FindContentType.InnerText);
Assert.IsNotNull(table);
If the result from this verification is null , we have the exact same case described in the KB article and you should consider the option to use this code instead:
HtmlTable table = Find.AllByAttributes<HtmlTable>("id=~permitsTable")[0];
Console.WriteLine(table.Rows.ElementAt(0).Cells.ElementAt(1).InnerText);

Hope this helps!

Best wishes,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
SD
Top achievements
Rank 1
answered on 05 Oct 2011, 01:15 AM
Ta for that. 
Indeed the verification code returns null. The thing is I don't want to use an attribute such as ID as IDs may change from time to time. 
Just to explain the approach I'm using,  for clicking a button I run
StringBuilder buffer = new StringBuilder();
           buffer.Append("//input[@value=\"")
                 .Append(buttonGUIName)
                 .Append("\"]");
           try
           {
               Manager.ActiveBrowser.Find.ByXPath<HtmlInputSubmit>(buffer.ToString()).Click();
               
So the button is actually being indetified by its GUI caption (i.e OK, Cancel etc). That way I'm taking IDs and alike out of the equation.  
I'm trying to do the same for that particular table. The fact that 'assessmentForm:permitsTable' ID is there is irrelevant. That may change in the next release. The thing that won't change is the table text as it seen on screen like 'Temporary Permits' or the the names of the columns 'Start Date' etc. 
I am aware of the ability to use partial ID string but in this case I need to relay on elements with the least chance of changing thru the dev cycle. 
0
Plamen
Telerik team
answered on 05 Oct 2011, 03:44 PM
Hi Sd,

Please try the code below, it should work correctly now. I was able to find the table in my sample app by using it. 
HtmlTable table = Find.AllByContent<HtmlTable>("p:Permit Number", FindContentType.InnerText)[0];
  
Assert.IsNotNull(table);

If this doesn't work, would it be possible to provide us access to your application? If  it is not possible, is there a public site that exhibits the same issue? We'll check it out and we'll come up with some sort of solution for the issue.

Regards,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Cody
Telerik team
answered on 05 Oct 2011, 04:39 PM
Hi Sd,

Another idea, instead of using XPath is to use our ByExpression like this:

Find.ByExpression<HtmlInputSubmit>("TagName=input", "value=" + buttonGUIName).Click();


Kind regards,
Cody
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Asked by
SD
Top achievements
Rank 1
Answers by
Plamen
Telerik team
SD
Top achievements
Rank 1
Cody
Telerik team
Share this question
or