如何在量角器的输入上获取文本

在关于量角器的文档中,我看到了以下示例:

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');
});

这里显而易见的是,您可以使用“ by. model”来设置输入框中的值,但是如果您想查看输入框并查看其中的内容,则需要使用“ by. binding”。

我有一套代码,在那里(概括地说)我做:

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

(在我的实际代码中,我保存了实体,然后在编辑模式下返回给它,我检查我的值实际上被保存了。但是它仍然归结为同样的事情,这个示例代码给出了同样的问题)。

这给了我一个错误:

Error: Expected '' to equal 'A value'.

理论上,按照文档中的例子,我可以这样做:

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

但是 by.binding 似乎并不喜欢完全限定模型,我得到了一个错误:

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

如果我这样做,它确实有效(勉强) :

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

这会找到一个元素,但也会提示我有多个匹配“ name”的元素。不幸的是,它选择的不是正确的。

有两个问题:

  1. 模型是否应该返回一个 getText () ,或者是否存在一个设计决策,它没有返回 getText () ,而我们需要使用 by. binding 来代替?
  2. 我是否应该在 by.binding 中使用一个完全限定的实体,或者有一个设计决策,by.binding 不喜欢完整的模型名称?如果是这样,我可以使用哪些其他限定符来在不同的绑定之间进行选择?

编辑:

我也尝试了 vdrulerz 建议的解决方案,我修改了代码如下:

element(by.model('risk.name')).getText().then(function(text) {
console.log(text);
expect(text).toEqual('A risk name');
});

Log 返回一个空值(不是承诺或对象) ,并且期望值不能给出消息:

Expected '' to equal 'A risk name'.

我的理解是,量角器已经补丁了处理承诺的期望,所以我觉得根本问题是 getText 不能在通过模型识别的字段上工作(我可以成功地在标签和其他小部件上获得文本)。

我还可以使用 getAttribute 而不是 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');
});

第一部分通过-期望工程。第二部分也可以工作,这表明 vdrulerz 的语法也是有效的,并且它将“ true”记录到控制台。我认为 getText 有潜在的缺陷?

111529 次浏览

getText() function won't work like the way it used to be for webdriver, in order to get it work for protractor you will need to wrap it in a function and return the text something like we did for our protractor framework we have kept it in a common function like -

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


},

By this you can have the text of an element.

Let me know if it is still unclear.

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

The result of getText from an input element is always empty

This is a webdriver quirk. and elements always have empty getText values. Instead, try:

element.getAttribute('value')

As for question 2, yes, you should be able to use a fully qualified name for by.binding. I suspect that your template does not actually having an element that is bound to risk.name via \{\{}} or ng-bind.

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;
});
}

Than you can call this function where you want to get the value..

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

for a 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 an 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);

I had this issue I tried Jmr's solution however it didn't work for me. As all input fields have ng-model attributes I could pull the 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

below code works for me, for getting text from input

return(this.webelement.getAttribute('value').then(function(text)
{
console.log("--------" + text);
}))

You can use jQuery to get text in textbox (work well for me), check in image detail

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()

Inject this above query to your code. Image detail:

enter image description here

You have to use Promise to print or store values of element.

 var ExpectedValue:string ="AllTestings.com";
element(by.id("xyz")).getAttribute("value").then(function (Text) {


expect(Text.trim()).toEqual("ExpectedValue", "Wrong page navigated");//Assertion
console.log("Text");//Print here in Console


});