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(".."))
?
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(".."))
?
- Help me understand why you can't use by.xpath("..")? – Michael Warner Commented Aug 30, 2017 at 21:19
-
@MichaelWarner I can and do use
..
here and there;parentElementArrayFinder
is just something that caught my eye and I wonder what purpose does it have and if it can be used. Thanks. – alecxe Commented Aug 30, 2017 at 21:29 -
2
If i remember correctly
parentElementArrayFinder
holds the methods to retrieve the web elements from a chained locator/filter like$().$()
or$$().filter()
. It's not related to the parent element from the DOM. – Florent B. Commented Aug 31, 2017 at 15:26 - @FlorentB. right, that was my thinking initially but I figured to ask that to confirm. Please post as an answer to resolve the topic. Thanks. – alecxe Commented Aug 31, 2017 at 15:33
- Check this link as it is a similar kind of problem. github./angular/protractor/blob/… – abhishek Sharma Commented Sep 5, 2017 at 11:36
2 Answers
Reset to default 2I 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.
To summarize ments, 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