Java-Playwright

Java Playwright Test Automation Project Structure

Here’s a well-structured mockup design for a UI and API automation framework leveraging Java, Playwright, and Page Object Model (POM) with Page Factory. The project uses Maven for dependency management and GitHub for CI/CD (nightly builds and builds triggered by each push). The structure includes examples and mockup code.


Project Structure

project-root/
│
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   ├── com/
│   │   │   │   ├── example/
│   │   │   │   │   ├── pages/                 # Page classes for UI automation
│   │   │   │   │   │   ├── LoginPage.java
│   │   │   │   │   │   ├── HomePage.java
│   │   │   │   │   │   ├── ApiUtils.java      # API utility methods
│   │   │   │   │   ├── utils/                 # Generic utility functions
│   │   │   │   │   │   ├── TestUtil.java
│   │   │   │   │   │   ├── ConfigReader.java
│   │   │   │   │   ├── base/                  # Base classes
│   │   │   │   │   │   ├── TestBase.java
│   │   │   │   │   │   ├── ApiBase.java
│   │   │   │   │   ├── constants/             # Application constants
│   │   │   │   │   │   ├── AppConstants.java
│   │   │   │   │   ├── factory/               # Page Factory classes
│   │   │   │   │   │   ├── PageFactoryManager.java
│   │   │   │   │   ├── tests/                 # Test classes
│   │   │   │   │   │   ├── ui/                # UI test classes
│   │   │   │   │   │   │   ├── LoginTest.java
│   │   │   │   │   │   ├── api/               # API test classes
│   │   │   │   │   │   │   ├── UserApiTest.java
│   │   ├── resources/                         # Configuration files
│   │   │   ├── config.properties
│   │   │   ├── log4j2.xml
│   │   │   ├── testdata.json
│
├── .github/
│   ├── CODEOWNERS                             # GitHub Code Owners
│   ├── workflows/                             # CI/CD workflows
│   │   ├── nightly-build.yml
│   │   ├── on-push-build.yml
│   ├── pull_request_template.md               # Pull Request Template
│
├── .gitignore                                 # Ignored files and directories
├── pom.xml                                    # Maven configuration
├── README.md                                  # Project documentation

Example Files

1. LoginPage.java (Page Object)

package com.example.pages;

import com.microsoft.playwright.Page;
import com.microsoft.playwright.Locator;

public class LoginPage {
    private final Page page;
    private final Locator usernameInput;
    private final Locator passwordInput;
    private final Locator loginButton;

    public LoginPage(Page page) {
        this.page = page;
        this.usernameInput = page.locator("#username");
        this.passwordInput = page.locator("#password");
        this.loginButton = page.locator("#login");
    }

    public void login(String username, String password) {
        usernameInput.fill(username);
        passwordInput.fill(password);
        loginButton.click();
    }
}

2. TestBase.java

package com.example.base;

import com.microsoft.playwright.*;
import org.testng.annotations.*;

public class TestBase {
    protected Playwright playwright;
    protected Browser browser;
    protected BrowserContext context;
    protected Page page;

    @BeforeClass
    public void setUp() {
        playwright = Playwright.create();
        browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
        context = browser.newContext();
        page = context.newPage();
    }

    @AfterClass
    public void tearDown() {
        context.close();
        browser.close();
        playwright.close();
    }
}

3. ApiBase.java

package com.example.base;

import io.restassured.RestAssured;
import org.testng.annotations.BeforeClass;

public class ApiBase {
    @BeforeClass
    public void setUp() {
        RestAssured.baseURI = "https://api.example.com";
    }
}

4. nightly-build.yml

name: Nightly Build

on:
  schedule:
    - cron: "0 0 * * *"

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Set up JDK 11
      uses: actions/setup-java@v3
      with:
        java-version: '11'
        distribution: 'temurin'

    - name: Install dependencies
      run: mvn install

    - name: Run tests
      run: mvn test

5. CODEOWNERS

# Define code owners
* @team-lead @qa-engineer

6. pull_request_template.md

### Description

Please include a summary of the change and the issue it fixes.

### Checklist

- [ ] Unit tests added
- [ ] Documentation updated
- [ ] Code reviewed

7. Parallel Execution Configuration in TestNG

<suite name="Parallel Execution" parallel="tests" thread-count="4">
    <test name="Chrome">
        <parameter name="browser" value="chrome"/>
        <classes>
            <class name="com.example.tests.ui.LoginTest"/>
        </classes>
    </test>
    <test name="Firefox">
        <parameter name="browser" value="firefox"/>
        <classes>
            <class name="com.example.tests.ui.LoginTest"/>
        </classes>
    </test>
</suite>

8. README.md

# UI and API Automation Framework

## Overview

This project demonstrates a UI and API automation framework using Java, Playwright, and Rest-Assured. It supports parallel execution and CI/CD via GitHub Actions.

## Prerequisites

- Java 11+
- Maven 3+
- Node.js (for Playwright)

## Running Tests

To run all tests:
```sh
mvn test

CI/CD

Builds run nightly and on each push using GitHub Actions.


This structure ensures modularity, reusability, and scalability, making it an industry-standard setup for automated testing.