18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

Error in JS childNodes

JaoBrJaoBr Members
edited June 2010 in Sahi - Open Source
Hi Narayan

The combobox in the system that I want to test is actually an table, and all of it's itens are links.
What I need to do here is to get an link from this combobox(table) to "_click" on it, but without knowing which link will be clicked (I want the first one).
To do this I need to click in the button (an image) to show the elements in the combobox and then click in the first one. This button, like the link, will be click without knowing the id or the name of the button.

I did all of this in javascript via Sahi Controller, and worked, here is the code:
// to get the button that will be clicked
var id = "formularioCadastro.UnidadeReferenciaId";
var divFrame = document.getElementById("iContent");
var frame = divFrame.childNodes[0];
var id_combo = frame.contentWindow.document.getElementById(id);
var id_btn_combo = id_combo.childNodes[2].childNodes[0].childNodes[0].childNodes[0].childNodes[1].id;
alert(id_btn_combo);
// worked fine, the alert show the right id of the button

// to get the link that will be clicked
var id = "formularioCadastro.UnidadeReferenciaId";
var divFrame = document.getElementById("iContent");
var frame = divFrame.childNodes[0];
var id_combo = frame.contentWindow.document.getElementById(id);
var href = id_combo.childNodes[3].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].innerHTML;
alert(href);
// worked fine too, it doesn't show the URL but show the name of the element, the same one that I already use in Sahi to click in the same link

Everything is perfect here :)
But now the problems shows up.

Here its the Sahi Script code that should do this to me:
var $id = "formularioCadastro.UnidadeReferenciaId";

/*
log in the system
*/

selectCombo($id);

function selectCombo($id){
    var $button = "";
    var $href = "";
    _popup(/elis.*/)._set($button, getBtn($id));
    _popup(/elis.*/)._click(_image($button));
    _popup(/elis.*/)._set($href, getLink($id));
    _log($href); // I used "_log" to record the return of the function, but here I'll just click in the link
    
    <browser>
    function getBtn(id){
        try{
            var divFrame = document.getElementById("iContent");
            var frame = divFrame.childNodes[0];
            var id_combo = frame.contentWindow.document.getElementById(id);
            var id_btn_combo = id_combo.childNodes[2].childNodes[0].childNodes[0].childNodes[0].childNodes[1].id;

            return id_btn_combo;
        }
        catch(e){
            return e;
        }
    }
    </browser>
    
    <browser>
    function getLink(id){
        try{
            var divFrame = document.getElementById("iContent");
            var frame = divFrame.childNodes[0];
            var id_combo = frame.contentWindow.document.getElementById(id);
            var href = id_combo.childNodes[3].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].innerHTML;
            
            return href;
        }
        catch(e){
            return e;
        }
    }
    </browser>
}

The function "getBtn()" works fine, after this Sahi can click on the button.
The problem is the function "getLink()", as you can see here:

errorpxf.jpg

The exception may be something like this:
{"name":"TypeError","message":"'childNodes.3.childNodes.0.childNodes' is null or not an object","number":-2146823281,"description":"'childNodes.3.childNodes.0.childNodes' is null or not an object"}

Also, I tried in the Sahi Script this: "var href = id_combo.childNodes[3].tagName;"
It returned the right tag name (DIV). But when I added another "childNodes[0]" in the same line, the same error ocurred (not the same, but a similar one).


Can you help me?
I can post the snippet of source code that contains what I want.

Comments

  • narayannarayan Administrators
    Hi JaoBr,

    Your code looks fine. It looks like the childNodes[3] actually does not have anything inside it. ie. childNodes.length = 0.

    Btw, Sahi has a few APIs like _near and _in which simplifies what you are trying to do. You will not need to write such complex javascript code to get to your elements.

    Regards,
    Narayan
  • JaoBrJaoBr Members
    Thanks for the reply Narayan.

    I tried only with the function getLink, and it worked!
    I don't know why, but when Sahi clicks on the button, Sahi becomes unable to get the link, even if I try to do it without the getBtn function, even using only "_popup("x")._click(_image("my button"))"

    I could use it by this way, but clicking on the link without clicking on the combo, the system throws an exception that opens an popup, apparently it doesn't do anything that could interfer in the test proccess, but it's better without this.

    I'll try with the APIs :)

    Thanks
  • JaoBrJaoBr Members
    I tried with the API _in and I got the same error (I think).

    The code:
    /*
    doing something
    */
    
    var $id = "formularioCadastro.UnidadeReferenciaId";
    
    /*
    doing something
    */
    
    try{
        var $t="";
        _popup(/elis.*/)._set($t, _image(0, _in(_div($id))).id);
        //_popup(/elis.*/)._click(_image($t));
        var $l="";
        _popup(/elis.*/)._set($l, _link(0, _in(_div($id))).innerHTML);
        _popup(/elis.*/)._click(_link($l));
    }
    catch($e){
        _logException($e);
    }
    _popup(/elis.*/)._click(_link("Sair"));
    

    The error:

    imagem2nhd.jpg

    "is null or not an object"

    And again, without clicking on the button, the link will clicked but it throws an exception.
  • JaoBrJaoBr Members
    Hi Narayan.

    I figured out what was going on, whenever the button is clicked, the html code behind it is changed, but I'm still able to get what I was needing with a few changes in the Sahi Script.

    Thanks for the help.
This discussion has been closed.