Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locating parent elements with parentElementArrayFinder

There is a parentElementArrayFinder attribute available on an ElementFinder object which, from I understand, may return a parent element of the current:

var myElement = $(".myclass");
var parentElement = myElement.parentElementArrayFinder;

It is, though, not documented as a part of Protractor's public API. Is parentElementArrayFinder a reliable and stable method to locate a parent element and would always return the same element as myElement.element(by.xpath(".."))?

like image 510
alecxe Avatar asked Aug 14 '17 13:08

alecxe


2 Answers

I found the code for the ElementFinder here which has a prop for parentElementArrayFinder here.

From what I have found in the code. It throws an error if parentElementArrayFinder does not exist.

super();
if (!elementArrayFinder) {
  throw new Error('BUG: elementArrayFinder cannot be empty');
}
this.parentElementArrayFinder = elementArrayFinder;

From this, we can safely assume it will always be there so I think it is safe to use.

like image 150
Michael Warner Avatar answered Nov 16 '22 17:11

Michael Warner


To summarize comments, the existing answer, my observations and the source code, parentElementArrayFinder should and can only be used if the element you get parentElementArrayFinder for was "chained" from an element.

This will work:

var parent = element(by.css(".parent"));
var child = parent.element(by.css(".child"));

child.parentElementArrayFinder  # link to "parent" - defined

This will not:

var child = element(by.css(".child"));

child.parentElementArrayFinder  # no link to "parent" - undefined
like image 2
alecxe Avatar answered Nov 16 '22 17:11

alecxe