Reputation: 585
First time trying to e2e test my AngularJS application.
I have an ul with the class 'meta'. The ul has a couple of li's. I want to grab the first li and compare that to a string.
var duration = browser.findElements(by.className('meta')).all(by.tagName('li')).get(1);
But when running the test i keep getting the error: Failed: browser.findElements(...).all is not a function.
I already tried findElement and findElements and keep getting the same error.
I stumbled upon this previous question: Selecting second anchor element within lists using protractor. But it didn't help me.
Upvotes: 2
Views: 7921
Reputation: 473833
The currently accepted answer is correct in terms of what you should be using, but here is why your code did not work.
Protractor, as you may know, wraps the WebDriverJS
- selenium javascript bindings. On top of what is there, it adds a lot of convenient concepts like ElementFinder
or ElementArrayFinder
or the browser
object.
Protractor also provides you a way to call the pure WebDriverJS
functions and methods directly, like, for example, it allows to use findElement()
or findElements()
on the browser
object.
In your case, the browser.findElements(by.className('meta'))
would return an array of WebElement
s which is a pure WebDriverJS
concept. Note, this is not an ElementArrayFinder
instance, you are not at the Protractor territory at this point.
Now, you are trying to call .all()
function on this array of elements and it obviously fails.
The Protractor way of doing this would be:
var duration = $$('.meta li').first();
Or, you can even get away with a single selector:
var duration = $('.meta li:first-of-type');
Related reading:
Upvotes: 7
Reputation: 2375
It is better to use the "default" ElementFinder syntax of Protractor, that is for example element(by.css('ul'))
, see the docs
You will then get this
var duration = element(by.className('meta')).all(by.tagName('li')).get(1);
Or element.all(by.css('.meta li')).first();
, see the docs
Or the shorthand $$('.meta li')).first();
, see the docs
Upvotes: 2