Verifying if a text exists on a page

Sometimes, to check if the targeted webpage has been completely opened (instead of receiving an instance of the 404 page not found error), it is enough to see if the page contains some specific text (usually, the text is unique for the given page).

In this recipe we will consider an example of such verification.

Getting ready

Launch the Internet Explorer and open the following site: http://smartbear.com/.

On the main page, locate any text, which will be unique namely for the given page (in our example, we will be using the Tools for your needs text).

How to do it...

To check if the targeted text is available on a page, it is necessary to perform the following actions:

  1. Write the isTextPresent function that executes the verification:
    function isTextPresent(element, text)
    {
      return aqString.Find(element.contentText, text) > -1;
    }
  2. Let's now write a simple script, using the isTextPresent function (replace the highlighted text with the one you have located in the beginning of this recipe):
    function testVerifyText()
    {
      var br = Sys.Browser("iexplore");
      var page = br.Page("http://smartbear.com/");
      var form = page.Form("aspnetForm");
      Log.Message(isTextPresent(form, "Tools for your needs"));
    }
  3. If you launch the testVerifyText function, the result in the log will be True.

How it works...

The contentText property contains the whole text of the element, including related child elements. This property is analogous to those of innerText and textContent; however, unlike those, it works equally in all the browsers.

In our example, we have used the contentText property for the whole of the page (that is, for the element that corresponds to the page), which means we have obtained all the available text on-site.

Similarly, we can obtain all the text of any other web-element (that is, Panel or TextNode).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.44.143