I want to fetch all the li
texts of suggestions after entering in the google search bar "Webdriver". I have wrote some code like this:
this.getElements = function(){
element.all(by.css('ul.sbsb_b')).then(function(text){
for(var i=0; i < text.length;i++)
{
console.log(text[i].getText());
}
});
};
On execution I m getting something like:
{ ptor_:
{ controlFlow: [Function],
schedule: [Function],
getSession: [Function],
getCapabilities: [Function],
quit: [Function],
instead of text values of Suggestions.
There is a set of javascript array-like functions in Protractor that allows you to filter()
or map()
result of async promise. For example if original element.all(by.css('something'))
would return list of ElementFinder
s, map()
allows to transform it into array of getText()
s, which in resolved state is just an array of strings. You can then use it however you want.
element.all(by.css('something')).map(function(elm) {
return elm.getText();
}).then(function(texts) {
texts.forEach(...);
});
We need to specify getText() to get list of text in element.all
<ul class="menu">
<li>Home</li>
<li> About Us</li>
</ul>
element.all(by.css('.menu li')).getText().then(function(menus) {
expect(menus.length).toBe(2); //true
console.log(menus);
});
log output
['Home','About Us']
Need more helper function
For looping through every element, you can also do something like:
element.all(by.css('ul.sbsb_b')).each(function(element, index) {
element.getText().then(function(text) {
console.log(text);
});
});
http://angular.github.io/protractor/#/api?view=ElementArrayFinder.prototype.each
To print the value from getText(), you need to resolve the promise using .then()
Something like this:
text[i].getText().then(function(textValue)
{
console.log(textValue);
});
Regards,
Sakshi
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