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(".."))?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With