Javascript 当在字符串中找到部分正确的量角器时,将预期结果设置为true
大家好,我得到了以下代码:Javascript 当在字符串中找到部分正确的量角器时,将预期结果设置为true,javascript,protractor,Javascript,Protractor,大家好,我得到了以下代码: it('Search for string', function () { var MySearch = element(by.model('searchQuery')); MySearch.sendKeys('Apple Pomace'); expect(MySearch.getAttribute('value')).toBe('Apple Pomace'); element(by.buttonTex
it('Search for string', function () {
var MySearch = element(by.model('searchQuery'));
MySearch.sendKeys('Apple Pomace');
expect(MySearch.getAttribute('value')).toBe('Apple Pomace');
element(by.buttonText('Search')).click();
//browser.pause();
var optionTexts = element.all(by.repeater('product in products')).map(function (Options) {
return Options.getText();
});
optionTexts.then(function (array){
expect(array).toContain("Apple Pomace");
});
});
expect(array).toContain('Apple Pomace');
然后我得到的结果是:
[ 'Apple Pomace\nFinest pressings of apples. Allergy disclaimer: Might contain traces of worms. Can be sent back to us for recycling.\n0.89' ]
现在我想检查字符串是否包含苹果渣
我尝试了以下代码:
it('Search for string', function () {
var MySearch = element(by.model('searchQuery'));
MySearch.sendKeys('Apple Pomace');
expect(MySearch.getAttribute('value')).toBe('Apple Pomace');
element(by.buttonText('Search')).click();
//browser.pause();
var optionTexts = element.all(by.repeater('product in products')).map(function (Options) {
return Options.getText();
});
optionTexts.then(function (array){
expect(array).toContain("Apple Pomace");
});
});
expect(array).toContain('Apple Pomace');
然后我得到:
Expected [ 'Apple Pomace
Finest pressings of apples. Allergy disclaimer: Might contain traces of worms. Can be sent back to us for recycling.
0.89' ] to contain 'Apple Pomace'. <Click to see difference>
预期的['苹果渣
最好的苹果压榨品。过敏免责声明:可能含有蠕虫的痕迹。可以送回我们进行回收。
0.89']含有“苹果渣”。
即使整个字符串与结果不匹配,如何将测试设置为true
或者将字符串验证为第一个“\”
提前感谢首先
元素。all(by.repeater('products in products')).getText()将返回字符串数组。如果使用在数组上包含
匹配器,它将检查数组中是否存在整个字符串
在本例中,您需要检查整个数组中是否有与单词Apple porace
匹配的字符串。要实现这一点,您需要将结果数组转换为字符串,然后应用toContain
matcher
var displayedResults = element.all(by.repeater('product in products')).getText()
.then(function(resultArray){
return resultArray.join(); // will convert the array to string.
})
expect(displayedResults).toContain("Apple Pomace");
希望这对你有帮助 多谢各位!我要试试看!