Skip to content

Commit

Permalink
Merge pull request #18573 from cypress-io/fix-7745-validate-selector-…
Browse files Browse the repository at this point in the history
…priority-config

fix: validate selectorPriority configuration
  • Loading branch information
mjhenkes authored Oct 21, 2021
2 parents 94a3261 + 1ed4e57 commit 5f94cad
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,50 @@ describe('src/cypress/selector_playground', () => {
})

it('sets selector:playground:priority if selectorPriority specified', () => {
const selectorPriority = [
'data-1',
'data-2',
'id',
'class',
'tag',
'attributes',
'nth-child',
]

SelectorPlayground.defaults({
selectorPriority: ['foo'],
selectorPriority,
})

expect(SelectorPlayground.getSelectorPriority()).to.eql(['foo'])
expect(SelectorPlayground.getSelectorPriority()).to.eql(selectorPriority)
})

it('throws if selectorPriority contains an unsupported priority', () => {
const fn = () => {
SelectorPlayground.defaults({
selectorPriority: [
'id',
'name',
],
})
}

expect(fn).to.throw()
.with.property('message')
.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`')
})

it('throws if selectorPriority has an unsupported priority that contains a substring of a valid priority', () => {
const fn = () => {
SelectorPlayground.defaults({
selectorPriority: [
'idIsNotValid',
],
})
}

expect(fn).to.throw()
.with.property('message')
.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`')
})

it('sets selector:playground:on:element if onElement specified', () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/driver/src/cypress/error_messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1428,10 +1428,14 @@ export default {
message: '`Cypress.SelectorPlayground.defaults()` must be called with an object. You passed: `{{arg}}`',
docsUrl: 'https://on.cypress.io/selector-playground-api',
},
defaults_invalid_priority: {
defaults_invalid_priority_type: {
message: '`Cypress.SelectorPlayground.defaults()` called with invalid `selectorPriority` property. It must be an array. You passed: `{{arg}}`',
docsUrl: 'https://on.cypress.io/selector-playground-api',
},
defaults_invalid_priority: {
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.',
docsUrl: 'https://on.cypress.io/selector-playground-api',
},
defaults_invalid_on_element: {
message: '`Cypress.SelectorPlayground.defaults()` called with invalid `onElement` property. It must be a function. You passed: `{{arg}}`',
docsUrl: 'https://on.cypress.io/selector-playground-api',
Expand Down
21 changes: 15 additions & 6 deletions packages/driver/src/cypress/selector_playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,25 @@ export default {
})
}

const { selectorPriority: priority, onElement } = props
const { selectorPriority, onElement } = props

if (priority) {
if (!_.isArray(priority)) {
$errUtils.throwErrByPath('selector_playground.defaults_invalid_priority', {
args: { arg: $utils.stringify(priority) },
if (selectorPriority) {
if (!_.isArray(selectorPriority)) {
$errUtils.throwErrByPath('selector_playground.defaults_invalid_priority_type', {
args: { arg: $utils.stringify(selectorPriority) },
})
}
// Validate that the priority is one of: "data-*", "id", "class", "tag", "attributes", "nth-child"

selectorPriority.forEach((priority) => {
if (!/^(data\-.*|id|class|tag|attributes|nth\-child)$/.test(priority)) {
$errUtils.throwErrByPath('selector_playground.defaults_invalid_priority', {
args: { arg: priority },
})
}
})

defaults.selectorPriority = priority
defaults.selectorPriority = selectorPriority
}

if (onElement) {
Expand Down

4 comments on commit 5f94cad

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5f94cad Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.7.0/circle-develop-5f94cad3cb4126e0567290b957050c33e3a78e3c/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5f94cad Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.7.0/appveyor-develop-5f94cad3cb4126e0567290b957050c33e3a78e3c/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5f94cad Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.7.0/appveyor-develop-5f94cad3cb4126e0567290b957050c33e3a78e3c/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5f94cad Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.7.0/circle-develop-5f94cad3cb4126e0567290b957050c33e3a78e3c/cypress.tgz

Please sign in to comment.