From bbf4dc3ba94c2eca14d792d36d27f6c12d2f24fd Mon Sep 17 00:00:00 2001 From: kamal-kaur04 <38219887+kamal-kaur04@users.noreply.github.com> Date: Tue, 15 Mar 2022 13:08:12 +0530 Subject: [PATCH 1/8] added get session details sample scripts --- .../PlaywrightSessionDetailsTest.cs | 68 +++++++++++++++++++ playwright-dotnet/Program.cs | 4 ++ playwright-dotnet/README.md | 5 ++ playwright-java/README.md | 1 + .../PlaywrightSessionDetailsTest.java | 66 ++++++++++++++++++ playwright-python/README.md | 5 ++ .../session-details-playwright-test.py | 51 ++++++++++++++ 7 files changed, 200 insertions(+) create mode 100644 playwright-dotnet/PlaywrightSessionDetailsTest.cs create mode 100644 playwright-java/src/test/java/com/browserstack/PlaywrightSessionDetailsTest.java create mode 100644 playwright-python/session-details-playwright-test.py diff --git a/playwright-dotnet/PlaywrightSessionDetailsTest.cs b/playwright-dotnet/PlaywrightSessionDetailsTest.cs new file mode 100644 index 0000000..aff0ce6 --- /dev/null +++ b/playwright-dotnet/PlaywrightSessionDetailsTest.cs @@ -0,0 +1,68 @@ +using Microsoft.Playwright; +using System.Threading.Tasks; +using System; +using System.Collections.Generic; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + + +class PlaywrightSessionDetailsTest +{ + public static async Task main(string[] args) + { + using var playwright = await Playwright.CreateAsync(); + + Dictionary browserstackOptions = new Dictionary(); + browserstackOptions.Add("name", "Playwright first sample test"); + browserstackOptions.Add("build", "playwright-dotnet-5"); + browserstackOptions.Add("os", "osx"); + browserstackOptions.Add("os_version", "catalina"); + browserstackOptions.Add("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` + browserstackOptions.Add("browserstack.username", "BROWSERSTACK_USERNAME"); + browserstackOptions.Add("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY"); + string capsJson = JsonConvert.SerializeObject(browserstackOptions); + string cdpUrl = "wss://cdp.browserstack.com/playwright?caps=" + Uri.EscapeDataString(capsJson); + + await using var browser = await playwright.Chromium.ConnectAsync(cdpUrl); + var page = await browser.NewPageAsync(); + try { + await page.GotoAsync("https://www.google.co.in/"); + await page.Locator("[aria-label='Search']").ClickAsync(); + await page.FillAsync("[aria-label='Search']", "BrowserStack"); + await page.Locator("[aria-label='Google Search'] >> nth=0").ClickAsync(); + var title = await page.TitleAsync(); + + if (title == "BrowserStack - Google Search") + { + // 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 + await MarkTestStatus("passed", "Title matched", page); + } else + { + await MarkTestStatus("failed", "Title did not match", page); + } + } + catch (Exception err) { + await MarkTestStatus("failed", err.Message, page); + } + Object sessionObject = await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\":\"getSessionDetails\"}"); + Console.WriteLine(sessionObject); + + // convert Object to String for parsing + string? json_resp = Convert.ToString(sessionObject); + + // parse the data + if (json_resp != null) + { + var session_details = JObject.Parse(json_resp); + + // print the session ID on IDE's console + Console.WriteLine(session_details["hashed_id"]); + } + + await browser.CloseAsync(); + } + + public static async Task MarkTestStatus(string status, string reason, IPage page) { + await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"" + status + "\", \"reason\": \"" + reason + "\"}}"); + } +} diff --git a/playwright-dotnet/Program.cs b/playwright-dotnet/Program.cs index 82b4ad9..1288510 100644 --- a/playwright-dotnet/Program.cs +++ b/playwright-dotnet/Program.cs @@ -29,6 +29,10 @@ public static async Task Main(string[] args) Console.WriteLine("Running Pixel Test"); await PlaywrightPixelTest.main(args); break; + case "sessiondetails": + Console.WriteLine("Getting Session Details Test"); + await PlaywrightSessionDetailsTest.main(args); + break; default: Console.WriteLine("Running Single Test by default"); await PlaywrightTest.main(args); diff --git a/playwright-dotnet/README.md b/playwright-dotnet/README.md index 1aa0413..3140380 100644 --- a/playwright-dotnet/README.md +++ b/playwright-dotnet/README.md @@ -13,6 +13,11 @@ - To run a single test, run `dotnet run single` - To run a parallel test, run command `dotnet run parallel` +- To run sessions on emulated devices, +`dotnet run iphonetest` or `dotnet run pixeltest` +You can specify any device name from thr below list: +https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json +- Run `dotnet run sessiondetails` to check how to get session details. ### Run sample test on privately hosted websites diff --git a/playwright-java/README.md b/playwright-java/README.md index 70dc770..803f81d 100644 --- a/playwright-java/README.md +++ b/playwright-java/README.md @@ -16,6 +16,7 @@ - To run parallel tests, run `mvn -Dexec.mainClass="com.browserstack.PlaywrightParallelTest" -Dexec.classpathScope=test test-compile exec:java ` +- Run `mvn -Dexec.mainClass="com.browserstack.PlaywrightSessionDetailsTest" -Dexec.classpathScope=test test-compile exec:java` to check how to get session details. ### Run sample test on privately hosted websites diff --git a/playwright-java/src/test/java/com/browserstack/PlaywrightSessionDetailsTest.java b/playwright-java/src/test/java/com/browserstack/PlaywrightSessionDetailsTest.java new file mode 100644 index 0000000..bb4ed58 --- /dev/null +++ b/playwright-java/src/test/java/com/browserstack/PlaywrightSessionDetailsTest.java @@ -0,0 +1,66 @@ +package com.browserstack; + +import com.google.gson.JsonObject; +import com.microsoft.playwright.*; +import com.google.gson.JsonParser; + +import java.net.URLEncoder; + +public class PlaywrightSessionDetailsTest { + public static void main(String[] args) { + try (Playwright playwright = Playwright.create()) { + JsonObject capabilitiesObject = new JsonObject(); + capabilitiesObject.addProperty("browser", "chrome"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` + capabilitiesObject.addProperty("browser_version", "latest"); + capabilitiesObject.addProperty("os", "osx"); + capabilitiesObject.addProperty("os_version", "catalina"); + capabilitiesObject.addProperty("name", "Playwright first single test"); + capabilitiesObject.addProperty("build", "playwright-java-5"); + capabilitiesObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME"); + capabilitiesObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY"); + + BrowserType chromium = playwright.chromium(); + String caps = URLEncoder.encode(capabilitiesObject.toString(), "utf-8"); + String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps; + Browser browser = chromium.connect(ws_endpoint); + Page page = browser.newPage(); + try { + page.navigate("https://www.google.co.in/"); + Locator locator = page.locator("[aria-label='Search']"); + locator.click(); + page.fill("[aria-label='Search']", "BrowserStack"); + page.locator("[aria-label='Google Search'] >> nth=0").click(); + String title = page.title(); + + if (title.equals("BrowserStack - Google Search")) { + // 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 + markTestStatus("passed", "Title matched", page); + } else { + markTestStatus("failed", "Title did not match", page); + } + + // store the JSON response in the Object class + Object response = page.evaluate("_ => {}", "browserstack_executor: {\"action\": \"getSessionDetails\"}"); + System.out.println(response); + + // parse the JSON response + JsonObject json = JsonParser.parseString((String) response).getAsJsonObject(); + + // store session ID in a variable + String sessionID = String.valueOf(json.get("hashed_id")); + + // print session ID in your IDE's console + System.out.println(sessionID); + } catch (Exception err) { + markTestStatus("failed", err.getMessage(), page); + } + browser.close(); + } catch (Exception err) { + System.out.println(err); + } + } + public static void markTestStatus(String status, String reason, Page page) { + Object result; + result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}"); + } +} diff --git a/playwright-python/README.md b/playwright-python/README.md index 6337517..29f58eb 100644 --- a/playwright-python/README.md +++ b/playwright-python/README.md @@ -19,6 +19,11 @@ - To run a single test, run `python single-playwright-test.py` - To run parallel tests, run `python parallel-playwright-test.py` +- To run sessions on emulated devices, +`python playwright-test-on-iphone.py` or `python playwright-test-on-pixel.py` +You can specify any device name from thr below list: +https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json +- Run `python session-details-playwright-test.py` to check how to get session details. ### Run sample test on privately hosted websites **Using Language Bindings** diff --git a/playwright-python/session-details-playwright-test.py b/playwright-python/session-details-playwright-test.py new file mode 100644 index 0000000..e49b936 --- /dev/null +++ b/playwright-python/session-details-playwright-test.py @@ -0,0 +1,51 @@ +import json +import urllib +from playwright.sync_api import sync_playwright + +desired_cap = { + 'browser': 'chrome', # allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` + 'browser_version': 'latest', # this capability is valid only for branded `chrome` and `edge` browsers and you can specify any browser version like `latest`, `latest-beta`, `latest-1` and so on. + 'os': 'osx', + 'os_version': 'catalina', + 'name': 'Branded Google Chrome on Catalina', + 'build': 'playwright-python-5', + 'browserstack.username': 'BROWSERSTACK_USERNAME', + 'browserstack.accessKey': 'BROWSERSTACK_ACCESS_KEY' +} + +def run_session(playwright): + cdpUrl = 'wss://cdp.browserstack.com/playwright?caps=' + urllib.parse.quote(json.dumps(desired_cap)) + browser = playwright.chromium.connect(cdpUrl) + page = browser.new_page() + try: + page.goto("https://www.google.co.in/") + page.fill("[aria-label='Search']", 'Browserstack') + locator = page.locator("[aria-label='Google Search'] >> nth=0") + locator.click() + title = page.title() + + if title == "Browserstack - Google Search": + # 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 + mark_test_status("passed", "Title matched", page) + else: + mark_test_status("failed", "Title did not match", page) + except Exception as err: + mark_test_status("failed", str(err), page) + + # get details of the session + response = page.evaluate("_=> {}", 'browserstack_executor: {"action": "getSessionDetails"}') + print(response) + + jsonResponse = json.loads(response) + + # print the session ID in the IDE's console + print(jsonResponse["hashed_id"]) + + browser.close() + +def mark_test_status(status, reason, page): + page.evaluate("_ => {}", "browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\""+ status + "\", \"reason\": \"" + reason + "\"}}"); + +with sync_playwright() as playwright: + run_session(playwright) + From 371a803be6547a960ed41eba99224c6c624db821 Mon Sep 17 00:00:00 2001 From: kamal-kaur04 <38219887+kamal-kaur04@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:24:56 +0530 Subject: [PATCH 2/8] improved wait strategy post pressing ENTER key --- playwright-dotnet/PlaywrightIPhoneTest.cs | 2 +- playwright-dotnet/PlaywrightPixelTest.cs | 2 +- playwright-java/README.md | 10 ++++ .../browserstack/PlaywrightIPhoneTest.java | 53 ++++++++++++++++++ .../com/browserstack/PlaywrightPixelTest.java | 54 +++++++++++++++++++ .../playwright-test-on-iphone.py | 2 +- playwright-python/playwright-test-on-pixel.py | 2 +- 7 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 playwright-java/src/test/java/com/browserstack/PlaywrightIPhoneTest.java create mode 100644 playwright-java/src/test/java/com/browserstack/PlaywrightPixelTest.java diff --git a/playwright-dotnet/PlaywrightIPhoneTest.cs b/playwright-dotnet/PlaywrightIPhoneTest.cs index 4d6ffb9..056f139 100644 --- a/playwright-dotnet/PlaywrightIPhoneTest.cs +++ b/playwright-dotnet/PlaywrightIPhoneTest.cs @@ -29,7 +29,7 @@ public static async Task main(string[] args) await page.Locator("[aria-label='Search']").ClickAsync(); await page.FillAsync("[aria-label='Search']", "BrowserStack"); await page.Keyboard.PressAsync("Enter"); - await page.WaitForTimeoutAsync(1000); + await page.Locator("[aria-current='page']").WaitForAsync(); var title = await page.TitleAsync(); if (title == "BrowserStack - Google Search") diff --git a/playwright-dotnet/PlaywrightPixelTest.cs b/playwright-dotnet/PlaywrightPixelTest.cs index f95ec9c..20bfafa 100644 --- a/playwright-dotnet/PlaywrightPixelTest.cs +++ b/playwright-dotnet/PlaywrightPixelTest.cs @@ -29,7 +29,7 @@ public static async Task main(string[] args) await page.Locator("[aria-label='Search']").ClickAsync(); await page.FillAsync("[aria-label='Search']", "BrowserStack"); await page.Keyboard.PressAsync("Enter"); - await page.WaitForTimeoutAsync(1000); + await page.Locator("[aria-current='page']").WaitForAsync(); var title = await page.TitleAsync(); if (title == "BrowserStack - Google Search") diff --git a/playwright-java/README.md b/playwright-java/README.md index 803f81d..f081bf8 100644 --- a/playwright-java/README.md +++ b/playwright-java/README.md @@ -16,6 +16,16 @@ - To run parallel tests, run `mvn -Dexec.mainClass="com.browserstack.PlaywrightParallelTest" -Dexec.classpathScope=test test-compile exec:java ` +- To run sessions on emulated devices, + ``` + mvn -Dexec.mainClass="com.browserstack.PlaywrightIPhoneTest" -Dexec.classpathScope=test test-compile exec:java + ``` + or + ``` + mvn -Dexec.mainClass="com.browserstack.PlaywrightPixelTest" -Dexec.classpathScope=test test-compile exec:java + ``` +You can specify contextOptions() from thr below list: +https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json - Run `mvn -Dexec.mainClass="com.browserstack.PlaywrightSessionDetailsTest" -Dexec.classpathScope=test test-compile exec:java` to check how to get session details. ### Run sample test on privately hosted websites diff --git a/playwright-java/src/test/java/com/browserstack/PlaywrightIPhoneTest.java b/playwright-java/src/test/java/com/browserstack/PlaywrightIPhoneTest.java new file mode 100644 index 0000000..5c3b780 --- /dev/null +++ b/playwright-java/src/test/java/com/browserstack/PlaywrightIPhoneTest.java @@ -0,0 +1,53 @@ +package com.browserstack; + +import com.google.gson.JsonObject; +import com.microsoft.playwright.*; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +public class PlaywrightIPhoneTest { + public static void main(String[] args) { + try (Playwright playwright = Playwright.create()) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` + jsonObject.addProperty("browser_version", "latest"); + jsonObject.addProperty("name", "Test on Playwright emulated Devices"); + jsonObject.addProperty("build", "playwright-java-4"); + jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME"); + jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY"); + + BrowserType chromium = playwright.chromium(); + String caps = URLEncoder.encode(jsonObject.toString(), "utf-8"); + String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps; + + Browser browser = chromium.connect(ws_endpoint); + BrowserContext context = browser.newContext(new Browser.NewContextOptions() + .setUserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1") + .setViewportSize(375, 812) + .setDeviceScaleFactor(3) + .setIsMobile(true) + .setHasTouch(true)); + + Page page = context.newPage(); + page.navigate("https://www.google.co.in/"); + Locator locator = page.locator("[aria-label='Search']"); + locator.click(); + page.fill("[aria-label='Search']", "BrowserStack"); + page.keyboard().press("Enter"); + page.locator("[aria-current='page']").waitFor(); + String title = page.title(); + + Object result; + if (title.equals("BrowserStack - Google Search")) { + // 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 + result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}"); + } else { + result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}"); + } + + browser.close(); + } catch (UnsupportedEncodingException e) { + System.out.println(e); + } + } +} diff --git a/playwright-java/src/test/java/com/browserstack/PlaywrightPixelTest.java b/playwright-java/src/test/java/com/browserstack/PlaywrightPixelTest.java new file mode 100644 index 0000000..91268ec --- /dev/null +++ b/playwright-java/src/test/java/com/browserstack/PlaywrightPixelTest.java @@ -0,0 +1,54 @@ +package com.browserstack; + +import com.google.gson.JsonObject; +import com.microsoft.playwright.*; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +public class PlaywrightPixelTest { + public static void main(String[] args) { + try (Playwright playwright = Playwright.create()) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` + jsonObject.addProperty("browser_version", "latest"); + jsonObject.addProperty("name", "Test on Playwright emulated Devices"); + jsonObject.addProperty("build", "playwright-java-4"); + jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME"); + jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY"); + + BrowserType chromium = playwright.chromium(); + String caps = URLEncoder.encode(jsonObject.toString(), "utf-8"); + String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps; + + Browser browser = chromium.connect(ws_endpoint); + BrowserContext context = browser.newContext(new Browser.NewContextOptions() + .setUserAgent("Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4943.0 Mobile Safari/537.36") + .setViewportSize(393, 727) + .setScreenSize(393, 851) + .setDeviceScaleFactor(3) + .setIsMobile(true) + .setHasTouch(true)); + + Page page = context.newPage(); + page.navigate("https://www.google.co.in/"); + Locator locator = page.locator("[aria-label='Search']"); + locator.click(); + page.fill("[aria-label='Search']", "BrowserStack"); + page.keyboard().press("Enter"); + page.locator("[aria-current='page']").waitFor(); + String title = page.title(); + + Object result; + if (title.equals("BrowserStack - Google Search")) { + // 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 + result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}"); + } else { + result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}"); + } + + browser.close(); + } catch (UnsupportedEncodingException e) { + System.out.println(e); + } + } +} diff --git a/playwright-python/playwright-test-on-iphone.py b/playwright-python/playwright-test-on-iphone.py index 44c6d82..2b9f5a6 100644 --- a/playwright-python/playwright-test-on-iphone.py +++ b/playwright-python/playwright-test-on-iphone.py @@ -25,7 +25,7 @@ def run_session(playwright): page.goto("https://www.google.co.in/") page.fill("[aria-label='Search']", 'Browserstack') page.keyboard.press('Enter') - page.wait_for_timeout(1000) + page.locator("[aria-current='page']").wait_for(); title = page.title() if title == "Browserstack - Google Search": diff --git a/playwright-python/playwright-test-on-pixel.py b/playwright-python/playwright-test-on-pixel.py index ce115df..b8579d6 100644 --- a/playwright-python/playwright-test-on-pixel.py +++ b/playwright-python/playwright-test-on-pixel.py @@ -25,7 +25,7 @@ def run_session(playwright): page.goto("https://www.google.co.in/") page.fill("[aria-label='Search']", 'Browserstack') page.keyboard.press('Enter') - page.wait_for_timeout(1000) + page.locator("[aria-current='page']").wait_for(); title = page.title() if title == "Browserstack - Google Search": From f0117f5caf6a5a22fa3d01d82e67f5e52e620e3a Mon Sep 17 00:00:00 2001 From: kamal-kaur04 <38219887+kamal-kaur04@users.noreply.github.com> Date: Tue, 15 Mar 2022 15:02:38 +0530 Subject: [PATCH 3/8] minor fix --- .../browserstack/PlaywrightIPhoneTest.java | 50 +++++++++++-------- .../com/browserstack/PlaywrightPixelTest.java | 50 +++++++++++-------- 2 files changed, 56 insertions(+), 44 deletions(-) diff --git a/playwright-java/src/test/java/com/browserstack/PlaywrightIPhoneTest.java b/playwright-java/src/test/java/com/browserstack/PlaywrightIPhoneTest.java index 5c3b780..517a31a 100644 --- a/playwright-java/src/test/java/com/browserstack/PlaywrightIPhoneTest.java +++ b/playwright-java/src/test/java/com/browserstack/PlaywrightIPhoneTest.java @@ -8,16 +8,16 @@ public class PlaywrightIPhoneTest { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` - jsonObject.addProperty("browser_version", "latest"); - jsonObject.addProperty("name", "Test on Playwright emulated Devices"); - jsonObject.addProperty("build", "playwright-java-4"); - jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME"); - jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY"); + JsonObject capabilitiesObject = new JsonObject(); + capabilitiesObject.addProperty("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` + capabilitiesObject.addProperty("browser_version", "latest"); + capabilitiesObject.addProperty("name", "Test on Playwright emulated Devices"); + capabilitiesObject.addProperty("build", "playwright-java-4"); + capabilitiesObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME"); + capabilitiesObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY"); BrowserType chromium = playwright.chromium(); - String caps = URLEncoder.encode(jsonObject.toString(), "utf-8"); + String caps = URLEncoder.encode(capabilitiesObject.toString(), "utf-8"); String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps; Browser browser = chromium.connect(ws_endpoint); @@ -29,25 +29,31 @@ public static void main(String[] args) { .setHasTouch(true)); Page page = context.newPage(); - page.navigate("https://www.google.co.in/"); - Locator locator = page.locator("[aria-label='Search']"); - locator.click(); - page.fill("[aria-label='Search']", "BrowserStack"); - page.keyboard().press("Enter"); - page.locator("[aria-current='page']").waitFor(); - String title = page.title(); + try { + page.navigate("https://www.google.co.in/"); + Locator locator = page.locator("[aria-label='Search']"); + locator.click(); + page.fill("[aria-label='Search']", "BrowserStack"); + page.keyboard().press("Enter"); + page.locator("[aria-current='page']").waitFor(); + String title = page.title(); - Object result; - if (title.equals("BrowserStack - Google Search")) { - // 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 - result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}"); - } else { - result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}"); + if (title.equals("BrowserStack - Google Search")) { + // 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 + markTestStatus("passed", "Title matched", page); + } else { + markTestStatus("failed", "Title did not match", page); + } + } catch (Exception err) { + markTestStatus("failed", err.getMessage(), page); } - browser.close(); } catch (UnsupportedEncodingException e) { System.out.println(e); } } + public static void markTestStatus(String status, String reason, Page page) { + Object result; + result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}"); + } } diff --git a/playwright-java/src/test/java/com/browserstack/PlaywrightPixelTest.java b/playwright-java/src/test/java/com/browserstack/PlaywrightPixelTest.java index 91268ec..8ab5aed 100644 --- a/playwright-java/src/test/java/com/browserstack/PlaywrightPixelTest.java +++ b/playwright-java/src/test/java/com/browserstack/PlaywrightPixelTest.java @@ -8,16 +8,16 @@ public class PlaywrightPixelTest { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { - JsonObject jsonObject = new JsonObject(); - jsonObject.addProperty("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` - jsonObject.addProperty("browser_version", "latest"); - jsonObject.addProperty("name", "Test on Playwright emulated Devices"); - jsonObject.addProperty("build", "playwright-java-4"); - jsonObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME"); - jsonObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY"); + JsonObject capabilitiesObject = new JsonObject(); + capabilitiesObject.addProperty("browser", "playwright-webkit"); // allowed browsers are `chrome`, `edge`, `playwright-chromium`, `playwright-firefox` and `playwright-webkit` + capabilitiesObject.addProperty("browser_version", "latest"); + capabilitiesObject.addProperty("name", "Test on Playwright emulated Devices"); + capabilitiesObject.addProperty("build", "playwright-java-4"); + capabilitiesObject.addProperty("browserstack.username", "BROWSERSTACK_USERNAME"); + capabilitiesObject.addProperty("browserstack.accessKey", "BROWSERSTACK_ACCESS_KEY"); BrowserType chromium = playwright.chromium(); - String caps = URLEncoder.encode(jsonObject.toString(), "utf-8"); + String caps = URLEncoder.encode(capabilitiesObject.toString(), "utf-8"); String ws_endpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps; Browser browser = chromium.connect(ws_endpoint); @@ -30,25 +30,31 @@ public static void main(String[] args) { .setHasTouch(true)); Page page = context.newPage(); - page.navigate("https://www.google.co.in/"); - Locator locator = page.locator("[aria-label='Search']"); - locator.click(); - page.fill("[aria-label='Search']", "BrowserStack"); - page.keyboard().press("Enter"); - page.locator("[aria-current='page']").waitFor(); - String title = page.title(); + try { + page.navigate("https://www.google.co.in/"); + Locator locator = page.locator("[aria-label='Search']"); + locator.click(); + page.fill("[aria-label='Search']", "BrowserStack"); + page.keyboard().press("Enter"); + page.locator("[aria-current='page']").waitFor(); + String title = page.title(); - Object result; - if (title.equals("BrowserStack - Google Search")) { - // 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 - result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"passed\", \"reason\": \"Title matched\"}}"); - } else { - result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"failed\", \"reason\": \"Title did not matched\"}}"); + if (title.equals("BrowserStack - Google Search")) { + // 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 + markTestStatus("passed", "Title matched", page); + } else { + markTestStatus("passed", "Title did not match", page); + } + } catch (Exception err) { + markTestStatus("failed", err.getMessage(), page); } - browser.close(); } catch (UnsupportedEncodingException e) { System.out.println(e); } } + public static void markTestStatus(String status, String reason, Page page) { + Object result; + result = page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"" + status + "\", \"reason\": \"" + reason + "\"}}"); + } } From 6bb92e2e5c53d983c0e1d4f53d638dfc3466d183 Mon Sep 17 00:00:00 2001 From: kamal-kaur04 <38219887+kamal-kaur04@users.noreply.github.com> Date: Tue, 15 Mar 2022 19:19:18 +0530 Subject: [PATCH 4/8] minor fix --- playwright-dotnet/README.md | 2 +- playwright-java/README.md | 2 +- playwright-python/README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/playwright-dotnet/README.md b/playwright-dotnet/README.md index 3140380..e0a2a7b 100644 --- a/playwright-dotnet/README.md +++ b/playwright-dotnet/README.md @@ -15,7 +15,7 @@ - To run a parallel test, run command `dotnet run parallel` - To run sessions on emulated devices, `dotnet run iphonetest` or `dotnet run pixeltest` -You can specify any device name from thr below list: +You can specify any device name from thebelow list: https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json - Run `dotnet run sessiondetails` to check how to get session details. diff --git a/playwright-java/README.md b/playwright-java/README.md index f081bf8..d2823bd 100644 --- a/playwright-java/README.md +++ b/playwright-java/README.md @@ -24,7 +24,7 @@ ``` mvn -Dexec.mainClass="com.browserstack.PlaywrightPixelTest" -Dexec.classpathScope=test test-compile exec:java ``` -You can specify contextOptions() from thr below list: +You can specify contextOptions() from thebelow list: https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json - Run `mvn -Dexec.mainClass="com.browserstack.PlaywrightSessionDetailsTest" -Dexec.classpathScope=test test-compile exec:java` to check how to get session details. diff --git a/playwright-python/README.md b/playwright-python/README.md index 29f58eb..6b551ca 100644 --- a/playwright-python/README.md +++ b/playwright-python/README.md @@ -21,7 +21,7 @@ - To run parallel tests, run `python parallel-playwright-test.py` - To run sessions on emulated devices, `python playwright-test-on-iphone.py` or `python playwright-test-on-pixel.py` -You can specify any device name from thr below list: +You can specify any device name from thebelow list: https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json - Run `python session-details-playwright-test.py` to check how to get session details. From f960452a622ca446f3896fc7d9f5a803c3eddedb Mon Sep 17 00:00:00 2001 From: kamal-kaur04 <38219887+kamal-kaur04@users.noreply.github.com> Date: Tue, 15 Mar 2022 19:20:21 +0530 Subject: [PATCH 5/8] minor fix --- playwright-dotnet/README.md | 2 +- playwright-java/README.md | 2 +- playwright-python/README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/playwright-dotnet/README.md b/playwright-dotnet/README.md index e0a2a7b..d3afc0e 100644 --- a/playwright-dotnet/README.md +++ b/playwright-dotnet/README.md @@ -15,7 +15,7 @@ - To run a parallel test, run command `dotnet run parallel` - To run sessions on emulated devices, `dotnet run iphonetest` or `dotnet run pixeltest` -You can specify any device name from thebelow list: +You can specify any device name from the below list: https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json - Run `dotnet run sessiondetails` to check how to get session details. diff --git a/playwright-java/README.md b/playwright-java/README.md index d2823bd..a8d3ced 100644 --- a/playwright-java/README.md +++ b/playwright-java/README.md @@ -24,7 +24,7 @@ ``` mvn -Dexec.mainClass="com.browserstack.PlaywrightPixelTest" -Dexec.classpathScope=test test-compile exec:java ``` -You can specify contextOptions() from thebelow list: +You can specify contextOptions() from the below list: https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json - Run `mvn -Dexec.mainClass="com.browserstack.PlaywrightSessionDetailsTest" -Dexec.classpathScope=test test-compile exec:java` to check how to get session details. diff --git a/playwright-python/README.md b/playwright-python/README.md index 6b551ca..ffe12ea 100644 --- a/playwright-python/README.md +++ b/playwright-python/README.md @@ -21,7 +21,7 @@ - To run parallel tests, run `python parallel-playwright-test.py` - To run sessions on emulated devices, `python playwright-test-on-iphone.py` or `python playwright-test-on-pixel.py` -You can specify any device name from thebelow list: +You can specify any device name from the below list: https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json - Run `python session-details-playwright-test.py` to check how to get session details. From f944decc1e5651af718eedc44fe4ba7bce462493 Mon Sep 17 00:00:00 2001 From: kamal-kaur04 <38219887+kamal-kaur04@users.noreply.github.com> Date: Wed, 16 Mar 2022 12:00:40 +0530 Subject: [PATCH 6/8] moved get session details in try block --- .../PlaywrightSessionDetailsTest.cs | 29 +++++++++---------- .../session-details-playwright-test.py | 19 ++++++------ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/playwright-dotnet/PlaywrightSessionDetailsTest.cs b/playwright-dotnet/PlaywrightSessionDetailsTest.cs index aff0ce6..795ffdf 100644 --- a/playwright-dotnet/PlaywrightSessionDetailsTest.cs +++ b/playwright-dotnet/PlaywrightSessionDetailsTest.cs @@ -40,25 +40,24 @@ public static async Task main(string[] args) { await MarkTestStatus("failed", "Title did not match", page); } + Object sessionObject = await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\":\"getSessionDetails\"}"); + Console.WriteLine(sessionObject); + + // convert Object to String for parsing + string? json_resp = Convert.ToString(sessionObject); + + // parse the data + if (json_resp != null) + { + var session_details = JObject.Parse(json_resp); + + // print the session ID on IDE's console + Console.WriteLine(session_details["hashed_id"]); + } } catch (Exception err) { await MarkTestStatus("failed", err.Message, page); } - Object sessionObject = await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\":\"getSessionDetails\"}"); - Console.WriteLine(sessionObject); - - // convert Object to String for parsing - string? json_resp = Convert.ToString(sessionObject); - - // parse the data - if (json_resp != null) - { - var session_details = JObject.Parse(json_resp); - - // print the session ID on IDE's console - Console.WriteLine(session_details["hashed_id"]); - } - await browser.CloseAsync(); } diff --git a/playwright-python/session-details-playwright-test.py b/playwright-python/session-details-playwright-test.py index e49b936..3ad5037 100644 --- a/playwright-python/session-details-playwright-test.py +++ b/playwright-python/session-details-playwright-test.py @@ -29,18 +29,19 @@ def run_session(playwright): mark_test_status("passed", "Title matched", page) else: mark_test_status("failed", "Title did not match", page) + + # get details of the session + response = page.evaluate("_=> {}", 'browserstack_executor: {"action": "getSessionDetails"}') + print(response) + + jsonResponse = json.loads(response) + + # print the session ID in the IDE's console + print(jsonResponse["hashed_id"]) + except Exception as err: mark_test_status("failed", str(err), page) - # get details of the session - response = page.evaluate("_=> {}", 'browserstack_executor: {"action": "getSessionDetails"}') - print(response) - - jsonResponse = json.loads(response) - - # print the session ID in the IDE's console - print(jsonResponse["hashed_id"]) - browser.close() def mark_test_status(status, reason, page): From 6b416ca45100f47bcafea862c9ef2969baf002ec Mon Sep 17 00:00:00 2001 From: kamal-kaur04 <38219887+kamal-kaur04@users.noreply.github.com> Date: Wed, 16 Mar 2022 12:22:55 +0530 Subject: [PATCH 7/8] minor updates --- playwright-dotnet/PlaywrightSessionDetailsTest.cs | 5 ++--- .../com/browserstack/PlaywrightSessionDetailsTest.java | 10 +++------- playwright-python/session-details-playwright-test.py | 7 +++---- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/playwright-dotnet/PlaywrightSessionDetailsTest.cs b/playwright-dotnet/PlaywrightSessionDetailsTest.cs index 795ffdf..560be95 100644 --- a/playwright-dotnet/PlaywrightSessionDetailsTest.cs +++ b/playwright-dotnet/PlaywrightSessionDetailsTest.cs @@ -41,7 +41,6 @@ public static async Task main(string[] args) await MarkTestStatus("failed", "Title did not match", page); } Object sessionObject = await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\":\"getSessionDetails\"}"); - Console.WriteLine(sessionObject); // convert Object to String for parsing string? json_resp = Convert.ToString(sessionObject); @@ -51,8 +50,8 @@ public static async Task main(string[] args) { var session_details = JObject.Parse(json_resp); - // print the session ID on IDE's console - Console.WriteLine(session_details["hashed_id"]); + // print the session Details on IDE's console + Console.WriteLine("GetSessionDetails response: \n" + session_details); } } catch (Exception err) { diff --git a/playwright-java/src/test/java/com/browserstack/PlaywrightSessionDetailsTest.java b/playwright-java/src/test/java/com/browserstack/PlaywrightSessionDetailsTest.java index bb4ed58..f56bf9b 100644 --- a/playwright-java/src/test/java/com/browserstack/PlaywrightSessionDetailsTest.java +++ b/playwright-java/src/test/java/com/browserstack/PlaywrightSessionDetailsTest.java @@ -41,16 +41,12 @@ public static void main(String[] args) { // store the JSON response in the Object class Object response = page.evaluate("_ => {}", "browserstack_executor: {\"action\": \"getSessionDetails\"}"); - System.out.println(response); // parse the JSON response - JsonObject json = JsonParser.parseString((String) response).getAsJsonObject(); + JsonObject sessionDetails = JsonParser.parseString((String) response).getAsJsonObject(); - // store session ID in a variable - String sessionID = String.valueOf(json.get("hashed_id")); - - // print session ID in your IDE's console - System.out.println(sessionID); + // print session Details in your IDE's console + System.out.println("GetSessionDetails response: \n" + sessionDetails); } catch (Exception err) { markTestStatus("failed", err.getMessage(), page); } diff --git a/playwright-python/session-details-playwright-test.py b/playwright-python/session-details-playwright-test.py index 3ad5037..dd11cca 100644 --- a/playwright-python/session-details-playwright-test.py +++ b/playwright-python/session-details-playwright-test.py @@ -32,12 +32,11 @@ def run_session(playwright): # get details of the session response = page.evaluate("_=> {}", 'browserstack_executor: {"action": "getSessionDetails"}') - print(response) - jsonResponse = json.loads(response) + session_details= json.loads(response) - # print the session ID in the IDE's console - print(jsonResponse["hashed_id"]) + # print the session Details in the IDE's console + print("GetSessionDetails response: \n", session_details) except Exception as err: mark_test_status("failed", str(err), page) From 59067838329cc2505425094b98032c876b51cecc Mon Sep 17 00:00:00 2001 From: kamal-kaur04 <38219887+kamal-kaur04@users.noreply.github.com> Date: Wed, 16 Mar 2022 12:24:56 +0530 Subject: [PATCH 8/8] minor updates --- playwright-dotnet/PlaywrightSessionDetailsTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/playwright-dotnet/PlaywrightSessionDetailsTest.cs b/playwright-dotnet/PlaywrightSessionDetailsTest.cs index 560be95..10a2ae5 100644 --- a/playwright-dotnet/PlaywrightSessionDetailsTest.cs +++ b/playwright-dotnet/PlaywrightSessionDetailsTest.cs @@ -48,10 +48,10 @@ public static async Task main(string[] args) // parse the data if (json_resp != null) { - var session_details = JObject.Parse(json_resp); + var sessionDetails = JObject.Parse(json_resp); // print the session Details on IDE's console - Console.WriteLine("GetSessionDetails response: \n" + session_details); + Console.WriteLine("GetSessionDetails response: \n" + sessionDetails); } } catch (Exception err) {