Protractor: how to read text value of a element

Created on 3 Oct 2013  ·  3Comments  ·  Source: angular/protractor

I am trying to read the text value of binding element. I have used function getText() to read the text value of elements but its not working. Is there any other way to do so

ptor = protractor.getInstance();
ptor.get('http://www.angularjs.org');

ptor.findElement(protractor.By.input("yourName")).sendKeys("Julie");

var greeting = ptor.findElement(protractor.By.binding("{{yourName}}!"));

console.log(greeting.getText());

Most helpful comment

@vishalshivnath Please check out the documentation for information on how webdriver and protractor work - protractor actions return promises, so you are probably a promise object.

@tennisgent There's a weird webdriver quirk that getText() is always empty for<input> elements. Try doing userNameInput.getAttribute('value') instead.

All 3 comments

I'm having the same issue. I just got protractor up and running but I can't seem to get data out of an input element. My test is:

it('should allow text entry into the userName field', function(){
    userNameInput = ptor.findElement(protractor.By.input('user.userName'));
    userNameInput.sendKeys(testUserName);
    expect(userNameInput.getText()).toEqual(testUserName);
});

but the test fails with:

1) Login Tests Sign In should allow text entry into the userName field
Message:
Expected '' to equal 'xacttester'.

As I watch the test (by slowing it down with ptor.driver.sleep(2000)), I watch it enter the text into the field, but then when it calls getText(), it returns an empty string.

Anyone have any ideas on what I'm doing wrong?

@vishalshivnath Please check out the documentation for information on how webdriver and protractor work - protractor actions return promises, so you are probably a promise object.

@tennisgent There's a weird webdriver quirk that getText() is always empty for<input> elements. Try doing userNameInput.getAttribute('value') instead.

is it ok to use $ instead of browser.findElement',

  it('should return the same result as browser.findElement', function() {
    browser.get('index.html');

   // var e = element(by.id('new_todo'));
    var e = $('#new-todo');
    e.sendKeys("foo");

    expect(e.getAttribute('value')).toEqual(
      "foo")
  });
Was this page helpful?
0 / 5 - 0 ratings