0.8.6 • Published 1 year ago
pomify v0.8.6
pomify
Introduction
pomify is a TypeScript library that simplifies creating and maintaining tests using the Playwright testing framework and the Page Object Pattern. It provides a clean structure for organizing UI element locators and interactions, enhancing test readability and maintainability.
Installation
npm install pomifyUsage
pomify utilizes a base class (BasePage) to define core functionalities for page objects. You extend this class to create specific page objects for different pages in your application.
Defining Page Objects
- Create a class extending
BasePage. - Implement the abstract
$property by assigning specific locators for your page elements. Utilize methods fromis(e.g.,this.is.Button('#login')) to create element locators.
Example:
// login.page.ts
import { BasePage } from 'pomify';
export class LoginPage extends BasePage {
$ = {
usernameInput: this.is.Input('#username'),
passwordInput: this.is.Input('#password'),
loginBtn: this.is.Button('#login'),
errorMsgDiv: this.is.Typography('#alert')
}
async goto(){
//...
}
async login(username: string, password: string) {
await this.$.usernameInput.fill(username);
await this.$.passwordInput.fill(password);
await this.$.loginBtn.click();
}
}Using Page Objects
- Create an instance of your page object class with a Playwright
Pageobject. - Access element locators and interact with them using methods provided by the page object or the underlying locators (e.g.,
await loginPage.$.username.fill('test')).
Example:
// login.spec.ts
import { expect, test } from '@playwright/test'
import LoginPage from '../pages/login/login.page';
test.describe('LOGIN TESTS', () => {
test('login and go to the home page', async ({ page }) => {
const login = new LoginPage(page);
await login.goto()
await login.login()
await page.waitForURL('home')
expect(page.url()).toContain('home')
})
test('error message please insert password', async ({ page }) => {
const login = new LoginPage(page)
await login.goto()
await login.$.usernameInput.fill('Moshe');
await login.$.loginBtn.click()
expect(await login.$.errorMsgDiv.innerText()).toContain('Please insert password')
})
})API Reference
BaseLocator
- Properties:
page(protected, PlaywrightPageobject)$(public, PlaywrightLocatorobject)
- Constructor:
BaseLocator(page: Page, locator: Locator | string, options?: LocatorOptions)page: The PlaywrightPageobjectlocator: A string selector or a PlaywrightLocatorobjectoptions(optional): Options for refining the locatorparent: A string selector or a PlaywrightLocatorobject representing the parent elementposition: String ('first', 'last') or number (nth) to filter by position
ButtonLocator (Extends BaseLocator)
- click: Clicks the button (bound to the underlying locator's
clickmethod)
LinkLocator (Extends ButtonLocator)
- Properties:
nextPage: Optional class for creating the next page object when the link is clicked.toUrl: Optional URL to wait for after clicking the link (as string or RegExp).
- Methods:
navigate: Clicks the link, waits for the target URL (if provided) and creates a new page object (ifnextPageClassis provided).
InputLocator (Extends BaseLocator)
- Methods:
click: Clicks the input element (bound to the underlying locator'sclickmethod)clear: Clears the input element (bound to the underlying locator'sclearmethod)fill: Fills the input element with a value (bound to the underlying locator'sfillmethod)getInputValue: Retrieves the current value of the input element (bound to the underlying locator'sinputValuemethod)specialFill: Fills the input element with additional options (clear, click, multipleValueBy, force, timeout, noWaitAfter)
CheckboxLocator (Extends BaseLocator)
- Methods:
check: Checks the checkbox (bound to the underlying locator'scheckmethod)uncheck: Unchecks the checkbox (bound to the underlying locator'suncheckmethod)isChecked: Checks if the checkbox is currently checked (bound to the underlying locator'sisCheckedmethod)setChecked: Sets the checkbox to a specific checked state (bound to the underlying locator'ssetCheckedmethod)getInputValue: Retrieves the current value of the checkbox (bound to the underlying locator'sinputValuemethod)
RadioLocator (Extends BaseLocator)
- Properties:
values: Array containing possible radio button values (defined in the constructor).
- Methods:
getInputValue: Retrieves the current value of the radio button (bound to the underlying locator'sinputValuemethod) (under development)getRadioByValue: Finds a radio button locator based on its value.whichIsChecked: Returns the checked radio button locator (if any).check: Checks a radio button by its value.
TypographyLocator
- Methods:
innerText: Retrieves the inner text of the element (bound to the underlying locator'sinnerTextmethod)