-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat: Rename SelectorPlayground API to ElementSelector API #31889
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
Merged
jennifer-shehane
merged 22 commits into
release/15.0.0
from
selector-playground-api-to-element-selector
Jun 26, 2025
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3e440ec
feat: Rename SelectorPriority API to ElementSelector API
jennifer-shehane 139874d
Merge branch 'release/15.0.0' into selector-playground-api-to-element…
jennifer-shehane e07db39
bump unique-selector + remove restriction on type of prop
jennifer-shehane 5b1eece
Merge branch 'selector-playground-api-to-element-selector' of https:/…
jennifer-shehane 54538a8
rename file + remove test
jennifer-shehane 2ce3309
rename and add types to files
jennifer-shehane 080d9d6
save file
jennifer-shehane 770af77
fix types
jennifer-shehane 6866162
Add error when Cypress.SelectorPlayground is called to suggest renaming
jennifer-shehane e3c103e
Add better description of ElementSelector API
jennifer-shehane c31f27d
Error on invalid selector priority types
jennifer-shehane f002750
changelog entry
jennifer-shehane 72e8260
Merge branch 'release/15.0.0' into selector-playground-api-to-element…
jennifer-shehane d792b66
Update types to match exact type of selectorPriority
jennifer-shehane ea5816d
remove semicolon
jennifer-shehane 3fa5bdb
Add feature description for separate feature
jennifer-shehane c909ecb
remove the other semicolon
jennifer-shehane 8d16f65
Update types to use official cypress types
jennifer-shehane 8330fa3
Add other issue that this PR resolves.
jennifer-shehane 54b71a7
add name to selector priority
jennifer-shehane bd5a7f9
alphabetize strings in selectorPriority types
jennifer-shehane d1c4948
Merge branch 'release/15.0.0' into selector-playground-api-to-element…
jennifer-shehane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
packages/driver/cypress/e2e/cypress/element_selector.cy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/// <reference types="cypress" /> | ||
import type { ElementSelectorAPI } from '../../../src/cypress/element_selector' | ||
import { DEFAULT_SELECTOR_PRIORITIES } from '../../../src/cypress/element_selector' | ||
const { $: $cypress } = Cypress.$Cypress | ||
const ElementSelector = Cypress.ElementSelector as ElementSelectorAPI | ||
|
||
const SELECTOR_DEFAULTS: Cypress.SelectorPriority[] = [...DEFAULT_SELECTOR_PRIORITIES] | ||
|
||
describe('src/cypress/element_selector', () => { | ||
beforeEach(() => { | ||
ElementSelector.reset() | ||
}) | ||
|
||
it('has defaults', () => { | ||
expect(ElementSelector.getSelectorPriority()).to.deep.eq(SELECTOR_DEFAULTS) | ||
}) | ||
|
||
context('.defaults', () => { | ||
it('is noop if not called with selectorPriority', () => { | ||
ElementSelector.defaults({}) | ||
expect(ElementSelector.getSelectorPriority()).to.deep.eq(SELECTOR_DEFAULTS) | ||
}) | ||
|
||
it('sets element:selector:priority if selectorPriority specified', () => { | ||
const selectorPriority: Cypress.SelectorPriority[] = [ | ||
'data-1', | ||
'data-2', | ||
'id', | ||
'class', | ||
'tag', | ||
'attributes', | ||
'nth-child', | ||
'name', | ||
'attribute:aria-label', | ||
'attribute:aria-labelledby', | ||
] | ||
|
||
ElementSelector.defaults({ | ||
selectorPriority, | ||
}) | ||
|
||
expect(ElementSelector.getSelectorPriority()).to.eql(selectorPriority) | ||
}) | ||
|
||
it('throws if not passed an object', () => { | ||
const fn = () => { | ||
ElementSelector.defaults(undefined as any) | ||
} | ||
|
||
expect(fn).to.throw() | ||
.with.property('message') | ||
.and.include('`Cypress.ElementSelector.defaults()` must be called with an object. You passed: ') | ||
|
||
expect(fn).to.throw() | ||
.with.property('docsUrl') | ||
.and.include('https://on.cypress.io/element-selector-api') | ||
}) | ||
|
||
it('throws if selectorPriority is not an array', () => { | ||
const fn = () => { | ||
ElementSelector.defaults({ selectorPriority: 'foo' as any }) | ||
} | ||
|
||
expect(fn).to.throw() | ||
.with.property('message') | ||
.and.include('`Cypress.ElementSelector.defaults()` called with invalid `selectorPriority` property. It must be an array. You passed: `foo`') | ||
|
||
expect(fn).to.throw() | ||
.with.property('docsUrl') | ||
.and.include('https://on.cypress.io/element-selector-api') | ||
}) | ||
|
||
it('throws if selectorPriority contains an unsupported priority', () => { | ||
const fn = () => { | ||
ElementSelector.defaults({ | ||
selectorPriority: [ | ||
'id', | ||
// @ts-expect-error - invalid priority | ||
'attr', | ||
], | ||
}) | ||
} | ||
|
||
expect(fn).to.throw() | ||
.with.property('message') | ||
.and.include('`Cypress.ElementSelector.defaults()` called with invalid `selectorPriority` property. It must be one of: `data-*`, `attribute:*`, `id`, `class`, `tag`, `name`,`attributes`, or `nth-child`. You passed: `attr`') | ||
}) | ||
|
||
it('throws if selectorPriority has an unsupported priority that contains a substring of a valid priority', () => { | ||
const fn = () => { | ||
ElementSelector.defaults({ | ||
selectorPriority: [ | ||
// @ts-expect-error - invalid priority | ||
'idIsNotValid', | ||
], | ||
}) | ||
} | ||
|
||
expect(fn).to.throw() | ||
.with.property('message') | ||
.and.include('`Cypress.ElementSelector.defaults()` called with invalid `selectorPriority` property. It must be one of: `data-*`, `attribute:*`, `id`, `class`, `tag`, `name`,`attributes`, or `nth-child`. You passed: `idIsNotValid`') | ||
}) | ||
}) | ||
|
||
context('.getSelector', () => { | ||
it('uses defaults.selectorPriority', () => { | ||
const $div = $cypress('<div data-cy=\'main button 123\' data-foo-bar-baz=\'quux\' data-test=\'qwerty\' data-foo=\'bar\' />') | ||
|
||
Cypress.$('body').append($div) | ||
|
||
expect(ElementSelector.getSelector($div)).to.eq('[data-cy="main button 123"]') | ||
|
||
ElementSelector.defaults({ | ||
selectorPriority: ['data-foo'], | ||
}) | ||
|
||
expect(ElementSelector.getSelector($div)).to.eq('[data-foo="bar"]') | ||
}) | ||
}) | ||
|
||
describe('Cypress.SelectorPlayground (renamed)', () => { | ||
it('throws error when calling defaults()', () => { | ||
const fn = () => { | ||
Cypress.SelectorPlayground.defaults({}) | ||
} | ||
|
||
expect(fn).to.throw() | ||
.with.property('message') | ||
.and.include('`Cypress.SelectorPlayground.defaults()` has been renamed to `Cypress.ElementSelector.defaults()`') | ||
|
||
expect(fn).to.throw() | ||
.with.property('message') | ||
.and.include('Please update your code to use `Cypress.ElementSelector` instead') | ||
}) | ||
|
||
it('throws error when calling getSelector()', () => { | ||
const fn = () => { | ||
Cypress.SelectorPlayground.getSelector($cypress('body')) | ||
} | ||
|
||
expect(fn).to.throw() | ||
.with.property('message') | ||
.and.include('`Cypress.SelectorPlayground.getSelector()` has been renamed to `Cypress.ElementSelector.getSelector()`') | ||
|
||
expect(fn).to.throw() | ||
.with.property('message') | ||
.and.include('Please update your code to use `Cypress.ElementSelector` instead') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converted to TypeScript - the tests are the same except for removing tests around
onElement