Skip to content

Latest commit

 

History

History
96 lines (74 loc) · 3.36 KB

running-tests-java.md

File metadata and controls

96 lines (74 loc) · 3.36 KB
id title
running-tests
Running and debugging tests

Introduction

Playwright tests can be run in a variety of ways. We recommend hooking it up to your favorite test runner, e.g., JUnit, since it gives you the ability to run tests in parallel, run single test, etc.

You can run a single test, a set of tests or all tests. Tests can be run on one browser or multiple browsers. By default tests are run in a headless manner meaning no browser window will be opened while running the tests and results will be seen in the terminal. If you prefer, you can run your tests in headed mode by using the launch(new BrowserType.LaunchOptions().setHeadless(false)) option.

In JUnit, you can initialize [Playwright] and [Browser] in @BeforeAll method and destroy them in @AfterAll. In the example below, all three test methods use the same [Browser]. Each test uses its own [BrowserContext] and [Page].

package org.example;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import org.junit.jupiter.api.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestExample {
  // Shared between all tests in this class.
  static Playwright playwright;
  static Browser browser;

  // New instance for each test method.
  BrowserContext context;
  Page page;

  @BeforeAll
  static void launchBrowser() {
    playwright = Playwright.create();
    browser = playwright.chromium().launch();
  }

  @AfterAll
  static void closeBrowser() {
    playwright.close();
  }

  @BeforeEach
  void createContextAndPage() {
    context = browser.newContext();
    page = context.newPage();
  }

  @AfterEach
  void closeContext() {
    context.close();
  }

  @Test
  void shouldClickButton() {
    page.navigate("data:text/html,<script>var result;</script><button onclick='result=\"Clicked\"'>Go</button>");
    page.locator("button").click();
    assertEquals("Clicked", page.evaluate("result"));
  }

  @Test
  void shouldCheckTheBox() {
    page.setContent("<input id='checkbox' type='checkbox'></input>");
    page.locator("input").check();
    assertTrue((Boolean) page.evaluate("() => window['checkbox'].checked"));
  }

  @Test
  void shouldSearchWiki() {
    page.navigate("https://www.wikipedia.org/");
    page.locator("input[name=\"search\"]").click();
    page.locator("input[name=\"search\"]").fill("playwright");
    page.locator("input[name=\"search\"]").press("Enter");
    assertEquals("https://en.wikipedia.org/wiki/Playwright", page.url());
  }
}

See here for further details on how to run tests in parallel, etc.

See experimental JUnit integration to automatically initialize Playwright objects and more.

Run tests in headed mode

If you prefer, you can run your tests in headed mode by using the launch(new BrowserType.LaunchOptions().setHeadless(false)) option.

What's Next