How to search for text anywhere on page?

8 posts, 0 answers
  1. Michael
    Michael avatar
    4 posts
    Member since:
    Aug 2015

    Posted 08 Jul Link to this post

    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

     

  2. Nikolay Petrov
    Admin
    Nikolay Petrov avatar
    81 posts

    Posted 13 Jul Link to this post

    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!
  3. Michael
    Michael avatar
    4 posts
    Member since:
    Aug 2015

    Posted 13 Jul in reply to Nikolay Petrov Link to this post

    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 

     

  4. Nikolay Petrov
    Admin
    Nikolay Petrov avatar
    81 posts

    Posted 15 Jul Link to this post

    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!
  5. Michael
    Michael avatar
    4 posts
    Member since:
    Aug 2015

    Posted 26 Jul in reply to Nikolay Petrov Link to this post

    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 

  6. Nikolay Petrov
    Admin
    Nikolay Petrov avatar
    81 posts

    Posted 29 Jul Link to this post

    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!
  7. Michael
    Michael avatar
    4 posts
    Member since:
    Aug 2015

    Posted 02 Aug in reply to Nikolay Petrov Link to this post

    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

     

  8. Elena Tsvetkova
    Admin
    Elena Tsvetkova avatar
    251 posts

    Posted 05 Aug Link to this post

    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!
Back to Top