How to getText on protractor input

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?

+75
angularjs protractor
Dec 01 '13 at 8:52
source share
7 answers

This is the answer to frequently asked questions in Protractor: https://github.com/angular/protractor/blob/master/docs/faq.md#the-result-of-gettext-from-an-input-element-is-always -empty

GetText result from input element is always empty

This is quirk webdriver. and elements always have empty getText values. Try instead:

 element.getAttribute('value') 

Regarding question 2, yes, you should be able to use the full name for .binding. I suspect that your template does not have an element associated with risk.name via {{}} or ng-bind.

+136
Dec 04 '13 at 22:10
source share

getText() function will not work the same as for webdriver, to make it work for the protractor, you will need to wrap it in a function and return the text, as we did for our protractor structure, we saved it in a common function, for example -

 getText : function(element, callback) { element.getText().then (function(text){ callback(text); }); }, 

In this case, you can have the text of the element.

Let me know if this is still unclear.

+5
Dec 03 '13 at
source share

I had this problem, I tried the Jmr solution, but it didnโ€™t work for me. Since all input fields have ng-model attributes, I could pull out the attribute and evaluate it and get the value.

HTML

 <input ng-model="qty" type="number"> 

protractor

 var qty = element( by.model('qty') ); qty.sendKeys('10'); qty.evaluate(qty.getAttribute('ng-model')) //-> 10 
+1
Sep 02 '16 at 5:31 on
source share

You can try something like this

 var access_token = driver.findElement(webdriver.By.name("AccToken")) var access_token_getTextFunction = function() { access_token.getText().then(function(value) { console.log(value); return value; }); } 

What can you call this function, where do you want to get the value.

0
Jul 01 '15 at 13:08
source share

This code works. I have a date input field that was set to read only, which forces the user to select from the calendar.

for start date:

 var updateInput = "var input = document.getElementById('startDateInput');" + "input.value = '18-Jan-2016';" + "angular.element(input).scope().$apply(function(s) { s.$parent..searchForm[input.name].$setViewValue(input.value);})"; browser.executeScript(updateInput); 

for end date:

 var updateInput = "var input = document.getElementById('endDateInput');" + "input.value = '22-Jan-2016';" + "angular.element(input).scope().$apply(function(s) { s.$parent.searchForm[input.name].$setViewValue(input.value);})"; browser.executeScript(updateInput); 
0
Jan 20 '16 at 16:36
source share

below code works for me to get text with input

 return(this.webelement.getAttribute('value').then(function(text) { console.log("--------" + text); })) 
0
Oct 03 '17 at 5:23
source share

You can use jQuery to get the text in the text box (work well for me), check the image details

the code:

 $(document.evaluate( "xpath" ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue).val() Example: $(document.evaluate( "//*[@id='mail']" ,document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue).val() 

Paste this above request into your code. Image Details:

enter image description here

-one
Nov 06 '17 at 6:44
source share



All Articles