Skip to content

Commit 5f94cad

Browse files
authored
Merge pull request #18573 from cypress-io/fix-7745-validate-selector-priority-config
fix: validate selectorPriority configuration
2 parents 94a3261 + 1ed4e57 commit 5f94cad

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

packages/driver/cypress/integration/cypress/selector_playground_spec.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,50 @@ describe('src/cypress/selector_playground', () => {
2323
})
2424

2525
it('sets selector:playground:priority if selectorPriority specified', () => {
26+
const selectorPriority = [
27+
'data-1',
28+
'data-2',
29+
'id',
30+
'class',
31+
'tag',
32+
'attributes',
33+
'nth-child',
34+
]
35+
2636
SelectorPlayground.defaults({
27-
selectorPriority: ['foo'],
37+
selectorPriority,
2838
})
2939

30-
expect(SelectorPlayground.getSelectorPriority()).to.eql(['foo'])
40+
expect(SelectorPlayground.getSelectorPriority()).to.eql(selectorPriority)
41+
})
42+
43+
it('throws if selectorPriority contains an unsupported priority', () => {
44+
const fn = () => {
45+
SelectorPlayground.defaults({
46+
selectorPriority: [
47+
'id',
48+
'name',
49+
],
50+
})
51+
}
52+
53+
expect(fn).to.throw()
54+
.with.property('message')
55+
.and.include('`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be one of: `data-*`, `id`, `class`, `tag`, `attributes`, `nth-child`. You passed: `name`')
56+
})
57+
58+
it('throws if selectorPriority has an unsupported priority that contains a substring of a valid priority', () => {
59+
const fn = () => {
60+
SelectorPlayground.defaults({
61+
selectorPriority: [
62+
'idIsNotValid',
63+
],
64+
})
65+
}
66+
67+
expect(fn).to.throw()
68+
.with.property('message')
69+
.and.include('`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be one of: `data-*`, `id`, `class`, `tag`, `attributes`, `nth-child`. You passed: `idIsNotValid`')
3170
})
3271

3372
it('sets selector:playground:on:element if onElement specified', () => {

packages/driver/src/cypress/error_messages.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1428,10 +1428,14 @@ export default {
14281428
message: '`Cypress.SelectorPlayground.defaults()` must be called with an object. You passed: `{{arg}}`',
14291429
docsUrl: 'https://on.cypress.io/selector-playground-api',
14301430
},
1431-
defaults_invalid_priority: {
1431+
defaults_invalid_priority_type: {
14321432
message: '`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be an array. You passed: `{{arg}}`',
14331433
docsUrl: 'https://on.cypress.io/selector-playground-api',
14341434
},
1435+
defaults_invalid_priority: {
1436+
message: '`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be one of: `data-*`, `id`, `class`, `tag`, `attributes`, `nth-child`. You passed: `{{arg}}`. Consider using the `onElement` property if a specific selector is desired.',
1437+
docsUrl: 'https://on.cypress.io/selector-playground-api',
1438+
},
14351439
defaults_invalid_on_element: {
14361440
message: '`Cypress.SelectorPlayground.defaults()` called with invalid `onElement` property. It must be a function. You passed: `{{arg}}`',
14371441
docsUrl: 'https://on.cypress.io/selector-playground-api',

packages/driver/src/cypress/selector_playground.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,25 @@ export default {
5858
})
5959
}
6060

61-
const { selectorPriority: priority, onElement } = props
61+
const { selectorPriority, onElement } = props
6262

63-
if (priority) {
64-
if (!_.isArray(priority)) {
65-
$errUtils.throwErrByPath('selector_playground.defaults_invalid_priority', {
66-
args: { arg: $utils.stringify(priority) },
63+
if (selectorPriority) {
64+
if (!_.isArray(selectorPriority)) {
65+
$errUtils.throwErrByPath('selector_playground.defaults_invalid_priority_type', {
66+
args: { arg: $utils.stringify(selectorPriority) },
6767
})
6868
}
69+
// Validate that the priority is one of: "data-*", "id", "class", "tag", "attributes", "nth-child"
70+
71+
selectorPriority.forEach((priority) => {
72+
if (!/^(data\-.*|id|class|tag|attributes|nth\-child)$/.test(priority)) {
73+
$errUtils.throwErrByPath('selector_playground.defaults_invalid_priority', {
74+
args: { arg: priority },
75+
})
76+
}
77+
})
6978

70-
defaults.selectorPriority = priority
79+
defaults.selectorPriority = selectorPriority
7180
}
7281

7382
if (onElement) {

0 commit comments

Comments
 (0)