使用量角器检查 DOM 元素中的文本 [英] Check text in a DOM element using Protractor

查看:44
本文介绍了使用量角器检查 DOM 元素中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在使用 Protractor 测试 Angular 应用程序时尝试做的事情.我想得到某个元素,有点像这样:

<p>{{textFromBoundModel}}</p>

然后获取它的 html,并检查它是否包含我期望的文本.

我尝试首先通过 cssContainingText 方法获取此元素,但效果不佳(不知道为什么;可能是因为段落中的文本是动态生成的).所以现在我只使用 by.css 定位器来获取这个元素.接下来,我正在检查它是否包含我正在测试的文本:

//这是 Cucumber.jsthis.Then(/^Doing my step"$/, function(callback){var el = element(by.css('.someClass'));expect(el).to.contain("一些有趣的字符串");打回来();});});

但这不起作用.问题是,el 是某种定位器对象,我无法弄清楚如何获取它找到的元素的 html 以便针对此 html 进行测试.尝试了 .getText(),但没有成功.

有什么建议吗?

解决方案

做:

expect(el.getText()).to.eventually.contain("一些有趣的字符串");

工作?

我相信您需要 .eventually 来等待承诺解决,并且您需要 .getText() 来获取 的内容div.

查看 开头的 chai-as-promised 内容黄瓜样品.

Here’s what I’m trying to do while testing an Angular app with Protractor. I would like to get a certain element, which is somewhat like this:

<div class="someClass">
   <p>{{textFromBoundModel}}</p>
</div>

then get its html, and check whether it contains the text that I expect it to have.

I tried to get this element first by the cssContainingText method, but it didn't quite work (not sure why; maybe because the text within the paragraph is produced dynamically). So now I’m getting this element using just the by.css locator. Next, I'm checking whether it contains the text I’m testing for:

// this is Cucumber.js    

this.Then(/^Doing my step"$/, function(callback){
    var el = element(by.css('.someClass'));            
    expect(el).to.contain("some interesting string");
    callback();
 });
});

but this doesn't work. Problem is, el is some kind of a locator object, and I can’t figure out how to get html of the element it found in order to test against this html. Tried .getText(), with no success.

Any suggestions?

解决方案

Does:

expect(el.getText()).to.eventually.contain("some interesting string");

work?

I believe you need the .eventually to wait for a promise to resolve, and you need the .getText() to get at the content of the div.

See the chai-as-promised stuff at the head of the cucumber sample.

这篇关于使用量角器检查 DOM 元素中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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