【发布时间】:2017-10-07 18:01:47
【问题描述】:
我有一个自动化场景,其中有 2 个数组,每个数组有 10 个网络链接,预期结果是比较两个数组内容并验证 10 个中的一些(非固定数字)是否不同。
我已经尝试了以下方法,在其中我使用期望比较两个数组:
describe('testing', function() {
var index = 'not found';
var text1 = [];
var text2 = [];
it('push elements', function() {
browser.ignoreSynchronization = true;
browser.get('https://www.w3schools.com/angular/');
browser.sleep(9000).then(function(){
});
var elm = element(by.id('leftmenuinner')).all(By.css('[target="_top"]'));
elm.count().then(function(count) {
pushToArray1(0, count, elm);
})
element(by.xpath(".//*[@id='topnav']/div/div[1]/a[5]")).click();
});
browser.sleep(9000).then(function(){
});
it('push elements', function() {
var elm = element(by.id('leftmenuinner')).all(By.css('[target="_top"]'));
elm.count().then(function(count) {
pushToArray2(0, count, elm);
})
})
it('Comparison of the array contents', function() {
expect(text1).not.toEqual(text2);
});
function pushToArray1(i, max, elm) {
if (i < max) {
elm.get(i).getText().then(function(tmpText) {
console.log(tmpText);
text1.push(tmpText);
})
pushToArray1(i + 1, max, elm);
}
}
function pushToArray2(i, max, elm) {
if (i < max) {
elm.get(i).getText().then(function(tmpText) {
console.log(tmpText);
text1.push(tmpText);
})
pushToArray2(i + 1, max, elm);
}
}
});
但这里的缺点是这个测试用例在两种情况下都会通过,即:
1. If the both array contents are different
2. If both the array elements are exactly similar but the order is jumbled.
i.e if array1 contains [sam,tom,jam,sil] and array2 contains[tom,jam,sil,sam]
在这种情况下,我希望测试用例失败,因为数组元素相同
更新*************************** 更准确地说: 案例一:
var text1 = ['sam','tom','jam','sil'];
var text2 = ['tom','jam','sil','sam'];
案例2:
var text1 = ['sam','tom','jam','sil'];
var text2 = ['tom','jam','sil','ronnie'];
情况 1 需要通过 & 情况 2 需要失败的数组比较
【问题讨论】:
-
是否期望在 text1 上找到的项目应该在 text2 上,而在 text2 上找到的项目应该在 text1 上?
-
没有必要。两个数组中可能有一些匹配,有些可能完全不同。
-
请检查我的答案,让我知道您的期望。由于您正在比较示例中的整个数组,因此我希望这些数组应该是相同的。
-
@PaulCo ...请检查我在问题正文最后一段中的更新..
-
如果您在下面尝试过我的回答,请告诉我
标签: javascript arrays automation jasmine protractor