dhru soni
dhru soni

Reputation: 37

element.all not able to split data in protractor testing

comparePopup() {
      element.all(by.xpath("//div[@class='My Private vDiv']//label//span[1]")).getText().then(function (Data) {
          console.log(Data);
          //Data.sort();
          Data.split(' ');
      });
    }

While executing the code above I will get an output like below.

Actual result:

[ 'Dr Testing1 Hill (Testing)',
  'Dr Testing2 Hill (Testing)',
  'Dr Testing3 Hill (Testing)',
  'Mr Testing1 Hill (Testing)',
  'Mr Testing2 Hill (Testing)',
  'Mr Testing3 Hill (Testing Testing)',
  'Mr Testing Hill (Testing)',
  'Mr Testing Hill (Testing)',
  'Mr Testing Hill (Testing)',
  'Dr Testing Hill (Testing)' ]

I need to delete the first 3 characters of each value.

Expected result:

[ 'Testing1 Hill (Testing)',
  'Testing2 Hill (Testing)',
  'Testing3 Hill (Testing)',
  'Testing1 Hill (Testing)',
  'Testing2 Hill (Testing)',
  'Testing3 Hill (Testing Testing)',
  'Testing Hill (Testing)',
  'Testing Hill (Testing)',
  'Testing Hill (Testing)',
  'Testing Hill (Testing)' ]

The error which I got while running protractor testing is:

Failed: Data.split is not a function

I need to sort and split data.

Upvotes: 1

Views: 802

Answers (2)

Madhan Raj
Madhan Raj

Reputation: 1442

Try the below one. Here the Data is a string array.So we need to iterate all the values to

    comparePopup() {
          element.all(by.xpath("//div[@class='My Private vDiv']//label//span[1]")).getText().then(function (Data) {
              for(i=0;i<Data.length-1;i++){  //To iterate into the array
                 Data[i] = Data[i].subString(3);  //Now we get Testing1 Hill (Testing)
           }
          });
        }

Hope it helps you

Upvotes: 1

z11i
z11i

Reputation: 1296

element.all returns an ElementArrayFinder, which is basically an array. An array has no method split, which is what your error says.

Instead, try

comparePopup() {
      element.all(by.xpath("//div[@class='My Private vDiv']//label//span[1]")).map(function(elm) => {
              return elm.getText();
          })
          .then(function (texts) {
              // for each string in the array, split by whitespace,
              // discard the first word, and join the rest by whitespace
              // then sort the resultant array
              texts.map(v => v.split(' ').splice(1).join(' ')).sort();
      });
    }

which will output

[ "Testing Hill (Testing)",
  "Testing Hill (Testing)",
  "Testing Hill (Testing)",
  "Testing Hill (Testing)",
  "Testing1 Hill (Testing)",
  "Testing1 Hill (Testing)",
  "Testing2 Hill (Testing)",
  "Testing2 Hill (Testing)",
  "Testing3 Hill (Testing Testing)",
  "Testing3 Hill (Testing)" ]

Upvotes: 0

Related Questions