|
| 1 | +package com.browserstack; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.microsoft.playwright.*; |
| 5 | +import java.io.UnsupportedEncodingException; |
| 6 | + |
| 7 | +import java.net.URLEncoder; |
| 8 | + |
| 9 | +class DeviceOne implements Runnable { |
| 10 | + public void run() { |
| 11 | + JsonObject jsonObject = new JsonObject(); |
| 12 | + jsonObject.addProperty("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` |
| 13 | + jsonObject.addProperty("browser_version", "latest"); |
| 14 | + jsonObject.addProperty("os", "osx"); |
| 15 | + jsonObject.addProperty("os_version", "catalina"); |
| 16 | + jsonObject.addProperty("name", "Branded Google Chrome on Catalina"); |
| 17 | + jsonObject.addProperty("build", "playwright-build-2"); |
| 18 | + |
| 19 | + PlaywrightParallelTest deviceOne = new PlaywrightParallelTest(); |
| 20 | + deviceOne.executeTest(jsonObject); |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +class DeviceTwo implements Runnable { |
| 25 | + public void run() { |
| 26 | + JsonObject jsonObject = new JsonObject(); |
| 27 | + jsonObject.addProperty("browser", "edge"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` |
| 28 | + jsonObject.addProperty("browser_version", "latest"); |
| 29 | + jsonObject.addProperty("os", "osx"); |
| 30 | + jsonObject.addProperty("os_version", "catalina"); |
| 31 | + jsonObject.addProperty("name", "Branded Microsoft Edge on Catalina"); |
| 32 | + jsonObject.addProperty("build", "playwright-build-2"); |
| 33 | + PlaywrightParallelTest deviceTwo = new PlaywrightParallelTest(); |
| 34 | + deviceTwo.executeTest(jsonObject); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +class DeviceThree implements Runnable { |
| 39 | + public void run() { |
| 40 | + JsonObject jsonObject = new JsonObject(); |
| 41 | + jsonObject.addProperty("browser", "playwright-firefox"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` |
| 42 | + jsonObject.addProperty("os", "osx"); |
| 43 | + jsonObject.addProperty("os_version", "catalina"); |
| 44 | + jsonObject.addProperty("name", "Playwright firefox on Catalina"); |
| 45 | + jsonObject.addProperty("build", "playwright-build-2"); |
| 46 | + PlaywrightParallelTest deviceThree = new PlaywrightParallelTest(); |
| 47 | + deviceThree.executeTest(jsonObject); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +class DeviceFour implements Runnable { |
| 52 | + public void run() { |
| 53 | + JsonObject jsonObject = new JsonObject(); |
| 54 | + jsonObject.addProperty("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` |
| 55 | + jsonObject.addProperty("os", "osx"); |
| 56 | + jsonObject.addProperty("os_version", "catalina"); |
| 57 | + jsonObject.addProperty("name", "Playwright webkit on Catalina"); |
| 58 | + jsonObject.addProperty("build", "playwright-build-2"); |
| 59 | + PlaywrightParallelTest deviceFour = new PlaywrightParallelTest(); |
| 60 | + deviceFour.executeTest(jsonObject); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +class DeviceFive implements Runnable { |
| 65 | + public void run() { |
| 66 | + JsonObject jsonObject = new JsonObject(); |
| 67 | + jsonObject.addProperty("browser", "playwright-chromium"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` |
| 68 | + jsonObject.addProperty("os", "osx"); |
| 69 | + jsonObject.addProperty("os_version", "catalina"); |
| 70 | + jsonObject.addProperty("name", "Chrome on Win10"); |
| 71 | + jsonObject.addProperty("build", "playwright-build-2"); |
| 72 | + PlaywrightParallelTest deviceFive = new PlaywrightParallelTest(); |
| 73 | + deviceFive.executeTest(jsonObject); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +public class PlaywrightParallelTest { |
| 78 | + |
| 79 | + public static void main(String[] args) throws Exception { |
| 80 | + Thread threadOne = new Thread(new DeviceOne()); |
| 81 | + threadOne.start(); |
| 82 | + Thread threadTwo = new Thread(new DeviceTwo()); |
| 83 | + threadTwo.start(); |
| 84 | + Thread threadThree = new Thread(new DeviceThree()); |
| 85 | + threadThree.start(); |
| 86 | + Thread threadFour = new Thread(new DeviceFour()); |
| 87 | + threadFour.start(); |
| 88 | + Thread threadFive = new Thread(new DeviceFive()); |
| 89 | + threadFive.start(); |
| 90 | + } |
| 91 | + |
| 92 | + public void executeTest( JsonObject jsonObject ) { |
| 93 | + jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME"); |
| 94 | + jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY"); |
| 95 | + |
| 96 | + try (Playwright playwright = Playwright.create()) { |
| 97 | + BrowserType chromium = playwright.chromium(); |
| 98 | + String caps = URLEncoder.encode(jsonObject.toString(), "utf-8"); |
| 99 | + String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps; |
| 100 | + Browser browser = chromium.connect(ws_endpoint); |
| 101 | + Page page = browser.newPage(); |
| 102 | + page.navigate("https://www.google.co.in/"); |
| 103 | + Locator locator = page.locator("[aria-label='Search']"); |
| 104 | + locator.click(); |
| 105 | + page.fill("[aria-label='Search']", "BrowserStack"); |
| 106 | + page.locator("[aria-label='Google Search'] >> nth=0").click(); |
| 107 | + String title = page.title(); |
| 108 | + |
| 109 | + Object result; |
| 110 | + if (title.equals("BrowserStack - Google Search")) { |
| 111 | + // following line of code is responsible for marking the status of the test on BrowserStack as 'passed'. You can use this code in your after hook after each test |
| 112 | + result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}"); |
| 113 | + System.out.println(result); |
| 114 | + } else { |
| 115 | + result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}"); |
| 116 | + System.out.println(result); |
| 117 | + } |
| 118 | + browser.close(); |
| 119 | + } catch (UnsupportedEncodingException e) { |
| 120 | + System.out.println(e); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments