getText() not working on input elements
See original GitHub issueI’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:
- Created 10 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
Check out this question in the FAQ: https://github.com/angular/protractor/blob/master/docs/faq.md#the-result-of-gettext-from-an-input-element-is-always-empty
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.