How sangcnguyen/playwright-java works
Project type and purpose
• This is an automated UI-testing project written in Java, built with Maven, using Playwright for browser automation.
• It follows the Page Object Model and a Factory pattern to spin up browser contexts and manage pages.
File structure
• Top-level files:
• pom.xml – Maven configuration and dependencies (Playwright, TestNG).
• testsuites/all-testcases.xml – TestNG suite definition.
• src/main/java:
• factory/ – BrowserFactory, AbstractBrowser, BrowserType, contexts, and ThreadLocal management.
• module/PageModule.java – central injector of page objects.
• pages/ – Page Object classes (DynamicLoadingPage, UploadPage).
• src/test/java:
• testcases/BaseTest.java – common setup/teardown (reads suite XML, initializes browser via PageModule).
• testcases/DynamicLoadingTest.java, UploadTest.java – concrete test cases.
Key architectural patterns
• Factory Pattern: BrowserFactory selects between ChromiumContext or FirefoxContext (AbstractBrowser).
• ThreadLocal Storage: PageThreadLocal holds per-thread browser/page instances.
• Dependency Injection: PageModule supplies page objects to tests.
• Page Object Model: pages/ directory encapsulates UI interactions.
Main components and their interactions
A. Test Runner / TestNG Suite
– Reads testsuites/all-testcases.xml → schedules test methods in DynamicLoadingTest, UploadTest.
B. BaseTest
– Before each test: calls PageModule.init() → BrowserFactory.createBrowser() → AbstractBrowser launches Playwright browser and context → stored in PageThreadLocal.
– After tests: closes browser contexts.
C. PageModule
– Provides getPage(DynamicLoadingPage.class) or getPage(UploadPage.class).
D. Page Objects
– Use Playwright Java API under the hood to drive UI (e.g., page.click(), page.uploadFile()).
E. Playwright & Browsers
– Underlying automation engine (Chromium, Firefox) invoked by AbstractBrowser implementations.
Important architectural elements to highlight
• ThreadLocal management: ensures browser/context are isolated per parallel test thread.
• Factory abstraction: allows easy extension to new browser types.
• PageModule as a simple DI container: centralizes page instantiation.
• Separation of concerns: Test logic vs. browser instantiation vs. page interactions.