如何在量角器中的输入上获取文本 [英] How to getText on an input in protractor

查看:19
本文介绍了如何在量角器中的输入上获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In the documentation for 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');
  });

这里看起来很清楚的是,您可以使用by.model"在输入框中设置值,但是如果您想查看输入框并查看其中的内容,则需要使用by.binding".

What appears clear here is that you can use "by.model" to set values in an input box, but if you want to look at an input box and see what's in it, you need to use "by.binding".

我有一组代码(总而言之):

I have a set of code where (in 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 entity then come back to it in edit mode, and I'm checking my value was actually saved. But it still boils down to the same thing, and this sample code gives the same problem).

这给了我一个错误:

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

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

In theory, following the 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');

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

But the by.binding doesn't appear to like the fully qualified model, I get an error:

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

如果我这样做,它确实有效(在某种程度上):

It does work (after a fashion) if I do:

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

这会找到一个元素,但也会警告我有多个与名称"匹配的元素.不幸的是,它选择的那个不是正确的.

This finds an element, but also gives a warning that I have more than one element that matches 'name'. And unfortunately the one it picks isn't the right one.

那么,两个问题:

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

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

I have also tried the solution suggested by vdrulerz, I modified the code as follows:

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

console.log 正在返回一个空白值(不是承诺或对象),并且期望未能给出消息:

The console.log is returning a blank value (not a promise or an object), and the expect fails giving the message:

Expected '' to equal 'A risk name'.

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

My understanding is that protractor already patches the expect to deal with the promise, so I feel that the underlying problem is the getText not working on a field identified via a model (I can successfully getText on labels and other widgets).

我还可以运行以下代码,使用 getAttribute 而不是 getText():

I can also run the following code, using getAttribute rather than 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 可能存在缺陷?

The first part passes - the expect works. The second part also works, suggesting that vdrulerz' syntax is also valid, and it logs 'true' to the console. I think there is potentially a defect with getText?

推荐答案

Protractor 常见问题解答中对此进行了回答:https://github.com/angular/protractor/blob/master/docs/faq.md#the-result-of-gettext-from-an-input-element-is-always-empty

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

输入元素的getText结果始终为空

The result of getText from an input element is always empty

这是一个 webdriver 怪癖.并且元素总是有空的 getText 值.相反,请尝试:

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

element.getAttribute('value')

至于问题 2,是的,您应该能够为 by.binding 使用完全限定名称.我怀疑您的模板实际上没有通过 {{}} 或 ng-bind 绑定到 risk.name 的元素.

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.

这篇关于如何在量角器中的输入上获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆