0

I'm trying to return the options present in the list using the below function.

HTML

<select>
  <option>one</option>
  <option>two</option>
  <option>three</option>
<select>

Function

getValues(){
  var ele = element(by.xpath("......../select"));
  return ele.all(by.tagName('option')).getAttribute('value').getText().then(function (text){
  for (var i = 0; i < text.length; i++) {
    text[i] = text[i].trim();
  }
  return text;
 });        

}

When I print the values in console using console.log(getValues()), the console displays theManagedPromise{.....} function instead of options.

But I'm expecting the options as ["one","two","three"] array. Can anyone help me?

4
  • Can you add the HTML where you are trying to do this? Commented Jul 4, 2017 at 13:50
  • 1
    getValues().then(function(values){ console.log(values)}) Commented Jul 4, 2017 at 13:50
  • @SureshSalloju My issue is I need to return the values as array. I can able to print the values. Commented Jul 4, 2017 at 14:11
  • your select isn't closed correctly Commented Jul 5, 2017 at 20:20

3 Answers 3

1

Why create a complex method for it if you can also do it like this

getValues() {
  return element.all(by.css('select option')).getText();
}

The getText() can also be executed on a ElementFinderArray and it will return a promise that contains ['one', 'two', 'three']

Update: with trim function

First of all your code will not work because of 2 reasons:

  1. protractor uses promises, you first needs to resolve the promises before you can use the value. See also 1 of your previous questions you asked
  2. You are using .getAttribute('value').getText(). This will never work.

Below you will find a working example in 3 different styles.

// with your code
getValues() {
  var ele = element(by.xpath("......../select"));
  return ele.all(by.tagName('option'))
    .map(function(option) {
      return option.getText()
        .then(function(optionText) {
          return optionText.trim();
        });
    });
}

// with protractor shorthand notation
getValues() {
  return $$('select option').map((option) => {
    return option.getText()
      .then((optionText) => {
        return optionText.trim();
      });
  });
}

// 1 line
getValues() {
  return $$('select option').map((option) => option.getText().then((optionText) => optionText.trim()));
}

Sign up to request clarification or add additional context in comments.

1 Comment

Before returning the value I need to trim the values. So only create the above function.
0

The most elegant way is to use .map(). See the documentation.

The only thing you need to remember is that .map() returns A promise that resolves to an array of values returned by the map function.

Comments

0
<!DOCTYPE html>
<html>
<body>

<select id="sel">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
<p>Click the button to display values</p>

<button onclick="clickHandler()">Try it</button>

<p id="demo"></p>  
</body>
<script "text/javacript">
function clickHandler() {
    var options = document.getElementById("sel").childNodes;
    let results =[];
    [].forEach.call(options, function(option) {
      if (option instanceof HTMLOptionElement) {
        results.push(option.text); 
        // Swap for option.value if you like too would be lower case
      }
    });
    document.getElementById("demo").innerHTML = results.toString();
}
</script>
</html>

See live demo here
HTMLOptionElement on MDN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.