Lesson:Locators - getting multiple elements

Ok so we have covered how to get a single element. But the tests so far would always fail if there were more than one matching element.

In this lesson we will explain how to query for and make assertions against multiple matching elements.

For example, if you had this component

const Component = () => {
  return (
    <div>
      <button>Save</button>
      <button>Cancel</button>
    </div>
  );
};

And you used expect(screen.getByRole('button')).toBeInTheDocument(), it would fail.

So let's go through some ways that we can assert that there are two buttons, or how to check the buttons have different texts.

Loading full lesson...