I am trying to add a custom command to my Cypress framework to check if elements are enabled/disabled.
Here is my attempted custom command in commands.js
:
Cypress.Commands.add('isEnabled', (element) => {
element.invoke('attr', 'class').then(classAttribute => {
if (classAttribute.includes('mat-button-disabled')) {
// do something
}
});
});
And I am trying to call this command through my step defintion here:
cy.isEnabled(widgets.getPaginatorLast());
And here is my page object model:
getPaginatorLast () {
return cy.get('.mat-paginator-navigation-last');
}
When I run this code, I get the following error:
Timed out retrying after 20000ms: cy.invoke() errored because your subject is: null. You cannot invoke any functions such as attr on a null value.
If you expect your subject to be null, then add an assertion such as:
cy.wrap(null).should('be.null')
If I update the isEnabled()
command to directly use the element like below, rather than pass in the element via my step defintion, then the class attribute is being picked up:
Cypress.Commands.add('isEnabled', (element) => {
widgets.getPaginatorLast().invoke('attr', 'class').then(classAttribute => {
if (classAttribute.includes('mat-button-disabled')) {
// do something
}
});
});
The only change I made is replace element
with widgets.getPaginatorLast()
.
I don't know why I'm getting the null error though because that's the value I'm passing in.
Can someone please tell me why this is happening, & what changes are required to fix it?