0

I wanted to unlike all liked tweets on Twitter. Since react is developed using tweeter I wanted to do that thing with code. I found the beloved code in Github which we should run on the developer console.

setInterval(() => {
  for (const d of document.querySelectorAll('div[data-testid="unlike"]')) {
    d.click()
  }
  window.scrollTo(0, document.body.scrollHeight)
}, 1000)

so this code will run once in every 1000 ms and I will scroll by itself. But I wanted to know the use of data-tested here why he didn't use normal class or id to select tweet like button? What is the difference between data-testid and id? this is the image of the tweet unlike component

this is the image of the tweet unlike component

  • They're just two different attributes, `
    ...
    `. `id`s have to be unique for the page to be valid and can be selected by the specific selector syntax `#hello`. Test IDs can repeat and are selected as shown, using the general attribute selector syntax.
    – jonrsharpe Nov 19 '20 at 16:02

1 Answers1

3

The data-testId attribute is supposed to be used as a handle for test code. Its a marker to say "don't change this class as tests rely on it".

If this is not test code above then it's likely sloppy programming practise i.e. a mistake. TestId's should not be used in the actual running code imo.

jsdeveloper
  • 3,945
  • 1
  • 15
  • 14