cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.03k stars 3.19k forks source link

CSS Selector Playground - allow configuration? #1135

Closed zth closed 6 years ago

zth commented 6 years ago

Hi!

This is a follow up to the launch of the CSS Selector Playground feature, and in reference to this comment: https://github.com/cypress-io/cypress/issues/917#issuecomment-352225763.

A use case I have is the following: We always set a unique data-testid on all of our elements used in E2E tests. It'd be really cool if the CSS Selector Playground was configurable so that it would prefer (or even enforce) selectors using the data-testid-convention. In addition to that, it'd be cool if it could also somehow highlight all elements with data-testid's on the page right now.

Any thoughts/comments?

brian-mann commented 6 years ago

@chrisbreiding let's go with the comment here: https://github.com/cypress-io/cypress/issues/1212#issuecomment-362113419

data-test and data-cy.

We need to support those as the preferred selector.

We also need to provide a callback function to enable you to control what selector you want returned... something like this..

// something like this
Cypress.SelectorPlayground.defaults({
  selectorPriority: ['data-cy', 'data-test', 'id', 'class', 'nth-child'],
  onElement: ($el) => {
    // return something here which will override the default attributes we search for
  })
})
brian-mann commented 6 years ago

After doing this, we need to provide docs for the API, we need to write a guide (to show you how to use the playground) and likely a best practice for naming your elements.

brian-mann commented 6 years ago

Released in 2.1.0.