forked from microsoft/playwright-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestApp.java
More file actions
62 lines (51 loc) · 1.72 KB
/
TestApp.java
File metadata and controls
62 lines (51 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.microsoft.playwright.springboottest;
import com.microsoft.playwright.*;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
@SpringBootApplication
public class TestApp implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(TestApp.class, args);
}
@Override
public void run(String... args) {
if (Arrays.asList(args).contains("--async")) {
runAsync();
} else {
runSync();
}
}
private void runAsync() {
CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(this::runSync);
voidCompletableFuture.join();
}
private void runSync() {
try (Playwright playwright = Playwright.create()) {
BrowserType browserType = getBrowserTypeFromEnv(playwright);
System.out.println("Running test with " + browserType.name());
Browser browser = browserType.launch();
BrowserContext context = browser.newContext();
Page page = context.newPage();
System.out.println(page.evaluate("'SUCCESS: did evaluate in page'"));
}
}
static BrowserType getBrowserTypeFromEnv(Playwright playwright) {
String browserName = System.getenv("BROWSER");
if (browserName == null) {
browserName = "chromium";
}
switch (browserName) {
case "webkit":
return playwright.webkit();
case "firefox":
return playwright.firefox();
case "chromium":
return playwright.chromium();
default:
throw new IllegalArgumentException("Unknown browser: " + browserName);
}
}
}