Giter VIP home page Giter VIP logo

junit-playwright's Introduction

junit-playwright

junit-playwright allows you to run your JUnit 5 Playwright tests in parallel easily. junit-playwright provides isolated environments for each test and exposes Playwright-related objects as test parameters for you to use in your tests.

Note

junit-playwright has recently been updated to v2.0. This brings some breaking changes. Please see the documentation in the wiki for v1 docs. It is recommended to upgrade to v2.0. Migration help can be found at the end of this readme.

Installation

<dependency>
    <groupId>io.github.uchagani</groupId>
    <artifactId>junit-playwright</artifactId>
    <version>3.0.1</version>
</dependency>

Getting Started

junit-playwright will inject Playwright objects in your tests. There are two different interfaces that you can implement based on the type of testing that you want to do: Browser or API.

Browser Testing

Create a config class

Create a class and implement the PlaywrightBrowserConfig interface

public class DefaultBrowserConfig implements PlaywrightBrowserConfig {

    @Override
    public BrowserConfig getBrowserConfig() {
        return new BrowserConfig()
                .chromium()
                .launch();
    }
}

PlaywrightBrowserConfig has one method: getBrowserConfig. Through the BrowserConfig object you can specify your playwright-related config. The API is similar to playwright-java. All the options that you would specify to initialize Playwright, Browser, BrowserContext, or Page you can do via BrowserConfig object.

Writing tests

To inject Playwright objects into your test there are two parts:

  1. Add the @UseBrowserConfig annotation to your test class and specify your config class.
  2. Add the Playwright object that you need to interact with in your test as a test parameter.
@UseBrowserConfig(DefaultBrowserConfig.class)
public class InjectBrowserTests {
    @Test
    public void someTest(Page page) {
        page.navigate("https://playwright.dev/java/");
    }
}

junit-playwright gives you the following Playwright-related test parameters:

API Testing

Two use Playwright's APIRequestContext you need to implement the PlaywrightRestConfig interface:

public class DefaultRestConfig implements PlaywrightRestConfig {
    @Override
    public RestConfig getRestConfig() {
        return new RestConfig();
    }
}

Then you can use this config in your tests:

@UseRestConfig(DefaultRestConfig.class)
public class APIRequestContextTests {
    @Test
    public void someAPITest(APIRequestContext request) {
        request.get("https://api.coindesk.com/v1/bpi/currentprice.json");
    }
}

@UseBrowserConfig annotation can be used either at the class level, method level.
@UseRestConfig annotation can be used either at the class level, method leve, or parameter level.

Running tests in parallel

playwright-junit makes it easy to run tests in parallel. Each test will get an isolated Playwright environment. All you have to do is enable parallel tests in junit. The easiest way to do this is to create a file in your classpath called junit-platform.properties. For example: src/test/resources/junit-platform.properties and enable parallel tests:

junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent
junit.jupiter.execution.parallel.mode.classes.default=concurrent

You can read more about running junit tests in parallel in their documentation. With the above configuration tests in the same class will be run in parallel.

Examples

Please take a look at the tests located int src/test/java/io/github/uchagani/jp for more information on how to create configs and create tests.

Advanced

You can override the config for a particular test method by adding the @UseBrowserConfig annotation over a test method:

Overriding config

@UseBrowserConfig(DefaultConfig.class)
public class InjectBrowserTests {
    @Test
    public void createAChromeBrowser(Browser browser) {
        //Browser is configured with the `DefaultConfig` specified at the class level
    }

    @Test
    @UseBrowserConfig(OverrideConfig.class)
    public void createAFirefoxBrowser(Browser browser) {
        //For this test, use the browser configured in the `OverrideConfig` class
    }
}

Testing multiple APIs in one test

@Test
public void useDifferentConfigsInTheSameTest(@UseRestConfig(
        SpecificRestConfig.class) APIRequestContext specificRequest, @UseRestConfig(
        OverrideRestConfig.class) APIRequestContext overrideRequest) {
    
    assertThat(specificRequest).isNotNull();
    assertThat(overrideRequest).isNotNull();
    assertThat(specificRequest.get("/foo").url()).contains("google.com/foo");
    assertThat(overrideRequest.get("/foo").url()).contains("bing.com/foo");
}

Requirements

  • Java 8+
  • Playwright 1.18.0+
  • JUnit 5.6+

Migrating from v1

To migrate to v2.x from v1.x, there are a couple of changes that you need to make:

  1. Change PlaywrightConfig to PlaywrightBrowserConfig.
  2. Change @InjectPlaywright to @UseBrowserConfig.

junit-playwright's People

Contributors

uchagani avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

junit-playwright's Issues

getSafeTestName() should make the test names more specific

While running tests in Jenkins, I noticed that Playwright trace file names only had the test method name in the file name (eg, MyTest.zip). If there are tests with the same name but different parameters, just using the test method name may not be specific enough and previous zip files may be overwritten. Threading may need to be taken in to account as well but not sure.

It is possible the following method needs to be changed: https://github.com/uchagani/junit-playwright/blob/develop/src/main/java/io/github/uchagani/jp/PlaywrightTestWatcher.java#L55-L57

Hooks classes

Is there away to add cookie accepts etc in the hooks class before running the test?

If we are injecting the context into the test how do we interact with that context outside of the test?

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.