Your e-Business Quality Partner eValid™ -- The Web Quality Suite
Browser-Based Client-Side Functional Testing and Validation Page Timing/Tuning Transaction Monitoring. WebSite Spidering & Analysis and Realistic Server Loading.
© Copyright 2000-2012 by Software Research, Inc.

eValid -- JavaScript Example: clickElement.js
eValid Home

Summary
This page describes the JavaScript clickElement function. See also the JavaScript Interface.

Illustration of Operation -- Links
This passage is constructed specifically to permit illustration of the use of CallScript clickElement(index, name) from within an eValid playback. What is important about the passage in the box is that there are two instances of "click here" as anchor tags. If your eValid recording was aiming to click the second one, but the page changes so that eValid's adaptive search always falls on the first one, this solution will overcome that limitation. For this actual page the two "click here" instances are at index 52 and at index 58.

HTML Sample Area
click here to go to www.google.com.
The quick brown fox jumped over the lazy dog's back.
This is a link that gets you nowhere.
click here to go to www.yahoo.com.

Sample eValid Script Passages
Sample calls in an eValid .evs script file would appear as shown below. To confirm operation you can use this page as the target of a script, edit in the indicated JavaScript call, and play back the resulting script.

JavaScript Source
Here is the JavaScript source (which if you check is actually present in this page) but which you will need to assure is included in the target page.

  <script>
  <!--
   // clickElement.js
   //
   // Search by HTML element's innerText on the current frame.
   // Enumerates through the document starting at the specified index.
   // Upon reaching the last element it enumerates from the top of
   // of the document up to the specified index.
   // Search starts from the beginning if the specified index
   // is out of bounds.
   //
   // If an HTML element cannot be found, clickElement()
   // executes eValid's NOTE script command.
   //
   function clickElement(index, name)
   {
    var numItems = document.all.length;
    var i = index;

    if (i < 0 || i >= numItems)
    {
     i = 0;
    }

    do
    {
     var oElement = document.all.item(i);
     var innerText = oElement.innerText;

     if (name == innerText)
     {
      oElement.click();
      return;
     }

     i++;

     if (i >= numItems)
     {
      i = 0;
     }
    }
    while (i != index);

    // Write message to eValid...
    external.eValidRunCommand("Note", "Cannot find element '" + name + "'");
   }
  // -->
  </script>