量角器 - 比较2个阵列的差异

时间:2017-05-09 12:15:47

标签: javascript arrays automation jasmine protractor

我有一个自动化场景,其中有2个阵列各有10个网络链接,预期结果是比较两个数组内容并验证10个中的一些(非固定数字)是否不同。

我尝试了下面的方法,我使用expect比较两个数组:

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] 

在这种情况下2我希望测试用例失败,因为数组元素相同

更新*************************** 更确切地说: 案例1:

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中的数组比较需要通过&amp;案例2需要失败

2 个答案:

答案 0 :(得分:1)

当你比较索引时,你不能把它们放在那样的期望中 它们将被视为文本,由于订单不正确,因此将被视为不相等。

我不确定是否有特定的方法,但我尝试为您的问题创建一个函数:

describe('test', function() {

    var text1 = ['sam','tom','jam','sil']; 
    var text2 = ['tom','jam','sil','sam'];

    it('should...', function() {
        blnSimilar = true
        compareArray(0, text1, 0, text2); //it compares value in text1 >> text2
        compareArray(0, text2, 0, text1); //it compares value in text2 >> text1
        expect(blnSimilar).toBe(true);
    })

    var blnSimilar;
    function compareArray(i, arr1, j, arr2) {
        if (i < arr1.length) {
            if (j < arr2.length) {
                if (arr1[i] == arr2[j]) {
                    compareArray(i + 1, arr1, 0, arr2)
                } else {
                    compareArray(i, arr1, j + 1, arr2)
                }
            } else {
                blnSimilar = false;
            }
        }
    }

});

如果text1和text2的值如下,blnSimilar将变为false

    var text1 = ['sam','tom','jam']; 
    var text2 = ['tom','jam','sil','sam'];

    var text1 = ['sam','tom','jam','sil']; 
    var text2 = ['tom','sil','sam'];

希望它能回答你的问题。

答案 1 :(得分:0)

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() {

        console.log('Text via global defined variable text1 is ' + text1);
        blnSimilar = true
            compareArray(0, text1, 0, text2); //it compares value in text1 >> text2
            compareArray(0, text2, 0, text1); //it compares value in text2 >> text1
            expect(blnSimilar).not.toBe(true);

    });


    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);
    }
    }

     var blnSimilar;
      function compareArray(i, arr1, j, arr2) {
          if (i < arr1.length) {
              if (j < arr2.length) {
                  if (arr1[i] == arr2[j]) {
                      compareArray(i + 1, arr1, 0, arr2)
                  } else {
                      compareArray(i, arr1, j + 1, arr2)
                  }
              } else {
                  blnSimilar = false;
              }
          }
    }
});
相关问题