0

I am trying to create a method that returns a string of an option in a dropdown selector given the specific index. I want to then call that method and store the string in a variable. Note: calling the function in protractor tests.

Here is my code:

returnStringDropDownValue = function(elementId, index) {
    var returnValue;
    var options = element(by.id(elementId)).findElements(by.tagName('option'))
        .then(function(options){
           returnValue =  options[index].getText();
        });

    return returnValue;
};


var x = returnStringDropDownValue('myId' ,1);

Whenever I call this method it returns undefined.I am new to javascript and protractor, can you please help?

Todd
  • 5,314
  • 3
  • 28
  • 45
AlphaWolf
  • 183
  • 4
  • 16

2 Answers2

1

This is an inherent issue with async code in JavaScript. The value gets filled at some later point inside the then. The then is actually run after your function returns. You need to return the promise:

returnStringDropDownValue = function(elementId, index) {
    return element(by.id(elementId)).findElements(by.tagName('option'))
        .then(function(options){
           return options[index].getText();
        });
};

Then unwrap it when you want to use it:

returnStringDropDownValue().then(function(value){
    // use value here
});
Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Can I somehow assign the value to a variable? if so, how? – AlphaWolf Dec 09 '14 at 22:27
  • @AlphaWolf no, you can not. This is a fundamental part of concurrency in JS. You can assign the promise to a value and `then` it at a later point but you can not just access the variable. – Benjamin Gruenbaum Dec 09 '14 at 22:46
  • The reason why I need to store it into a variable is because my test is being executed another html page (that doesn't contain the select dropdown anymore), and I want to check whether the current page in the test uses the same string as the string from the variable that will be stored (from page containing selector). This is not possible at all? sorry for not pointing that out in the question itself. – AlphaWolf Dec 09 '14 at 22:57
  • `var val = returnStringDropDownValue` and then you can later `val.then` even after it's gone from the actual page. – Benjamin Gruenbaum Dec 09 '14 at 22:58
-2

Try this:

CODEPEN

  function returnStringDropDownValue(elementId, index) {
      return document.getElementById(elementId).options[index].value;
  }
Todd
  • 5,314
  • 3
  • 28
  • 45
  • 1
    This doesn't work because I'm calling it in my protractor tests, which gives me "document is not defined" etc. – AlphaWolf Dec 09 '14 at 22:44
  • why the downvote? This is exactly what you asked for in the question. Don't select as correct -- that would be fine. But to downvote is a bit harsh when the question was ambiguous. – Todd Dec 09 '14 at 22:52
  • The downvote is because you did not read the whole question and provided an incorrect answer. You assumed that you understand the problem but didn't - which is a fair assumption to make often here in StackOverflow. Don't be surprised if when it doesn't check you get a downvote - that's sort of what down votes are for after all... cheers. – Benjamin Gruenbaum Dec 09 '14 at 23:00
  • Tell me where in the question it asks for a function that will be called from a protractor test. Other than the edit I proposed. – Todd Dec 09 '14 at 23:03