-
Notifications
You must be signed in to change notification settings - Fork 43
added get session details sample scripts #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bbf4dc3
added get session details sample scripts
kamal-kaur04 371a803
improved wait strategy post pressing ENTER key
kamal-kaur04 f0117f5
minor fix
kamal-kaur04 6bb92e2
minor fix
kamal-kaur04 f960452
minor fix
kamal-kaur04 f944dec
moved get session details in try block
kamal-kaur04 6b416ca
minor updates
kamal-kaur04 5906783
minor updates
kamal-kaur04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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<string, string> browserstackOptions = new Dictionary<string, string>(); | ||
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); | ||
} | ||
Object sessionObject = await page.EvaluateAsync("_ => {}", "browserstack_executor: {\"action\":\"getSessionDetails\"}"); | ||
|
||
// convert Object to String for parsing | ||
string? json_resp = Convert.ToString(sessionObject); | ||
|
||
// parse the data | ||
if (json_resp != null) | ||
{ | ||
var sessionDetails = JObject.Parse(json_resp); | ||
|
||
// print the session Details on IDE's console | ||
Console.WriteLine("GetSessionDetails response: \n" + sessionDetails); | ||
} | ||
} | ||
catch (Exception err) { | ||
await MarkTestStatus("failed", err.Message, page); | ||
} | ||
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 + "\"}}"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
playwright-java/src/test/java/com/browserstack/PlaywrightIPhoneTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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 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(capabilitiesObject.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(); | ||
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(); | ||
|
||
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 + "\"}}"); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
playwright-java/src/test/java/com/browserstack/PlaywrightPixelTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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 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(capabilitiesObject.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(); | ||
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(); | ||
|
||
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 + "\"}}"); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
playwright-java/src/test/java/com/browserstack/PlaywrightSessionDetailsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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\"}"); | ||
|
||
// parse the JSON response | ||
JsonObject sessionDetails = JsonParser.parseString((String) response).getAsJsonObject(); | ||
|
||
// print session Details in your IDE's console | ||
System.out.println("GetSessionDetails response: \n" + sessionDetails); | ||
} 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 + "\"}}"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
# get details of the session | ||
response = page.evaluate("_=> {}", 'browserstack_executor: {"action": "getSessionDetails"}') | ||
|
||
session_details= json.loads(response) | ||
|
||
# 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) | ||
|
||
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) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we fetch this viewport from devices json based on device name?