pomify
v0.8.6
Published
Playwright js framework for page objects and explicit locators
Downloads
20
Maintainers
Readme
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 pomify
Usage
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
Page
object. - 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, PlaywrightPage
object)$
(public, PlaywrightLocator
object)
- Constructor:
BaseLocator(page: Page, locator: Locator | string, options?: LocatorOptions)
page
: The PlaywrightPage
objectlocator
: A string selector or a PlaywrightLocator
objectoptions
(optional): Options for refining the locatorparent
: A string selector or a PlaywrightLocator
object 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
click
method)
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 (ifnextPageClass
is provided).
InputLocator (Extends BaseLocator)
- Methods:
click
: Clicks the input element (bound to the underlying locator'sclick
method)clear
: Clears the input element (bound to the underlying locator'sclear
method)fill
: Fills the input element with a value (bound to the underlying locator'sfill
method)getInputValue
: Retrieves the current value of the input element (bound to the underlying locator'sinputValue
method)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'scheck
method)uncheck
: Unchecks the checkbox (bound to the underlying locator'suncheck
method)isChecked
: Checks if the checkbox is currently checked (bound to the underlying locator'sisChecked
method)setChecked
: Sets the checkbox to a specific checked state (bound to the underlying locator'ssetChecked
method)getInputValue
: Retrieves the current value of the checkbox (bound to the underlying locator'sinputValue
method)
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'sinputValue
method) (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'sinnerText
method)