I have the following page object model representing a widget in my app
/**
* Contains common actions for all widgets
*/
export default abstract class AbstractWidget {
private widgetId: number;
elements = {
widget: () => cy.getDataTestId(`widgetbox-container-${this.widgetId}`),
};
constructor(widgetId: number) {
this.widgetId = widgetId;
}
}
And I have a concrete class extending this class where I would like to append extra elements specific to this widget to the elements property
import AbstractWidget from './AbstractWidget';
export default class SpecificWidget extends AbstractWidget {
elements = {
...super.elements,
assetSearch: () => cy.getDataTestId('assetSearch'),
};
constructor(widgetId: number) {
super(widgetId);
}
}
however, when I try to spread the elements from the abstract super class
elements = {
...super.elements,
assetSearch: () => cy.getDataTestId('assetSearch'),
};
it results in the typescript error
TS2340: Only public and protected methods of the base class are accessible via the super keyword.
What am I doing wrong?
This seems to be a result of this issue
Accessing property on super should be a type error #35314 Nov 24, 2019
If you follow the TS Playground link, it opens with the latest TS version and
console.log(super.n)shows an error. If you change the version to3.7.5the error goes away.But if you look at the Handbook Overriding Methods, the equivalent access for methods is permitted, so the above change seems to have created an anomaly
Specifically in Cypress you can either use
this.elementsinstead ofsuper.elements, since the derived class inherits the base class element property:Testing (with string return values instead of Chainers)
Or you can create a "helper method" in the base class
An additional note, returning Chainers like this
can result in unexpected problems if the returned Chainer gets invalidated due to page changes.
See Variables and aliases - Return values
It's ok if you intend to chain immediately with no page actions between calling
widgit()and using the result, but that means anyone using the class has to remember that caveat.Cypress made a change to aliases to defaullt to
type: queryspecifically for this problem.If an alias becomes stale when used, the chainer is re-run to get a fresh copy of the query result. But this won't happen automatically with your page-object methods.