【问题标题】:Protractor-Comparison of 2 arrays for differences量角器-比较 2 个数组的差异
【发布时间】: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


【解决方案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'];

希望它能回答你的问题。

【讨论】:

    【解决方案2】:
    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;
                  }
              }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 2020-02-15
      • 2015-08-21
      • 1970-01-01
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多