Rayrun
← Back to Discord Forum

Hello everyone!

malcolm_99273posted in #help-playwright
Open in Discord

I was wondering if anyone can give me a good hint of which is the best way to approach the following test, because I'm finding the logic a little tricky

I've got: index[0] with text "A" index[1] with text "B" index[2] with text "C" index[3] with text "D" index[4] with text "E" index[5] with text "F"

Logic: If Text "A" missing, then text "B" must become index[0], and the other "C","D","E", etc should follow the same rool.

As a matter of speaking, the order must never break, so what would make the test fail? I've got "A"index[0], "B"index[1], "D"index[2], "E"index[3], "F"index[4], (Notice that "C" is missing which was index[2] before). IF "C" is replaced by any other letter other than "D" the. test should fail

I need to script out a function/method, that can verify that if for some reason the array of letters (actual result) becomes unordered (example: "E","A","B","C","F","D") the test must fail.

Any good expert out there who can give me a hand with this??? 🙂

This thread is trying to answer question "How can I write a test to verify that an array of letters is in the correct order and contains no missing elements?"

2 replies

If I understand your question correctly, then you could sort a copy of the original array, then assert that the before and after values were equal. If you also want to check for missing/added elements, then you could assert the updated array length was the same as the original array. For example:

test('AtoF', () => {
  const origArray = ['A','B','C','D','E','F']; // pass
  // const origArray = ['A','B','D','C','E','F']; // fail
  const copiedArray = [...origArray];
  copiedArray.sort();
  // origArray.pop(); // causes test to fail
  expect.soft(copiedArray).toHaveLength(origArray.length);
  expect.soft(copiedArray).toEqual(origArray);
});
malcolm_99273

Yeah! This approach is basically what I had @brk9394 , thanks! I managed to get the expected array not to be hardcoded just in case. Thanks a lot!

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.