Skip to content

Instantly share code, notes, and snippets.

@marcelmokos
Last active July 20, 2017 21:09
Show Gist options
  • Save marcelmokos/fed9364a33fcf5fd7def24e351fceba2 to your computer and use it in GitHub Desktop.
Save marcelmokos/fed9364a33fcf5fd7def24e351fceba2 to your computer and use it in GitHub Desktop.
Example of Page object pattern base class
/**
* Base ui component class that other components should inherit from.
*/
export default class UIComponent {
/**
* This class property enables use of specific functions 'isDisplayed' and 'waitUntilDisplayed'
* @type {ElementFinder}
*/
selector = undefined;
constructor(selector = undefined) {
this.selector = selector;
}
checkSelectorExist = () => {
if (this.selector === undefined) {
throw new TypeError(
`Class '${this.constructor.name}' ` +
"extends 'UIComponent' possibly 'Page' Object Class and have to implement abstract property 'selector' " +
"when 'isDisplayed' or 'waitUntilDisplayed' are used",
);
}
};
/**
* @returns Function which resolves to boolean
*/
isDisplayed = () => {
this.checkSelectorExist();
return ExpectedConditions.visibilityOf(this.selector)();
};
waitUntilDisplayedTimeout = 5000;
/**
* Wait until this page is displayed.
*/
waitUntilDisplayed = () => {
this.checkSelectorExist();
browser.wait(
() => this.isDisplayed(),
this.waitUntilDisplayedTimeout,
`Failed while waiting for "${this.selector.locator()}" of Page Object Class '${this
.constructor.name}' to display.`,
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment