|
| 1 | +package com.browserstack; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.microsoft.playwright.*; |
| 5 | +import com.google.gson.JsonParser; |
| 6 | + |
| 7 | +import java.net.URLEncoder; |
| 8 | + |
| 9 | +public class PlaywrightSessionDetailsTest { |
| 10 | + public static void main(String[] args) { |
| 11 | + try (Playwright playwright = Playwright.create()) { |
| 12 | + JsonObject capabilitiesObject = new JsonObject(); |
| 13 | + capabilitiesObject.addProperty("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` |
| 14 | + capabilitiesObject.addProperty("browser_version", "latest"); |
| 15 | + capabilitiesObject.addProperty("os", "osx"); |
| 16 | + capabilitiesObject.addProperty("os_version", "catalina"); |
| 17 | + capabilitiesObject.addProperty("name", "Playwright first single test"); |
| 18 | + capabilitiesObject.addProperty("build", "playwright-java-5"); |
| 19 | + capabilitiesObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME"); |
| 20 | + capabilitiesObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY"); |
| 21 | + |
| 22 | + BrowserType chromium = playwright.chromium(); |
| 23 | + String caps = URLEncoder.encode(capabilitiesObject.toString(), "utf-8"); |
| 24 | + String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps; |
| 25 | + Browser browser = chromium.connect(ws_endpoint); |
| 26 | + Page page = browser.newPage(); |
| 27 | + try { |
| 28 | + page.navigate("https://www.google.co.in/"); |
| 29 | + Locator locator = page.locator("[aria-label='Search']"); |
| 30 | + locator.click(); |
| 31 | + page.fill("[aria-label='Search']", "BrowserStack"); |
| 32 | + page.locator("[aria-label='Google Search'] >> nth=0").click(); |
| 33 | + String title = page.title(); |
| 34 | + |
| 35 | + if (title.equals("BrowserStack - Google Search")) { |
| 36 | + // 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 |
| 37 | + markTestStatus("passed", "Title matched", page); |
| 38 | + } else { |
| 39 | + markTestStatus("failed", "Title did not match", page); |
| 40 | + } |
| 41 | + |
| 42 | + // store the JSON response in the Object class |
| 43 | + Object response = page.evaluate("_ => {}", "browserstack_executor: {\"action\": \"getSessionDetails\"}"); |
| 44 | + System.out.println(response); |
| 45 | + |
| 46 | + // parse the JSON response |
| 47 | + JsonObject json = JsonParser.parseString((String) response).getAsJsonObject(); |
| 48 | + |
| 49 | + // store session ID in a variable |
| 50 | + String sessionID = String.valueOf(json.get("hashed_id")); |
| 51 | + |
| 52 | + // print session ID in your IDE's console |
| 53 | + System.out.println(sessionID); |
| 54 | + } catch (Exception err) { |
| 55 | + markTestStatus("failed", err.getMessage(), page); |
| 56 | + } |
| 57 | + browser.close(); |
| 58 | + } catch (Exception err) { |
| 59 | + System.out.println(err); |
| 60 | + } |
| 61 | + } |
| 62 | + public static void markTestStatus(String status, String reason, Page page) { |
| 63 | + Object result; |
| 64 | + result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}"); |
| 65 | + } |
| 66 | +} |
0 commit comments