Automation_User
Automation_User

Reputation: 69

Protractor-Comparison of 2 arrays for differences

I have an automation scenario in which there are 2 arrays having 10 weblinks each, the expected results is compare both array contents and verify if some out of 10(not fixed number) are different.

I have tried the below approach in which i am comparing both the arrays using 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);
    }
    }
});

But the drawback here is this testcase will pass in both the scenarios i.e:

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] 

In this case 2 i want the test case to fail as the array elements are same

Update *************************** To be more precise: case 1:

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

case 2:

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

array comparison in case 1 needs to pass & case 2 needs to fail

Upvotes: 0

Views: 988

Answers (2)

Automation Engr
Automation Engr

Reputation: 454

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

Upvotes: 0

PQ Co
PQ Co

Reputation: 517

When you are comparing index, you cannot put them in expect like that.
They will be considered as a text and since order is not correct, it will be considered as not equal.

I am not sure if there is a specific method but I tried creating a function for your issue:

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

});

If the value of text1 and text2 are the following, blnSimilar will become false:

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

or

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

Hope it answers your question.

Upvotes: 1

Related Questions