docs(test-runners-java.md): added testng implementation (#26537)
Resolves microsoft/playwright-java#1343
This commit is contained in:
parent
c27317b6e5
commit
cd07401d20
|
|
@ -278,3 +278,77 @@ Also, Playwright command line tools can be run with :
|
|||
```bash
|
||||
./gradlew playwright --args="help"
|
||||
```
|
||||
|
||||
## TestNG
|
||||
|
||||
In [TestNG](https://testng.org/) you can initialize [Playwright] and [Browser] in [@BeforeClass](https://javadoc.io/doc/org.testng/testng/latest/org/testng/annotations/BeforeClass.html) method and
|
||||
destroy them in [@AfterClass](https://javadoc.io/doc/org.testng/testng/latest/org/testng/annotations/AfterClass.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.testng.annotations.*;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
public class TestExample {
|
||||
// Shared between all tests in this class.
|
||||
Playwright playwright;
|
||||
Browser browser;
|
||||
|
||||
// New instance for each test method.
|
||||
BrowserContext context;
|
||||
Page page;
|
||||
|
||||
@BeforeClass
|
||||
void launchBrowser() {
|
||||
playwright = Playwright.create();
|
||||
browser = playwright.chromium().launch();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
void closeBrowser() {
|
||||
playwright.close();
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
void createContextAndPage() {
|
||||
context = browser.newContext();
|
||||
page = context.newPage();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
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());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
|||
Loading…
Reference in a new issue