0

There are many tutorials but just can't figure it out... So i am practicing on https://www.saucedemo.com/inventory.html and trying to get the list of elements, and compare it to a list, to see that it is ok. Will do the same after sorting Z to A, and same with price. But i am quite a noob and beginner and really struggling. Until now i managed to get the list and reverse it (?)

const array1 = $$('.inventory_item_name').forEach(element => {
            console.log(element.getText())
        });
const array2 = $$('.inventory_item_name').reverse().forEach(element => {
            console.log(element.getText())
        });

If i try to compare these with === it will be true :/ i am trying it in VScode -Js - webdriverIO - Mocha

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 1
    Can you include the HTML for a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). The provided link is hidden behind a login screen – Reyno Nov 29 '21 at 11:07
  • Sorry, the login is written udner the login fields, if you just scroll down: standard_user pass: secret_sauce – Francisc Lukacs Nov 29 '21 at 11:58
  • You can't compare arrays using `===` sadly, because it checks refers, not a values ([The Strict Equality Comparison Algorithm](https://262.ecma-international.org/5.1/#sec-11.9.6)). You can find how to compare arrays [here](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript). – Xeelley Nov 29 '21 at 13:07
  • sadly this not help me. Or too advanced, and do not understand. – Francisc Lukacs Nov 29 '21 at 15:06
  • If you want add an answer, please post it as an answer instead of editing into the question – Suraj Rao Nov 30 '21 at 07:22

1 Answers1

0

Found a great tutorial on udemy

const itemNameLocators = $$('.inventory_item_name'); //gets the item names
const originalItemNameText = itemNameLocators.map(item => item.getText()); //maps the item names and covnerts to text (?)
console.log(originalItemNameText) //prints the value- item names- in an array
const itemNameCopy = originalItemNameText.slice() //creates a copy of the item names - not to change the original array
const sortedItemNames = itemNameCopy.reverse() //sorts the copied list - to match the websites copy
console.log(sortedItemNames) //logs it - for personal use only
expectChai(originalItemNameText).to.eql(sortedItemNames) //compares the 2 array texts

where I added the .reverse(), in this case the test will fail. without the .Slice it was passing whatever the values were and in what order. there it will be .sort(), and in the test case where I will sort on the page itself, I will change it accordingly. The same thing can be done on the price.

Of course, there was an easier way, just to check the first item name = x and when sorting check the name again to contain the last item, but that was too basic.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103