Giter Club home page Giter Club logo

playwright-factory's Introduction

Playwright Factory

Page Objects for Playwright Java made easy!

Is a Java library that helps you create clean and readable Page Objects. Inspired by Selenium's PageFactory, solution allows you to create locators easily by annotating fields.

Installation

<dependency>
    <groupId>io.github.adv4nt4ge</groupId>
    <artifactId>playwright-factory</artifactId>
    <version>1.2.1</version>
</dependency>

Getting Started

To define Locators, create a Field and annotate it with @FindBy:

public class HomePage {

    @FindBy(locator = "#some-id")
    public Locator header;

    @FindBy(testId = "some-id")
    public Locator myButton;

    @FindBy(placeholder = "#some-id")
    public Locator myPlaceholder;

    @FindBy(altText = "#some-id")
    public Locator myAltText;

    @FindBy(text = "#some-id")
    public Locator myText;

    @FindBy(label = "#some-id")
    public Locator myLabel;

    @FindBy(title = "#some-id")
    public Locator myTitle;    
    
    @FindBy(text = "#some-id")
    public List<ElementHandle> myListText;

}

@FindBy(locator = "") accepts any Playwright Other Locators.

To use your Page Object in your tests use the PageFactory to create an instance of your page and pass it your page class and an instance of Playwright's Page:

    HomePage homePage=PageFactory.create(HomePage.class,page)

The PageFactory can create an instance of any page that has a default constructor or a constructor with just a Playwright Page parameter.

public void initElementsPageWithFieldDecorator() {
        HomePage homePage = new HomePage();
        PageFactory.initElements(homePage, new ElementFieldDecorator(page));
}

Implementing Page Factory

For an example of creating a test with Page Factory in your project

Dependent Locators

At times, you may want to find a locator that is under another locator. The way to do this in Playwright would be: page.locator("#parent").locator(".child"). To define this you can use the @Parent annotation:

public class HomePage {

    @FindBy(testId = "some-id")
    public Locator parentLocator;

    @Under("parentLocator")
    @FindBy(locator = "#some-id")
    public Locator childLocator;

    @Parent("parentLocator")
    @FindBy(placeholder = "#some-id")
    public Locator anotherChild;
}

The value you pass to the @Parent annotation should be the name of the parent Locator. The child Locator must be public.

Iframes

The solution allows you to quickly locate elements inside an Iframe by passing in a selector to find the Iframe:

@Frame(frame = "#preview-iframe")
public class PageIframe {
    
    @FindBy(testId = "some-id")
    public Locator someButton;
}

Any Locators defined inside a class that is decorated with @Frame(frame = "X" will be scoped to that Iframe.

The above example is equivalent to Playwright's page.frameLocator("#preview-iframe").getByTestId("some-id").

Requirements

Solution requires Java 8+ and Playwright 1.30.0+.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.