|
1 | 1 | package runners;
|
2 | 2 |
|
3 |
| -import com.microsoft.playwright.Browser; |
4 |
| -import com.microsoft.playwright.BrowserType; |
5 |
| -import com.microsoft.playwright.Playwright; |
6 |
| -import org.json.simple.JSONArray; |
7 |
| -import org.json.simple.JSONObject; |
8 |
| -import org.json.simple.parser.JSONParser; |
9 |
| -import org.junit.jupiter.api.extension.*; |
| 3 | +import com.microsoft.playwright.*; |
| 4 | +import org.junit.jupiter.api.*; |
| 5 | +import org.yaml.snakeyaml.Yaml; |
10 | 6 |
|
11 |
| -import java.io.FileReader; |
12 | 7 | import java.io.UnsupportedEncodingException;
|
13 | 8 | import java.net.URLEncoder;
|
| 9 | +import org.json.JSONObject; |
14 | 10 | import java.util.*;
|
15 |
| -import java.util.stream.Stream; |
| 11 | +import java.io.File; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.nio.file.Files; |
16 | 14 |
|
17 |
| -public class BstackRunner implements TestTemplateInvocationContextProvider { |
18 |
| - public Browser browser; |
19 |
| - public String wss; |
20 |
| - private JSONObject mainConfig; |
21 |
| - private JSONArray platformConfig; |
| 15 | +public class BstackRunner { |
| 16 | + public static String userName, accessKey; |
| 17 | + public static Map<String, Object> browserStackYamlMap; |
| 18 | + public static final String USER_DIR = "user.dir"; |
| 19 | + |
| 20 | + static Playwright playwright; |
| 21 | + static Browser browser; |
22 | 22 |
|
23 |
| - @Override |
24 |
| - public boolean supportsTestTemplate(ExtensionContext extensionContext) { |
25 |
| - return true; |
| 23 | + BrowserContext context; |
| 24 | + public Page page; |
| 25 | + |
| 26 | + public BstackRunner() { |
| 27 | + File file = new File(getUserDir() + "/browserstack.yml"); |
| 28 | + browserStackYamlMap = convertYamlFileToMap(file, new HashMap<>()); |
26 | 29 | }
|
27 | 30 |
|
28 |
| - @Override |
29 |
| - public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext extensionContext) { |
30 |
| - List<TestTemplateInvocationContext> desiredCapsInvocationContexts = new ArrayList<>(); |
| 31 | + @BeforeEach |
| 32 | + void launchBrowser() { |
| 33 | + playwright = Playwright.create(); |
| 34 | + BrowserType browserType = playwright.chromium(); |
| 35 | + String caps = null; |
| 36 | + userName = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : (String) browserStackYamlMap.get("userName"); |
| 37 | + accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : (String) browserStackYamlMap.get("accessKey"); |
31 | 38 |
|
32 |
| - try { |
33 |
| - JSONParser parser = new JSONParser(); |
34 |
| - mainConfig = (JSONObject) parser |
35 |
| - .parse(new FileReader("src/test/resources/conf/" + System.getProperty("config"))); |
36 |
| - platformConfig = (JSONArray) mainConfig.get("environments"); |
37 |
| - wss = (String) mainConfig.get("wss"); |
| 39 | + HashMap<String, String> capabilitiesObject = new HashMap<>(); |
| 40 | + capabilitiesObject.put("browserstack.user", userName); |
| 41 | + capabilitiesObject.put("browserstack.key", accessKey); |
| 42 | + capabilitiesObject.put("browserstack.source", "java-playwright-browserstack:sample-sdk:v1.0"); |
| 43 | + capabilitiesObject.put("browser", "chrome"); |
38 | 44 |
|
39 |
| - for (int i = 0; i < platformConfig.size(); i++) { |
40 |
| - JSONObject platform = (JSONObject) platformConfig.get(i); |
41 |
| - desiredCapsInvocationContexts.add(invocationContext(platform)); |
42 |
| - } |
43 |
| - } catch (Exception e) { |
44 |
| - e.printStackTrace(); |
| 45 | + JSONObject jsonCaps = new JSONObject(capabilitiesObject); |
| 46 | + try { |
| 47 | + caps = URLEncoder.encode(jsonCaps.toString(), "utf-8"); |
| 48 | + } catch (UnsupportedEncodingException e) { |
| 49 | + throw new RuntimeException(e); |
45 | 50 | }
|
46 |
| - return desiredCapsInvocationContexts.stream(); |
| 51 | + String wsEndpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps; |
| 52 | + browser = browserType.connect(wsEndpoint); |
| 53 | + page = browser.newPage(); |
47 | 54 | }
|
48 | 55 |
|
49 |
| - private TestTemplateInvocationContext invocationContext(JSONObject capabilitiesObject) { |
50 |
| - return new TestTemplateInvocationContext() { |
51 |
| - |
52 |
| - @Override |
53 |
| - public List<Extension> getAdditionalExtensions() { |
| 56 | + @AfterEach |
| 57 | + void closeContext() { |
| 58 | + page.close(); |
| 59 | + browser.close(); |
| 60 | + } |
54 | 61 |
|
55 |
| - return Collections.singletonList(new ParameterResolver() { |
56 |
| - @Override |
57 |
| - public boolean supportsParameter(ParameterContext parameterContext, |
58 |
| - ExtensionContext extensionContext) { |
59 |
| - return parameterContext.getParameter().getType().equals(Browser.class); |
60 |
| - } |
| 62 | + private String getUserDir() { |
| 63 | + return System.getProperty(USER_DIR); |
| 64 | + } |
61 | 65 |
|
62 |
| - @Override |
63 |
| - public Object resolveParameter(ParameterContext parameterContext, |
64 |
| - ExtensionContext extensionContext) { |
65 |
| - Playwright playwright = Playwright.create(); |
66 |
| - BrowserType browserType = playwright.chromium(); |
67 |
| - String caps = null; |
68 |
| - try { |
69 |
| - caps = URLEncoder.encode(capabilitiesObject.toString(), "utf-8"); |
70 |
| - } catch (UnsupportedEncodingException e) { |
71 |
| - throw new RuntimeException(e); |
72 |
| - } |
73 |
| - String ws_endpoint = wss + "?caps=" + caps; |
74 |
| - browser = browserType.connect(ws_endpoint); |
75 |
| - return browser; |
76 |
| - } |
77 |
| - }); |
78 |
| - } |
79 |
| - }; |
| 66 | + private Map<String, Object> convertYamlFileToMap(File yamlFile, Map<String, Object> map) { |
| 67 | + try { |
| 68 | + InputStream inputStream = Files.newInputStream(yamlFile.toPath()); |
| 69 | + Yaml yaml = new Yaml(); |
| 70 | + Map<String, Object> config = yaml.load(inputStream); |
| 71 | + map.putAll(config); |
| 72 | + } catch (Exception e) { |
| 73 | + throw new RuntimeException(String.format("Malformed browserstack.yml file - %s.", e)); |
| 74 | + } |
| 75 | + return map; |
80 | 76 | }
|
81 | 77 | }
|
0 commit comments