1

I am using protractor for my E2E testcases. In my case, I am having an array and I want to sort it

Ex.

Arraylist.push(elements[1].getText());

Arraylist.push(elements[2].getText());

Arraylist.push(elements[0].getText());

element[n] is returning a text value which is getting inserted into the ArrayList. Now I want to Sort this Array and test that the elements are in the correct order.

Do someone has idea, How can we do that?

3
  • Can you clarify why you would like to print the sorted values? My understanding of E2E tests using Protractor is that the output are is successful expectations. Commented Apr 11, 2014 at 20:57
  • My testcase is to compare that whenever user click on header of an list, the items under it should be sorted. Therefore I am inserting each element in an array so that I can sort that array too and compare with the sorted list on UI. Commented Apr 14, 2014 at 6:31
  • I've edited the question so your requirements are clearer. Feel free to amend if I've misunderstood. Commented Apr 14, 2014 at 6:53

2 Answers 2

2

To do this, you should be aware that all the elements and return values of getText() are promises. Therefore sorting the array, and comparing the sorted with the unsorted, has to be done with the resolved results of these promises.

var unsorted = elements.map(function(element) {
  return element.getText();
});

var sorted = unsorted.then(function(texts) {
  // Make sure to copy the array to not sort the original
  return texts.slice(0).sort();
});

var equals = protractor.promise.all([unsorted, sorted]).then(function(texts) {
  var unsorted = texts[0];
  var sorted = texts[1];
  var allEqual = (unsorted.length === sorted.length);
  sorted.forEach(function(sortedItem, i) {
    allEqual = allEqual && (sortedItem === unsorted[i]);
  });
  return allEqual;
});

expect(equals).toBe(true);
Sign up to request clarification or add additional context in comments.

Comments

0

not sure what the connection to E2E testing or protractor is, but well ... Arraylist.sort() or use lodash/underscorejs _.sortBy(Arraylist, sortingValue)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.