|
| 1 | +--- |
| 2 | +id: test-runners |
| 3 | +title: "Test Runners" |
| 4 | +--- |
| 5 | + |
| 6 | +With a few lines of code, you can hook up Playwright to your favorite Java test runner. |
| 7 | + |
| 8 | +Playwright and Browser instances can be reused between tests for better performance. We |
| 9 | +recommend running each test case in a new BrowserContext, this way browser state will be |
| 10 | +isolated between the tests. |
| 11 | + |
| 12 | +<!-- TOC --> |
| 13 | + |
| 14 | +## JUnit |
| 15 | + |
| 16 | +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 |
| 17 | +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 |
| 18 | +[Browser]. Each test uses its own [BrowserContext] and [Page]. |
| 19 | + |
| 20 | +```java |
| 21 | +package org.example; |
| 22 | + |
| 23 | +import com.microsoft.playwright.Browser; |
| 24 | +import com.microsoft.playwright.BrowserContext; |
| 25 | +import com.microsoft.playwright.Page; |
| 26 | +import com.microsoft.playwright.Playwright; |
| 27 | +import org.junit.jupiter.api.*; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 31 | + |
| 32 | +public class TestExample { |
| 33 | + // Shared between all tests in this class. |
| 34 | + static Playwright playwright; |
| 35 | + static Browser browser; |
| 36 | + |
| 37 | + // New instance for each test method. |
| 38 | + BrowserContext context; |
| 39 | + Page page; |
| 40 | + |
| 41 | + @BeforeAll |
| 42 | + static void launchBrowser() { |
| 43 | + playwright = Playwright.create(); |
| 44 | + browser = playwright.chromium().launch(); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterAll |
| 48 | + static void closeBrowser() { |
| 49 | + playwright.close(); |
| 50 | + } |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + void createContextAndPage() { |
| 54 | + context = browser.newContext(); |
| 55 | + page = context.newPage(); |
| 56 | + } |
| 57 | + |
| 58 | + @AfterEach |
| 59 | + void closeContext() { |
| 60 | + context.close(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void shouldClickButton() { |
| 65 | + page.navigate("data:text/html,<script>var result;</script><button onclick='result=\"Clicked\"'>Go</button>"); |
| 66 | + page.click("button"); |
| 67 | + assertEquals("Clicked", page.evaluate("result")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void shouldCheckTheBox() { |
| 72 | + page.setContent("<input id='checkbox' type='checkbox'></input>"); |
| 73 | + page.check("input"); |
| 74 | + assertTrue((Boolean) page.evaluate("() => window['checkbox'].checked")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void shouldSearchWiki() { |
| 79 | + page.navigate("https://www.wikipedia.org/"); |
| 80 | + page.click("input[name=\"search\"]"); |
| 81 | + page.fill("input[name=\"search\"]", "playwright"); |
| 82 | + page.press("input[name=\"search\"]", "Enter"); |
| 83 | + assertEquals("https://en.wikipedia.org/wiki/Playwright", page.url()); |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### Running Tests in Parallel |
| 89 | + |
| 90 | +By default JUnit will run all tests sequentially on a single thread. Since JUnit 5.3 you can change this behavior to run tests in parallel |
| 91 | +to speed up execution (see [this page](https://junit.org/junit5/docs/snapshot/user-guide/index.html#writing-tests-parallel-execution)). |
| 92 | +Since it is not safe to use same Playwright objects from multiple threads without extra synchronization we recommend you create Playwright |
| 93 | +instance per thread and use it on that thread exclusively. Here is an example how to run multiple test classes in parallel. |
| 94 | + |
| 95 | +Use [`@TestInstance(TestInstance.Lifecycle.PER_CLASS)`](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/TestInstance.html) |
| 96 | +annotation to make JUnit create one instance of a class for all test methods within that class (by default each JUnit will create a new instance of the class |
| 97 | +for each test method). Store [Playwright] and [Browser] objects in instance fields. They will be shared between tests. Each instace of the class will use its |
| 98 | +own copy of Playwright. |
| 99 | + |
| 100 | + |
| 101 | +```java |
| 102 | +// Subclasses will inherit PER_CLASS behavior. |
| 103 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 104 | +class TestFixtures { |
| 105 | + // Shared between all tests in the class. |
| 106 | + Playwright playwright; |
| 107 | + Browser browser; |
| 108 | + |
| 109 | + @BeforeAll |
| 110 | + void launchBrowser() { |
| 111 | + playwright = Playwright.create(); |
| 112 | + browser = playwright.chromium().launch(); |
| 113 | + } |
| 114 | + |
| 115 | + @AfterAll |
| 116 | + void closeBrowser() { |
| 117 | + playwright.close(); |
| 118 | + } |
| 119 | + |
| 120 | + // New instance for each test method. |
| 121 | + BrowserContext context; |
| 122 | + Page page; |
| 123 | + |
| 124 | + @BeforeEach |
| 125 | + void createContextAndPage() { |
| 126 | + context = browser.newContext(); |
| 127 | + page = context.newPage(); |
| 128 | + } |
| 129 | + |
| 130 | + @AfterEach |
| 131 | + void closeContext() { |
| 132 | + context.close(); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +class Test1 extends TestFixtures { |
| 137 | + @Test |
| 138 | + void shouldClickButton() { |
| 139 | + page.navigate("data:text/html,<script>var result;</script><button onclick='result=\"Clicked\"'>Go</button>"); |
| 140 | + page.click("button"); |
| 141 | + assertEquals("Clicked", page.evaluate("result")); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + void shouldCheckTheBox() { |
| 146 | + page.setContent("<input id='checkbox' type='checkbox'></input>"); |
| 147 | + page.check("input"); |
| 148 | + assertTrue((Boolean) page.evaluate("() => window['checkbox'].checked")); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void shouldSearchWiki() { |
| 153 | + page.navigate("https://www.wikipedia.org/"); |
| 154 | + page.click("input[name=\"search\"]"); |
| 155 | + page.fill("input[name=\"search\"]", "playwright"); |
| 156 | + page.press("input[name=\"search\"]", "Enter"); |
| 157 | + assertEquals("https://en.wikipedia.org/wiki/Playwright", page.url()); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +class Test2 extends TestFixtures { |
| 162 | + @Test |
| 163 | + void shouldReturnInnerHTML() { |
| 164 | + page.setContent("<div>hello</div>"); |
| 165 | + assertEquals("hello", page.innerHTML("css=div")); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + void shouldClickButton() { |
| 170 | + Page popup = page.waitForPopup(() -> { |
| 171 | + page.evaluate("window.open('about:blank');"); |
| 172 | + }); |
| 173 | + assertEquals("about:blank", popup.url()); |
| 174 | + } |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | + |
| 179 | +Configure JUnit to run tests in each class sequentially and run multiple classes on parallel threads (with max |
| 180 | +number of thread equal to 1/2 of the number of CPU cores): |
| 181 | + |
| 182 | +```sh |
| 183 | +junit.jupiter.execution.parallel.enabled = true |
| 184 | +junit.jupiter.execution.parallel.mode.default = same_thread |
| 185 | +junit.jupiter.execution.parallel.mode.classes.default = concurrent |
| 186 | +junit.jupiter.execution.parallel.config.strategy=dynamic |
| 187 | +junit.jupiter.execution.parallel.config.dynamic.factor=0.5 |
| 188 | +``` |
0 commit comments