如何的getText在量角器输入 [英] How to getText on an input in protractor

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

问题描述

在为量角器的文档,我看到了下面的例子:

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".

我有一套code在哪里(在摘要)我做的:

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

(以我的真实code我省则该实体回来给它的编辑模式,我检查自己的价值实际上是得救了。但它仍然归结为同样的事情,而这个样本code给出了同样的问题)。

(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提出的解决方案,我修改了code如下:

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

我也可以运行以下code,使用的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'语法也是有效的,而且记录真到控制台。我认为有可能与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?

推荐答案

这是在量角器常见问题解答:<一href=\"https://github.com/angular/protractor/blob/master/docs/faq.md#the-result-of-gettext-from-an-input-element-is-always-empty\">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。我怀疑你的模板实际上并不具有必然通过向risk.name元素{{}}或NG绑定。

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.

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

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