-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Add options to customize name
and displayName
and message
of some builtin commands
#15438
Comments
A hackish approach I found to work for
I'm not sure whether it would be better to add the name and message to My current approach looks something like this: export function getByTestId<E extends HTMLElement>(
testId: string,
options?: Partial<Existable & Cypress.Loggable & Cypress.Timeoutable & Cypress.Withinable & Cypress.Shadow>
): Cypress.Chainable<JQuery<E>> {
const selector = `[data-testid=${testId}]`
const userOptions = options || {}
let mustExist: boolean = userOptions.exists ?? true
delete userOptions.exists
options = Cypress._.defaults({}, userOptions, {
exists: true,
log: true,
timeout: Cypress.config('defaultCommandTimeout'),
})
const consoleProps = {
Selector: selector,
Yielded: undefined,
Elements: undefined,
}
const logConf: Partial<Cypress.LogConfig> = {
name: 'Get Element by Data-TestID',
displayName: 'get(testid)',
message: testId,
timeout: options.timeout,
//consoleProps: () => consoleProps
// @ts-ignore (undocumented attribute)
type: 'parent', // no '-' in command log
// @ts-ignore (undocumented attribute)
autoEnd: false,
}
const log = options?.log ? Cypress.log(logConf) : null
options['_log'] = log
return cy
.get<E>(selector, { ...options })
.should((currentSubject: JQuery<E>) => {
log?.set('$el', currentSubject)
Cypress._.extend(logConf, { $el: currentSubject })
Cypress._.extend(consoleProps, {
Selector: selector,
Yielded: currentSubject ? Cypress.dom.getElements(currentSubject) : '--nothing--',
Elements: currentSubject ? currentSubject.length : undefined,
})
if (mustExist) {
expect(currentSubject).to.exist
} else {
expect(currentSubject).to.not.exist
}
log?.snapshot()
log?.end()
})
} |
In my own uses I ended up just switching back to native For reference, I ended up making a far simpler helper method:
Used as follows:
Cypress command log comparison, showing how quotes help:
(I also tried using |
Since this hasn’t gotten a lot of interest over the years, we’ll close this issue. |
What would you like?
A way to customize the
name
anddisplayName
andmessage
attributes of some built-in commands, such ascy.get
and.find
.Why is this needed?
This can help unclutter the command log, by moving the "key" of what it's looking for from the message to the displayName.
This could also be useful to partially address another feature request, by giving a way to tidy up the logging without disabling it entirely: #8296
I've tried doing it with a custom command, but custom commands that wrap
cy
commands aren't able to fully replicate the logging:$el
, especially in cases where you expect it not to exist.then
only happens after element exists, and.should
breaks default assertions).onFail
: it would be block-scoped, and would allow logging, but not allow canceling the failure)verifyUpcomingAssertions
on the return value of.get
(because that mixes synchronous and asynchronous code)verifyUpcomingAssertions
in a.should
block (because it causes an infinite loop)As an experiment, I managed to get something like what I want by copying the code of the
.get()
method, but that's too messy, and all the shadowDom stuff seemed not to be publicly exposed.The text was updated successfully, but these errors were encountered: