ale*_*cxe 17 javascript testing string-matching jasmine protractor
问题:
我有一个promises数组,它被解析为一个字符串数组.现在,如果至少有一个字符串与正则表达式匹配,则测试应该通过.
目前,我使用简单的字符串连接解决它:
protractor.promise.all([text1, text2, text3]).then(function (values) {
expect(values[0] + values[1] + values[2]).toMatch(/expression/);
});
Run Code Online (Sandbox Code Playgroud)
显然,这不能很好地扩展,并且不是特别可读.
问题:
是可以使用它来解决定制茉莉匹配,或jasmine.any()或定制的非对称平等的测试?
正如评论中所说的那样,虽然我最初使用map,但是减少会让你做你需要的东西,而这种种姓至少会更有意义:
protractor.promise.all([text1, text2, text3]).then(function (values) {
expect(
values.reduce(function(p, v) {
return v.match(/expression/) || p;
}, false)
).toBe(true);
});
Run Code Online (Sandbox Code Playgroud)
或写同样的东西,但使用ES6箭头功能:
protractor.promise.all([text1, text2, text3]).then(function(values) {
exptect(
values.reduce( (p, v) => v.match(/expression/) || p, false )
).toBe(true);
});
Run Code Online (Sandbox Code Playgroud)
两者都做同样的事情,reduce回调将默认为false,直到v.match表达式求值为true.
我认为这对大多数人来说是显而易见的,但我认为我会提供语法和一些解释以供将来参考
也许这个解决方案可以进一步优化,一旦发现单个匹配就停止匹配模式:
protractor.promise.all([text1, text2, text3]).then(function (values) {
expect(
values.reduce(function(p, v) {
return p || !!v.match(/expression/);
}, false)
).toBe(true);
});
Run Code Online (Sandbox Code Playgroud)
我所做的就是使用当前的reduce值作为默认值(一旦设置为true,测试任何其他字符串值都没有意义).为了确保v.match计算布尔值而不是数组,我只是使用了!!v.match().那部分是可选的.在ES6中,同样的事情看起来像这样:
protractor.promise.all([text1, text2, text3]).then(function(values) {
exptect(
values.reduce( (p, v) => p || !!v.match(/expression/), false )
).toBe(true);
});
Run Code Online (Sandbox Code Playgroud)
对于大数据集,这可能会表现得更好(考虑到match找到第一个匹配后调用停止,而不是v.match每次都调用).
如果这样有效,
protractor.promise.all([text1, text2, text3]).then(function (values) {
expect(values[0] + values[1] + values[2]).toMatch(/expression/);
});
Run Code Online (Sandbox Code Playgroud)
我想你可以写如下:
protractor.promise.all([text1, text2, text3]).then(function (values) {
expect(values.join('')).toMatch(/expression/);
});
Run Code Online (Sandbox Code Playgroud)
它具有可扩展性.:)
您可以简单地使用map获取列表,boolean然后使用以下命令断言结果toContain(true):
var all = protractor.promise.all;
var map = protractor.promise.map;
expect(map(all([text1, text2, text3]), RegExp.prototype.test, /expression/)).toContain(true);
Run Code Online (Sandbox Code Playgroud)
您还可以使用自定义匹配器:
expect(all([text1, text2, text3])).toContainPattern(/expression/);
Run Code Online (Sandbox Code Playgroud)
自定义匹配器声明beforeEach:
beforeEach(function() {
jasmine.addMatchers({
toContainPattern: function() {
return {
compare: function(actual, regex) {
return {
pass: actual.some(RegExp.prototype.test, regex),
message: "Expected [" + actual + "] to have one or more match with " + regex
};
}
};
}
});
});
Run Code Online (Sandbox Code Playgroud)
如果那些[text1,text2,text3]是来自ElementFinder的文本,.getText()那么你也可以尝试预期条件(你知道我是EC的忠实粉丝吗?:)).
describe('test', function () {
it('test', function () {
var EC = protractor.ExpectedConditions;
browser.get('http://www.protractortest.org/testapp/ng1/#/form');
var textToContain = 'Check'; //Notice, that this is not 'equals', this is 'contains'
var elementTextToCheck1 = EC.textToBePresentInElement($('#checkboxes h4'), textToContain); // Here it will be true : Checkboxes
var elementTextToCheck2 = EC.textToBePresentInElement($('#animals h4'), textToContain); //false
var elementTextToCheck3 = EC.textToBePresentInElement($('#transformedtext h4'), textToContain); //false
var oneElementShouldContainText = EC.or(elementTextToCheck1, elementTextToCheck2, elementTextToCheck3);
expect(oneElementShouldContainText()).toBeTruthy(`At least one element should contain "${textToContain}"`);
})
});
Run Code Online (Sandbox Code Playgroud)
对于简单元素:http: //www.protractortest.org/#/api?view = ExpectedConditions.prototype.textToBePresentInElement
对于textArea,输入:http://www.protractortest.org/#/api? view = ExpectedConditions.prototype.textToBePresentInElementValue
请注意,不幸的是.textToBePresentInElement仅适用于单个ElementFinder,不支持ArrayElementFinder.但在这种情况下,您可以使用.filter()并断言返回的列表为空.
| 归档时间: |
|
| 查看次数: |
1211 次 |
| 最近记录: |