docs: add new java intro (#17695)
Fixes https://github.com/microsoft/playwright/issues/17479
This commit is contained in:
parent
7a65e0cbb3
commit
a1d37e9169
|
|
@ -195,7 +195,7 @@ See our doc on [Test Runners](./test-runners.md) to learn more about running tes
|
|||
## What's next
|
||||
|
||||
- [Write tests using web first assertions, page fixtures and locators](./writing-tests.md)
|
||||
- [Run single tests, multiple tests, headed mode](./running-tests.md)
|
||||
- [Run single test, multiple tests, headed mode](./running-tests.md)
|
||||
- [Learn more about the NUnit and MSTest base classes](./test-runners.md)
|
||||
- [Generate tests with Codegen](./codegen.md)
|
||||
- [See a trace of your tests](./trace-viewer-intro.md)
|
||||
|
|
|
|||
|
|
@ -1,40 +1,40 @@
|
|||
---
|
||||
id: intro
|
||||
title: "Getting started"
|
||||
title: "Installation"
|
||||
---
|
||||
|
||||
<!-- TOC -->
|
||||
- [Release notes](./release-notes.md)
|
||||
|
||||
## Installation
|
||||
Playwright was created specifically to accommodate the needs of end-to-end testing. Playwright supports all modern rendering engines including Chromium, WebKit, and Firefox. Test on Windows, Linux, and macOS, locally or on CI, headless or headed with native mobile emulation.
|
||||
|
||||
Playwright is distributed as a set of [Maven](https://maven.apache.org/what-is-maven.html) modules. The easiest way to use it is to add one dependency to your project's `pom.xml` as described below. If you're not familiar with Maven please refer to its [documentation](https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html).
|
||||
|
||||
## Usage
|
||||
|
||||
Get started by installing Playwright and running the example file to see it in action.
|
||||
|
||||
<Tabs
|
||||
defaultValue="java"
|
||||
values={[
|
||||
{label: 'Example.java', value: 'java'},
|
||||
{label: 'App.java', value: 'java'},
|
||||
{label: 'pom.xml', value: 'pom'}
|
||||
]
|
||||
}>
|
||||
<TabItem value="java">
|
||||
|
||||
```java
|
||||
// src/main/java/org/example/App.java
|
||||
package org.example;
|
||||
|
||||
import com.microsoft.playwright.*;
|
||||
|
||||
public class Example {
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create()) {
|
||||
Browser browser = playwright.chromium().launch();
|
||||
Page page = browser.newPage();
|
||||
page.navigate("http://playwright.dev");
|
||||
System.out.println(page.title());
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create()) {
|
||||
Browser browser = playwright.chromium().launch();
|
||||
Page page = browser.newPage();
|
||||
page.navigate("http://playwright.dev");
|
||||
System.out.println(page.title());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
@ -43,9 +43,8 @@ public class Example {
|
|||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
|
|
@ -67,11 +66,7 @@ public class Example {
|
|||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
<version>3.10.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
|
@ -84,7 +79,7 @@ public class Example {
|
|||
With the Example.java and pom.xml above, compile and execute your new program as follows:
|
||||
|
||||
```bash
|
||||
mvn compile exec:java -Dexec.mainClass="org.example.Example"
|
||||
mvn compile exec:java -Dexec.mainClass="org.example.App"
|
||||
```
|
||||
|
||||
Running it downloads the Playwright package and installs browser binaries for Chromium, Firefox and WebKit. To modify this behavior see [installation parameters](./browsers.md#installing-browsers).
|
||||
|
|
@ -94,10 +89,12 @@ Running it downloads the Playwright package and installs browser binaries for Ch
|
|||
In our first script, we will navigate to `whatsmyuseragent.org` and take a screenshot in WebKit.
|
||||
|
||||
```java
|
||||
package org.example;
|
||||
|
||||
import com.microsoft.playwright.*;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class WebKitScreenshot {
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create()) {
|
||||
Browser browser = playwright.webkit().launch();
|
||||
|
|
@ -115,36 +112,17 @@ By default, Playwright runs the browsers in headless mode. To see the browser UI
|
|||
playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(50));
|
||||
```
|
||||
|
||||
## Record scripts
|
||||
|
||||
[Command line tools](./cli.md) can be used to record user interactions and generate Java code.
|
||||
## Running the Example script
|
||||
|
||||
```bash
|
||||
mvn exec:java -e -Dexec.mainClass=com.microsoft.playwright.CLI -Dexec.args="codegen wikipedia.org"
|
||||
mvn compile exec:java -Dexec.mainClass="org.example.App"
|
||||
```
|
||||
|
||||
## System requirements
|
||||
By default browsers launched with Playwright run headless, meaning no browser UI will open up when running the script. To change that you can pass `new BrowserType.LaunchOptions().setHeadless(false)` when launching the browser.
|
||||
|
||||
Playwright requires **Java 8** or newer. The browser binaries for Chromium,
|
||||
Firefox and WebKit work across the 3 platforms (Windows, macOS, Linux):
|
||||
## What's next
|
||||
|
||||
### Windows
|
||||
|
||||
Works with Windows and Windows Subsystem for Linux (WSL).
|
||||
|
||||
### macOS
|
||||
|
||||
Requires 11 (Big Sur) or above.
|
||||
|
||||
### Linux
|
||||
|
||||
Depending on your Linux distribution, you might need to install additional
|
||||
dependencies to run the browsers.
|
||||
|
||||
:::note
|
||||
Only Ubuntu 18.04, 20.04, and 22.04 are officially supported.
|
||||
:::
|
||||
|
||||
See also in the [Command line tools](./cli.md#install-system-dependencies)
|
||||
which has a command to install all necessary dependencies automatically for Ubuntu
|
||||
LTS releases.
|
||||
- [Write tests using web first assertions, page fixtures and locators](./writing-tests.md)
|
||||
- [Run single test, multiple tests, headed mode](./running-tests.md)
|
||||
- [Generate tests with Codegen](./codegen.md)
|
||||
- [See a trace of your tests](./trace-viewer-intro.md)
|
||||
|
|
|
|||
|
|
@ -99,6 +99,6 @@ npx playwright show-report
|
|||
## What's next
|
||||
|
||||
- [Write tests using web first assertions, page fixtures and locators](./writing-tests.md)
|
||||
- [Run single tests, multiple tests, headed mode](./running-tests.md)
|
||||
- [Run single test, multiple tests, headed mode](./running-tests.md)
|
||||
- [Generate tests with Codegen](./codegen-intro.md)
|
||||
- [See a trace of your tests](./trace-viewer-intro.md)
|
||||
|
|
|
|||
|
|
@ -57,11 +57,9 @@ By default tests will be run on chromium. This can be configured via the CLI opt
|
|||
pytest
|
||||
```
|
||||
|
||||
See our doc on [Running Tests](./running-tests.md) to learn more about running tests in headed mode, running multiple tests, running specific tests etc.
|
||||
|
||||
## What's next
|
||||
|
||||
- [Write tests using web first assertions, page fixtures and locators](./writing-tests.md)
|
||||
- [Run single tests, multiple tests, headed mode](./running-tests.md)
|
||||
- [Run single test, multiple tests, headed mode](./running-tests.md)
|
||||
- [Generate tests with Codegen](./codegen.md)
|
||||
- [See a trace of your tests](./trace-viewer-intro.md)
|
||||
|
|
|
|||
|
|
@ -124,7 +124,7 @@ test('getting started should contain table of contents', async ({ page }) => {
|
|||
`How to run the example test`,
|
||||
`How to open the HTML test report`,
|
||||
`Write tests using web first assertions, page fixtures and locators`,
|
||||
`Run single tests, multiple tests, headed mode`,
|
||||
`Run single test, multiple tests, headed mode`,
|
||||
`Generate tests with Codegen`,
|
||||
`See a trace of your tests`
|
||||
]);
|
||||
|
|
@ -153,7 +153,7 @@ test('getting started should contain table of contents', async ({ page }) => {
|
|||
`How to run the example test`,
|
||||
`How to open the HTML test report`,
|
||||
`Write tests using web first assertions, page fixtures and locators`,
|
||||
`Run single tests, multiple tests, headed mode`,
|
||||
`Run single test, multiple tests, headed mode`,
|
||||
`Generate tests with Codegen`,
|
||||
`See a trace of your tests`
|
||||
]);
|
||||
|
|
@ -181,7 +181,7 @@ await expect(playwrightDev.tocList).toHaveText([
|
|||
`How to run the example test`,
|
||||
`How to open the HTML test report`,
|
||||
`Write tests using web first assertions, page fixtures and locators`,
|
||||
`Run single tests, multiple tests, headed mode`,
|
||||
`Run single test, multiple tests, headed mode`,
|
||||
`Generate tests with Codegen`,
|
||||
`See a trace of your tests`
|
||||
]);
|
||||
|
|
|
|||
88
docs/src/running-tests-java.md
Normal file
88
docs/src/running-tests-java.md
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
---
|
||||
id: running-tests
|
||||
title: "Running Tests"
|
||||
---
|
||||
|
||||
Playwright tests can be run in a variety of ways. We recommend hooking it up to your favorite test runner, e.g. [JUnit](./test-runners.md) 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](https://junit.org/junit5/) you can initialize [Playwright] and [Browser] in [@BeforeAll](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/BeforeAll.html) method and
|
||||
destroy them in [@AfterAll](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/AfterAll.html). In the example below all three test methods use the same
|
||||
[Browser]. Each test uses its own [BrowserContext] and [Page].
|
||||
|
||||
```java
|
||||
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](./test-runners.md) for further details on how to run tests in parallel etc..
|
||||
|
||||
## What's Next
|
||||
|
||||
- [Debugging tests](./debug.md)
|
||||
- [Generate tests with Codegen](./codegen.md)
|
||||
- [See a trace of your tests](./trace-viewer-intro.md)
|
||||
|
|
@ -92,7 +92,7 @@ test('getting started should contain table of contents', async ({ page }) => {
|
|||
`How to run the example test`,
|
||||
`How to open the HTML test report`,
|
||||
`Write tests using web first assertions, page fixtures and locators`,
|
||||
`Run single tests, multiple tests, headed mode`,
|
||||
`Run single test, multiple tests, headed mode`,
|
||||
`Generate tests with Codegen`,
|
||||
`See a trace of your tests`
|
||||
]);
|
||||
|
|
@ -121,7 +121,7 @@ test('getting started should contain table of contents', async ({ page }) => {
|
|||
`How to run the example test`,
|
||||
`How to open the HTML test report`,
|
||||
`Write tests using web first assertions, page fixtures and locators`,
|
||||
`Run single tests, multiple tests, headed mode`,
|
||||
`Run single test, multiple tests, headed mode`,
|
||||
`Generate tests with Codegen`,
|
||||
`See a trace of your tests`
|
||||
]);
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ isolated between the tests.
|
|||
|
||||
## JUnit
|
||||
|
||||
In JUnit you can initialize [Playwright] and [Browser] in [@BeforeAll](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/BeforeAll.html) method and
|
||||
In [JUnit](https://junit.org/junit5/) you can initialize [Playwright] and [Browser] in [@BeforeAll](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/BeforeAll.html) method and
|
||||
destroy them in [@AfterAll](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/AfterAll.html). In the example below all three test methods use the same
|
||||
[Browser]. Each test uses its own [BrowserContext] and [Page].
|
||||
|
||||
|
|
|
|||
|
|
@ -241,6 +241,6 @@ public class UnitTest1 : PageTest
|
|||
|
||||
## What's Next
|
||||
|
||||
- [Run single tests, multiple tests, headed mode](./running-tests.md)
|
||||
- [Run single test, multiple tests, headed mode](./running-tests.md)
|
||||
- [Generate tests with Codegen](./codegen.md)
|
||||
- [See a trace of your tests](./trace-viewer-intro.md)
|
||||
94
docs/src/writing-tests-java.md
Normal file
94
docs/src/writing-tests-java.md
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
---
|
||||
id: writing-tests
|
||||
title: "Writing Tests"
|
||||
---
|
||||
|
||||
Playwright assertions are created specifically for the dynamic web. Checks are automatically retried until the necessary conditions are met. Playwright comes with [auto-wait](./actionability.md) built in meaning it waits for elements to be actionable prior to performing actions. Playwright provides [assertThat](./test-assertions.md) overloads to write assertions.
|
||||
|
||||
Take a look at the example test below to see how to write a test using web first assertions, locators and selectors.
|
||||
|
||||
```java
|
||||
package org.example;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
import com.microsoft.playwright.*;
|
||||
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
try (Playwright playwright = Playwright.create()) {
|
||||
Browser browser = playwright.chromium().launch();
|
||||
Page page = browser.newPage();
|
||||
page.navigate("http://playwright.dev");
|
||||
|
||||
// Expect a title "to contain" a substring.
|
||||
assertThat(page).hasTitle(Pattern.compile("Playwright"));
|
||||
|
||||
// create a locator
|
||||
Locator getStarted = page.locator("text=Get Started");
|
||||
|
||||
// Expect an attribute "to be strictly equal" to the value.
|
||||
assertThat(getStarted).hasAttribute("href", "/docs/intro");
|
||||
|
||||
// Click the get started link.
|
||||
getStarted.click();
|
||||
|
||||
// Expects the URL to contain intro.
|
||||
assertThat(page).hasURL(Pattern.compile(".*intro"));
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Assertions
|
||||
|
||||
Playwright provides [`assertThat`](./test-assertions.md) overloads which will wait until the expected condition is met.
|
||||
|
||||
```java
|
||||
import java.util.regex.Pattern;
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
|
||||
|
||||
assertThat(page).hasTitle(Pattern.compile("Playwright"));
|
||||
```
|
||||
|
||||
|
||||
### Locators
|
||||
|
||||
[Locators](./locators.md) are the central piece of Playwright's auto-waiting and retry-ability. Locators represent a way to find element(s) on the page at any moment and are used to perform actions on elements such as `.click` `.fill` etc. Custom locators can be created with the [`method: Page.locator`] method.
|
||||
|
||||
```java
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
|
||||
|
||||
Locator getStarted = page.locator("text=Get Started");
|
||||
|
||||
assertThat(getStarted).hasAttribute("href", "/docs/intro");
|
||||
getStarted.click();
|
||||
```
|
||||
|
||||
[Selectors](./selectors.md) are strings that are used to create Locators. Playwright supports many different selectors like [Text](./selectors.md#text-selector), [CSS](./selectors.md#css-selector), [XPath](./selectors.md#xpath-selectors) and many more. Learn more about available selectors and how to pick one in this [in-depth guide](./selectors.md).
|
||||
|
||||
|
||||
```java
|
||||
import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;
|
||||
|
||||
assertThat(page.locator("text=Installation")).isVisible();
|
||||
```
|
||||
|
||||
|
||||
### Test Isolation
|
||||
|
||||
Playwright has the concept of a [BrowserContext] which is an in-memory isolated browser profile. It's recommended to create a new [BrowserContext] for each test to ensure they don't interfere with each other.
|
||||
|
||||
```java
|
||||
Browser browser = playwright.chromium().launch();
|
||||
BrowserContext context = browser.newContext();
|
||||
Page page = context.newPage();
|
||||
```
|
||||
|
||||
## What's Next
|
||||
|
||||
- [Run single test, multiple tests, headed mode](./running-tests.md)
|
||||
- [Generate tests with Codegen](./codegen.md)
|
||||
- [See a trace of your tests](./trace-viewer-intro.md)
|
||||
|
|
@ -144,6 +144,6 @@ test.describe("navigation", () => {
|
|||
|
||||
## What's Next
|
||||
|
||||
- [Run single tests, multiple tests, headed mode](./running-tests.md)
|
||||
- [Run single test, multiple tests, headed mode](./running-tests.md)
|
||||
- [Generate tests with Codegen](./codegen-intro.md)
|
||||
- [See a trace of your tests](./trace-viewer-intro.md)
|
||||
|
|
@ -102,6 +102,6 @@ def test_main_navigation(page: Page):
|
|||
|
||||
## What's Next
|
||||
|
||||
- [Run single tests, multiple tests, headed mode](./running-tests.md)
|
||||
- [Run single test, multiple tests, headed mode](./running-tests.md)
|
||||
- [Generate tests with Codegen](./codegen.md)
|
||||
- [See a trace of your tests](./trace-viewer-intro.md)
|
||||
Loading…
Reference in a new issue