Forum Discussion

william_roe's avatar
william_roe
Super Contributor
9 years ago
Solved

Get All Input Elements on Screen via FindChildByXPath

I've used FindChildByXPath to find first TD with click event (see below) but how do I get all controls on a page of a certain types (i.e. TextBoxes, Images, Tables, etc)?

 

  var page = Sys.Browser().Page("*");

// finds first td with click event var obj = page.FindChildByXPath("//table[@id='grdCribPrivileges_DXMainTable']//td[text()='" + Privileges[i] + "']/parent::node()/td[@onclick]", true);

I need to write the test such that I test all input controls which are disabled and don't want to explicitly map each field and rely on devs to tell me when a new field is added.

 

Can anyone help help with FindChildByXPath statement below to find all Textboxes, Images, Tables, etc?

 

function ValidateAllControlsDisabled(){
    // retrieves reference to browser
    var page = Sys.Browser().Page("*");

    // retrieves all controls on page which should be disalbed (textbox, tables, images, etc)
    var allControls = page.FindChildByXPath("//Textbox", true); // NEED HELP HERE

    for (var i = 0; i < allControls.length; i++) {
        // return false if any control not disabled    
    }
}

 

  • HKosova's avatar
    HKosova
    9 years ago

    Hi William,

     

    Sorry for the late reply. If by "disabled" you mean any of these:

     

    <input type="hidden" .../>
    <input type="text" disabled .../>
    <input type="text" readonly .../>
    

    then the query for enabled inputs shoud be something like:

     

    var arr = page.EvaluateXPath("//input[@type!='hidden' and not (@readonly or @disabled)]", true);

     

4 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    The XPath for multiple element types would be like this:

     

    //img|//table|//input

    Note that XPath always uses HTML tag names (a, input, div) and not TestComplete object types (Link, Textbox). Also, you need to use EvaluateXPath instead of FindChildByXPath to return multiple elements:

     

    var arr = page.EvaluateXPath("//img|//table|//input", true);
    if (arr != null)
    {
    // Convert the result to the JScript array format
    var allControls = (new VBArray(arr)).toArray();

    ...
    }
    • william_roe's avatar
      william_roe
      Super Contributor

      Thank you very much. Obvsiously I need to learn XPath. One more question:

       

      How do you specify a second predicate for the input where the input is also enabled?

       

          var page = Sys.Browser().Page("*").Panel("primaryWrapper").Section("primaryContent").Frame("*").Section("actionWrapper").Section("actionContent");
      
          var arr = page.EvaluateXPath("//input[@type!='hidden']", true);

       I've earnestly tried to find the answer online to no avail.

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        Hi William,

         

        Sorry for the late reply. If by "disabled" you mean any of these:

         

        <input type="hidden" .../>
        <input type="text" disabled .../>
        <input type="text" readonly .../>
        

        then the query for enabled inputs shoud be something like:

         

        var arr = page.EvaluateXPath("//input[@type!='hidden' and not (@readonly or @disabled)]", true);