This is a migrated thread and some comments may be shown as answers.

How to search for text anywhere on page?

7 Answers 164 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 08 Jul 2016, 03:18 PM

Hi

I'm trying to write a check to see if the word "error" is visible anywhere on the current page: how might I do this?

I'd hoped to be able to do something like...

Element e = Find.ByContent("p:error");
if (e.Equals(null))
{
    Log.WriteLine("Could not find error message");
}

But I get this error:

System.NullReferenceException: Object reference not set to an instance of an object.

Any help gratefully received.

Thanks

 

7 Answers, 1 is accepted

Sort by
0
Nikolay Petrov
Telerik team
answered on 13 Jul 2016, 01:41 PM
Hi Michael,

Here is a sample piece of code that you may use to search for a word:
IList<Element> e = ActiveBrowser.Find.AllByContent("p:error");
 
if (e.Count == 0)
{
    Log.WriteLine("Could not find 'error' message");
}
else
{
    foreach (Element elem in e)
    {
        // print the inner text of the element
        Log.WriteLine(elem.InnerText);
    }
}

This will find all elements that contains 'error' string. It will work for all elements that are not in iframe though. If you have frame elements in your application - first you have to find this frame and than search in it.

I hope this clarification helps.

Regards,
Nikolay Petrov
Telerik by Progress
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Michael
Top achievements
Rank 1
answered on 13 Jul 2016, 03:24 PM

Hi

Thanks very much for that. I've tried it but unfortunately it doesn't find the word "error" as I was expecting.

In the attached screenshot the word "error" appears a few times but when I run the code above on it, it goes down the "Log.WriteLine("Could not find 'error' message");" branch.

I've highlighted the DOM at the bottom to show that at least one of the "error"s is not in its own frame.

Any ideas of other things I could try please?

Thanks

Mike 

 

0
Nikolay Petrov
Telerik team
answered on 15 Jul 2016, 03:17 PM
Hi Michael,

I reproduce your DOM structure and I still get the searched value using this code.

If your application is a dynamic one - double check that the search step is applied after the element containing "error" message appears in the DOM.

Regards,
Nikolay Petrov
Telerik by Progress
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Michael
Top achievements
Rank 1
answered on 26 Jul 2016, 03:43 PM

Hi

Thanks very much for your advice. If I run your code on a simple page it finds the text correctly.

The application I'm testing is dynamic, so I'm guessing that it might be a timing problem. I've tried putting a long delay (5 seconds, which is long after the page has finished loading) after loading the page but before I run the search step: unfortunately it still isn't finding the search text.

Is there a way of making sure that the DOM is fully ready before I run the search step?

Thanks

Mike 

0
Nikolay Petrov
Telerik team
answered on 29 Jul 2016, 12:25 PM
Hi Michael,

It could be not a timing problem. If this word appears in a part of your application that is attached to the DOM due to an user action like a button click or entering a text and submit - you have to search for it after that action. You may use "Wait for Exist" step to be sure that you have the element in the DOM that contains the word.

Regards,
Nikolay Petrov
Telerik by Progress
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Michael
Top achievements
Rank 1
answered on 02 Aug 2016, 09:06 AM

Hi

Thanks for your help: I've got it working now.

What I needed to do was to add a "Wait for exist" for an element that is on every page of the app I'm testing (not necessarily the element that I was searching for). Once I'd added that, your search code worked fine.

Thanks again

Mike

 

0
Elena
Telerik team
answered on 05 Aug 2016, 07:46 AM
Hi Mike,

Let me interfere in this thread since my colleague Nikolay is out of office. I am glad to hear you managed to get your scenario working. Do not hesitate to contact us in case of any future queries that might appear. Thanks for your cooperation! 

Regards,
Elena Tsvetkova
Telerik by Progress
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
Michael
Top achievements
Rank 1
Answers by
Nikolay Petrov
Telerik team
Michael
Top achievements
Rank 1
Elena
Telerik team
Share this question
or