-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Bug: Modifying the Cypress.SelectorPlayground.defaults - selectorPriority
breaks the Selector Playground
#7745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I can verify that overwriting the This seems to have always been an issue? Back to 3.4.1 anyway.
// this is default selector priority
Cypress.SelectorPlayground.defaults({
selectorPriority: [
"data-cy",
"data-test",
"data-testid",
"id",
"class",
"name",
"tag",
"attributes",
"nth-child",
]
})
it('Selector Playground', () => {
cy.visit('https://example.cypress.io')
}) Without overwriting defaultsWith overwriting defaults |
I am facing the same issue. Will this be resolved? |
Today I hit the same issue. Is there a planned date in which this might be fixed? |
I found a workaround, instead of using So instead of: Cypress.SelectorPlayground.defaults({
selectorPriority: [
"my-attribute-1",
"my-attribute-2",
...
]
}) Use this: Cypress.SelectorPlayground.defaults({
onElement: ($el) => {
const attr1 = $el.attr("my-attribute-1");
if (attr1) {
return `[my-attribute-1="${attr1}"]`;
}
const attr2 = $el.attr("my-attribute-2");
if (attr2) {
return `[my-attribute-2="${attr2}"]`;
}
// Default behaviour
return false;
},
}); |
CauseThe selector playground breaks in this instance because the Cypress.SelectorPlayground.defaults({
selectorPriority: [
"data-qa",
"data-test",
"data-cy",
"id",
"class",
"name", // <-This priority is causing the issue.
"tag",
"attributes",
"nth-child",
],
}); ExplanationWe use a fork of the unique-selector package to generate the selectors for the playground. The unique-selector package only supports the following 'priorities':
Adding the ResolutionWe can modify Cypress to validate that the selectPriority configuration option is only set with the accepted keys. This resolution would prevent users from putting cypress in a bad state. If unique-selector falls back to the attributes priority to generate the selector, it will choose the first unique attribute. If a user wishes to create a selector that prefers one attribute over another they can (as the above workaround suggests) use the ExampleDOM <div id="item1" name="div"> example </div>
<div id="item2" name="Fred"> Fred Example </div>
<div id="item3" name="Fred"> Second Fred Example </div> Config Cypress.SelectorPlayground.defaults({
onElement: ($el) => {
const attr1 = $el.attr("name");
if (attr1) {
return `[name="${attr1}"]`;
}
const attr2 = $el.attr("title");
if (attr2) {
return `[title="${attr2}"]`;
}
return undefined;
},
});
Cypress.SelectorPlayground.defaults({
selectorPriority: [
"id",
"attributes",
],
}); Selector
For the first example you might expect the selector to be For the second and third example, the id is used because the |
The code for this is done in cypress-io/cypress#18573, but has yet to be released. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Uh oh!
There was an error while loading. Please reload this page.
Current behavior:
Modifying the
Cypress.SelectorPlayground.defaults
in thesupport/index.js
file breaks the Selector Playground in the GUI.Desired behavior:
To be able to modify/add to the
selectorPriority
list and still use the Selector Playground.Test code to reproduce
If you modify the
cypress/support/index.js
file with this:The selector playground is broken and doesn't work at all.
When I comment this block of code out, the Selector Playground works normally.
This issue helped me figure out where to modify the selectorPriority:
7606
Versions
Cypress 4.7.0 and 4.8.0, MacOS, Chrome
The text was updated successfully, but these errors were encountered: