0

I am writing a test which takes into consideration whether a select has been pre-populated.

it('should select field if it was not selected', function() {
  var usersField = element(by.id('users'));

  usersField.getAttribute('value').then(function(value) {
    console.log('value = ', value);

    if(!value) {
      //select first option if not pre-populated
      usersField.all(by.tagName('option')).get(0).click();
    }
  });
});

I have tried usersField.getText() but that just lists all the options from the dropdown so it doesn't help. My console log returns ? object:null ? for the value. Why is that? How can I simply check the value of the selected option if it's there and if not, select the first option?

LazioTibijczyk
  • 1,701
  • 21
  • 48
  • Does this answer your question? [How to get selected option value in protractor](https://stackoverflow.com/questions/47588304/how-to-get-selected-option-value-in-protractor) – DublinDev Jan 16 '20 at 12:06

1 Answers1

1

Did you make sure the element actually has a value attribute? maybe it doesn't. And also, have you tried with isSelected() method?

var foo = element(by.id('foo'));
expect(foo.isSelected()).toBe(false);
Joaquin Casco
  • 704
  • 5
  • 14