Forum Discussion

torus's avatar
torus
Contributor
2 years ago

Web testing - Switch between using FindElement + Xpath to grab objects and using NameMap objects

Is there a way to use the 'FindElement' method for a NameMapping object? I thought (was hoping) the below code would work, but it does not. I read the 'FindElement' documentation and it says the FindElement is a method all Web test objects have.

https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/web-objects/findelement-method-web-object.html

The things in the NameMapping tree are web test object (I think). I don't know where I am going wrong.

I see the tableCell object has a 'firstChild' attribute. However, I don't want to use this. I only know how to identify the checkbox via the xpath ... I would like to use the xpath to find this sub-element.

 

 

function testFindElementMethod()
{
    // Table_Row1_Col_HasAccess xpath-selector = //table[@id='s-table']//tbody//tr[not(contains(@class,'filter-row'))][1]/td[9]
    var tableCell = Aliases.WebBrowser.OQM_Users_Page.Table_Row1_Col_HasAccess;  
    // If add 'tableCell' to my Watch list, i can see that this object did store it's findElement selector in the name attribute:
    // tableCell's Name attribute: "FindElement(\"xpath=//table[@id='s-table']//tbody//tr[not(contains(@class,'filter-row'))][1]/td[3]\")"
    // However, the below line of code does not find the child (grandchild) checkbox element like I think it would. 
    var child3 = tableCell.FindElement("//input[contain(@type,'checkbox')]");
}

 

 

  • You can use FindElement method on a mapped object. Not sure if this is a good example,

     

    function TestX()
    {
        // URL: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_html_nested_table
        
        // Aliases.frameIframeresult = Sys.Browser("chrome").Page("https://www.w3schools.com/howto/tryit.asp?filename=tryhow_html_nested_table").Panel("container").Panel("iframecontainer").Panel("iframe").Panel("iframewrapper").Frame("iframeResult")
        var frame = Aliases.frameIframeresult;
        var table1 = frame.FindElement("/html/body/table");
        Sys.HighlightObject(table1);
        
        var table2 = table1.FindElement("//*/td[2]/table");
        Sys.HighlightObject(table2);
       
        var cells = table2.FindElements("//*/td[2]/table/tbody/tr");
        for (var i = 0; i < cells.length; i++) {
            Sys.HighlightObject(cells[i]);
        }
    }

     

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    You can use FindElement method on a mapped object. Not sure if this is a good example,

     

    function TestX()
    {
        // URL: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_html_nested_table
        
        // Aliases.frameIframeresult = Sys.Browser("chrome").Page("https://www.w3schools.com/howto/tryit.asp?filename=tryhow_html_nested_table").Panel("container").Panel("iframecontainer").Panel("iframe").Panel("iframewrapper").Frame("iframeResult")
        var frame = Aliases.frameIframeresult;
        var table1 = frame.FindElement("/html/body/table");
        Sys.HighlightObject(table1);
        
        var table2 = table1.FindElement("//*/td[2]/table");
        Sys.HighlightObject(table2);
       
        var cells = table2.FindElements("//*/td[2]/table/tbody/tr");
        for (var i = 0; i < cells.length; i++) {
            Sys.HighlightObject(cells[i]);
        }
    }

     

     

  • torus's avatar
    torus
    Contributor

    thank you for the help rraghvani 

     

    I see now that I had a typo in my xpath. I had

     

     

    var child3 = tableCell.FindElement("//input[contain(@type,'checkbox')]");

     

     

    but the 'contain' should have been 'contains'.

     

     

    var child3 = tableCell.FindElement(".//input[contains(@type,'checkbox')]");

     

    Additionally, I needed to start the xpath out with a dot (period) indicating that TestComplete should search for the checkbox within the tableCell object ... only search for the checkbox within the tableCell ... don't search the entire page.

    https://stackoverflow.com/questions/35606708/what-is-the-difference-between-and-in-xpath

     

    With this change, the code I originally posted works great now. Thank you again for the help. I am glad to know that I can use FindElement with NameMapping objects.