In the documentation for the protractor, I see 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'); });
It is clear here that you can use "by.model" to set values โโin the input field, but if you want to look at the input window and see what is in it, you need to use "by.binding" ,.
I have a set of code where (in the summary) I do:
element(by.model('risk.name')).sendKeys('A value'); expect(element(by.model('risk.name')).getText()).toEqual('A value');
(in my real code, I save the object, and then return to it in edit mode, and I verify that my value has actually been saved. But it comes down to the same thing, and this code example gives the same problem).
This gives me an error:
Error: Expected '' to equal 'A value'.
In theory, following an example from the docs, I can instead do:
element(by.model('risk.name')).sendKeys('A value'); expect(element(by.binding('risk.name)).getText()).toEqual('A value');
But the binding does not seem like a fully qualified model, I get an error message:
Error: No element found using locator: by.binding("risk.name")
It works (after the mod) if I do this:
element(by.model('risk.name')).sendKeys('A value'); expect(element(by.binding('name')).getText()).toEqual('A value');
This finds the item, but also gives a warning that I have more than one item that matches "name". And, unfortunately, the one he chooses is not correct.
So, two questions:
- If the by.model method can return getText (), or is there a design decision that it does not, and we need to use by.binding instead?
- Should I use a fully bound object in the by.binding directory, or is there a design solution that by.binding does not like the full name of the model? If so, what other qualifier can I use to choose between my different bindings?
EDIT:
I also tried the solution suggested by vdrulerz, I changed the code as follows:
element(by.model('risk.name')).getText().then(function(text) { console.log(text); expect(text).toEqual('A risk name'); });
The .log console returns an empty value (not a promise or object), and waiting does not give a message:
Expected '' to equal 'A risk name'.
My understanding is that the protractor is already correcting the expected answer to the promise, so I feel that the main problem is that getText does not work in the field identified by the model (I can successfully getText on shortcuts and other widgets).
I can also run the following code using getAttribute and not getText ():
expect(element(by.model('risk.name')).getAttribute('autofocus')).toEqual('true'); element(by.model('risk.name')).getAttribute('autofocus').then(function(text) { console.log(text); expect(text).toEqual('true'); });
The first part passes - the wait works. The second part also works, assuming that the vdrulerz syntax is also valid and it writes "true" to the console. I think there is a potential defect with getText?