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.

Why doesn't Sahi have a _parent DOM relation?

prutledgeprutledge Members
edited July 2014 in Sahi Pro
Why doesn't a DOM relation
_parent
exist in the API? This would only search against elements which are parents of the provided element. This would add functionality because currently, using
_parentNode
, you can only specify a tag type and an occurrence index (and it's not a DOM relation, it's an accessor).

Ideally, it would look something like this. To get the first DIV that is a parent of a TABLE called "myTable" and where the DIV has the class "container", I would do:
var $firstDiv = _div(0, {className: "container"}, _parent(_table({id:"myTable"})));

As a side note, you can implement something to achieve similar functionality (but unfortunately, a less convenient interface since it's not really a DOM relation) by doing the following:
function findParent($apiType, $tagName, $identifier, $childNode){

        //start search on the first parent
        var $parent = _parentNode($childNode, $tagName);

        do{

            //getting reference to old parent
            var $child = $parent;

            //setting parent to parent of old parent
            $parent = _parentNode($parent, $tagName);

            //getting all children of the new parent that match the identifier
            var $children = _collect($apiType, $identifier, _in($parent));

            //checking each match to see if they are a parent of the original childNode
            for(var $index in $children){

                //if element matches and is a parent, we can return the previous parent
                //in other words, ($children[$index] === $child) at this point
                if(_contains($children[$index], $childNode)){
                    return $child;
                }
            }

        } while($parent != null);

        return null;
    }

And using the above like so:
var $firstDiv = findParent("_div","DIV",{className:"container"},_table({id:"myTable"}));

Furthermore, I'm aware that while my temporary solution works, you could likely achieve a much more efficient version by writing it in the API itself.

Best Answer

  • Well, if you'd be using Sahi OS you could pretty much implement the API yourself :) but i think as Sahi Pro user you can write a mail to support@sahi.... you might get an answer that way

    Regards
    Wormi

Answers

  • globalwormingglobalworming Moderators
    edited July 2014
    Hi

    When I recall correctly, the _near API achieves the same.

    _div($selector, _near($childSelector)) should give you the nearest DOM parent which is a div($selector), if the $childSelector excludes the DOM neighbours... not sure though

    Regards
    Wormi
  • I'm pretty sure _near includes the neighbors, that's the whole reason I wrote the above. Plus, the _near relation is limited in scope (I believe up to seven nesting levels)
  • That's it? Can we do something about this? It can't be that hard of a change seeing as I can already write code to mimic the behavior.
Sign In or Register to comment.