protractorJS柴-如何使用getText()断言包含字符串的数组中元素的文本?

时间:2019-01-11 23:18:59

标签: protractor cucumber chai gherkin

当尝试对元素数组中找到的文本进行断言时,我得到一个错误消息

 AssertionError: expected [ Array(1) ] to include 'This profile exists already and has two places.'

我在页面目标文件中声明了webelement引用

我创建了一个包含一些代码的步骤,以验证元素数组中的文本

这是在页面对象中声明的webelement引用:

get importErrorsList(){
    return element.all(by.css('[ng-if="error.error.detailMessage"]'));
}

这是我尝试检查Web元素数组中的文本的方式

                directoriesPageObj.importErrorsList.getText().then(function(text) {         
                    console.log('test console output: ' + text);
                    expect(text).to.contain(errorText);
                    callback();
                });

实际:我收到断言错误

预期: 测试通过。

请注意,在该步骤的代码中,我有一个console.log代码段,该代码段输出的字符串确实包含要搜索的字符串: 测试控制台输出:com.reputation.imex.imp.ImportException:此配置文件已经存在,并且有两个位置。此配置文件不支持使用CSV导入

2 个答案:

答案 0 :(得分:0)

importErrorsList返回ElementArrayFinder

如果应用程序仅返回与css selecror匹配的一个元素,则将您的方法更改为:

get importErrorsList(){
    return element.all(by.css('[ng-if="error.error.detailMessage"]'));
}

但是,如果您可以ElementArrayFinder,请将expect更改为:

expect(text).to.deep.contain(errorText);

答案 1 :(得分:0)

这最终对我有用:

expect(directoriesPageObj.importErrorsList.getText()).to.eventually.contain(errorText).and.notify(callback);

相关问题