question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getText() not working on input elements

See original GitHub issue

I’m having a problem where getText() is working on labels, but not working on input elements. I’ve raised a question on stackoverflow: http://stackoverflow.com/questions/20310442/how-to-gettext-on-an-input-in-protractor on the assumption that this is probably a usage error on my part, but I’m not seeing anything that I’m doing that’s wrong or that’s obvious. It’s possible that getText is broken, so I’m raising here.

My use case is that I can sendKeys to my input element, I can expect and retrieve attributes from that input element, but I cannot getText that input element:

element(by.model('risk.name')).sendKeys('A value');
expect(element(by.model('risk.name')).getAttribute('autofocus')).toEqual('true');
expect(element(by.model('risk.name')).getText()).toEqual('A value');

The first two work, the third does not match the expect, returning:

Expected '' to equal 'A value'.

The documentation provides the following example:

describe('by model', function() {
  it('should find an element by text input model', function() {
    var username = element(by.model('username'));
    username.clear();
    username.sendKeys('Jane Doe');

    var name = element(by.binding('username'));

    expect(name.getText()).toEqual('Jane Doe');
  });

Which might suggest that getText() isn’t supposed to work on a by.model, and that instead I should use by.binding. This doesn’t make intuitive sense to me, but more importantly, by.binding doesn’t like a fully qualified model name. So:

expect(element(by.binding('risk.name)).getText()).toEqual('A value');

Returns an error:

Error: No element found using locator: by.binding("risk.name")

Without the full qualification it works, but unfortunately for me “name” matches more than one item on my page, and the way I’m compositing pages means that sometimes there are more elements than others - so I cannot easily rely on this.

expect(element(by.binding('name')).getText()).toEqual('A value');

I could change my attribute name, which I think would work, but this is inconvenient in terms of my naming standards, and means that my testing logic would rely on every attribute being globally unique (i.e. if I put two views on the same page, I have to guarantee there are no name collisions in the attributes).

Is needing to use by.binding instead of by.model a deliberate design decision? Is the fact that by.binding doesn’t accept a fully qualified name a deliberate design decision?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
juliemrcommented, Dec 5, 2013

No problem! And sorry if my reply was curt, I’ve answered this question too many times 😃 - which obviously means it does need to be easier to find the answer. Just a quick question - was the FAQ hard to find, or was the question inside the FAQ unclear?

I think it makes sense to clarify on the readme - I’ll add something.

Read more comments on GitHub >

github_iconTop Results From Across the Web

getText() on input is not returning text - java - Stack Overflow
getText() returns the innerText held by the element ... It is called void element. Input does NOT work this way to use getText()....
Read more >
How To Get Text Of An Element In Selenium? - LambdaTest
Selenium offers a getText() method used to get the text of an element, i.e.; ... inner text (which is not hidden by CSS)...
Read more >
element.text (similar to getText() from java) is not working and ...
Working solution: Element is normal text field: element.text ... Element is input field:input_element.get_attribute("value").
Read more >
<input type="text"> - HTML: HyperText Markup Language | MDN
<input> elements of type text create basic, single-line inputs. You should use them anywhere you want the user to enter a single-line value...
Read more >
.text() | jQuery API Documentation
The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found