Skip to content

Commit 80afc45

Browse files
Ankit098francisf
authored andcommitted
add: sdk branch
1 parent f2b9052 commit 80afc45

File tree

7 files changed

+89
-130
lines changed

7 files changed

+89
-130
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
.project
44
.settings/
55
target/
6-
browserstack.err
6+
browserstack.err

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
```
1414
## Running your tests
1515
* To run a sample tests, run `mvn test -P sample-test`
16-
* To run sample tests with local, run `mvn test -P sample-local-test`
1716
* Update `<parallel.count>` in `pom.xml` to set the number of parallel threads
1817

1918
Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)

browserstack.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# =============================
2+
# Set BrowserStack Credentials
3+
# =============================
4+
# Add your BrowserStack userName and accessKey here or set BROWSERSTACK_USERNAME and
5+
# BROWSERSTACK_ACCESS_KEY as env variables
6+
userName: YOUR_USERNAME
7+
accessKey: YOUR_ACCESS_KEY
8+
9+
# ======================
10+
# BrowserStack Reporting
11+
# ======================
12+
# The following capabilities are used to set up reporting on BrowserStack:
13+
# Set 'projectName' to the name of your project. Example, Marketing Website
14+
projectName: BrowserStack Samples
15+
# Set `buildName` as the name of the job / testsuite being run
16+
buildName: browserstack build
17+
# `buildIdentifier` is a unique id to differentiate every execution that gets appended to
18+
# buildName. Choose your buildIdentifier format from the available expressions:
19+
# ${BUILD_NUMBER} (Default): Generates an incremental counter with every execution
20+
# ${DATE_TIME}: Generates a Timestamp with every execution. Eg. 05-Nov-19:30
21+
# Read more about buildIdentifiers here -> https://www.browserstack.com/docs/automate/selenium/organize-tests
22+
buildIdentifier: '#${BUILD_NUMBER}' # Supports strings along with either/both ${expression}
23+
# Set `framework` of your test suite. Example, `testng`, `cucumber`, `cucumber-testng`
24+
# This property is needed to send test context to BrowserStack (test name, status)
25+
framework: junit5
26+
27+
source: java-playwright-browserstack:sample-sdk:v1.0
28+
29+
# =======================================
30+
# Platforms (Browsers / Devices to test)
31+
# =======================================
32+
# Platforms object contains all the browser / device combinations you want to test on.
33+
# Entire list available here -> (https://www.browserstack.com/list-of-browsers-and-platforms/automate)
34+
platforms:
35+
- os: OS X
36+
osVersion: Big Sur
37+
browserName: Chrome
38+
browserVersion: latest
39+
- os: Windows
40+
osVersion: 10
41+
browserName: Edge
42+
browserVersion: latest
43+
- deviceName: Samsung Galaxy S22 Ultra
44+
browserName: chrome # Try 'samsung' for Samsung browser
45+
osVersion: 12.0
46+
47+
# =======================
48+
# Parallels per Platform
49+
# =======================
50+
# The number of parallel threads to be used for each platform set.
51+
# BrowserStack's SDK runner will select the best strategy based on the configured value
52+
#
53+
# Example 1 - If you have configured 3 platforms and set `parallelsPerPlatform` as 2, a total of 6 (2 * 3) parallel threads will be used on BrowserStack
54+
#
55+
# Example 2 - If you have configured 1 platform and set `parallelsPerPlatform` as 5, a total of 5 (1 * 5) parallel threads will be used on BrowserStack
56+
parallelsPerPlatform: 1
57+
58+
# ==========================================
59+
# BrowserStack Local
60+
# (For localhost, staging/private websites)
61+
# ==========================================
62+
# Set browserStackLocal to true if your website under test is not accessible publicly over the internet
63+
# Learn more about how BrowserStack Local works here -> https://www.browserstack.com/docs/automate/selenium/local-testing-introduction
64+
browserstackLocal: true # <boolean> (Default false)
65+
66+
# Options to be passed to BrowserStack local in-case of advanced configurations
67+
# browserStackLocalOptions:
68+
# localIdentifier: # <string> (Default: null) Needed if you need to run multiple instances of local.
69+
# forceLocal: true # <boolean> (Default: false) Set to true if you need to resolve all your traffic via BrowserStack Local tunnel.
70+
# Entire list of arguments available here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections
71+
72+
# ===================
73+
# Debugging features
74+
# ===================
75+
debug: false # <boolean> # Set to true if you need screenshots for every selenium command ran
76+
networkLogs: false # <boolean> Set to true to enable HAR logs capturing
77+
consoleLogs: errors # <string> Remote browser's console debug levels to be printed (Default: errors)
78+
# Available options are `disable`, `errors`, `warnings`, `info`, `verbose` (Default: errors)

src/test/java/runners/BstackRunner.java

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.json.simple.JSONObject;
88
import org.json.simple.parser.JSONParser;
99
import org.junit.jupiter.api.extension.*;
10-
import utils.SetupLocalTesting;
1110

1211
import java.io.FileReader;
1312
import java.io.UnsupportedEncodingException;
@@ -17,43 +16,9 @@
1716

1817
public class BstackRunner implements TestTemplateInvocationContextProvider {
1918
public Browser browser;
20-
public String username, accessKey, wss;
19+
public String wss;
2120
private JSONObject mainConfig;
2221
private JSONArray platformConfig;
23-
private Map<String, Object> commonCapsConfig;
24-
25-
26-
public BstackRunner() {
27-
this.username = setupCredsAndServer().get("username");
28-
this.accessKey = setupCredsAndServer().get("accesskey");
29-
this.wss = setupCredsAndServer().get("wss");
30-
}
31-
32-
public HashMap<String, String> setupCredsAndServer() {
33-
try {
34-
if (System.getProperty("config") != null) {
35-
JSONParser parser = new JSONParser();
36-
mainConfig = (JSONObject) parser
37-
.parse(new FileReader("src/test/resources/conf/" + System.getProperty("config")));
38-
}
39-
wss = (String) mainConfig.get("wss");
40-
username = System.getenv("BROWSERSTACK_USERNAME");
41-
if (username == null) {
42-
username = (String) mainConfig.get("userName");
43-
}
44-
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
45-
if (accessKey == null) {
46-
accessKey = (String) mainConfig.get("accessKey");
47-
}
48-
} catch (Exception e) {
49-
System.out.println(e.getMessage());
50-
}
51-
HashMap<String, String> creds = new HashMap();
52-
creds.put("username", username);
53-
creds.put("accesskey", accessKey);
54-
creds.put("wss", wss);
55-
return creds;
56-
}
5722

5823
@Override
5924
public boolean supportsTestTemplate(ExtensionContext extensionContext) {
@@ -65,21 +30,14 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
6530
List<TestTemplateInvocationContext> desiredCapsInvocationContexts = new ArrayList<>();
6631

6732
try {
33+
JSONParser parser = new JSONParser();
34+
mainConfig = (JSONObject) parser
35+
.parse(new FileReader("src/test/resources/conf/" + System.getProperty("config")));
6836
platformConfig = (JSONArray) mainConfig.get("environments");
69-
commonCapsConfig = (Map<String, Object>) mainConfig.get("capabilities");
37+
wss = (String) mainConfig.get("wss");
7038

7139
for (int i = 0; i < platformConfig.size(); i++) {
7240
JSONObject platform = (JSONObject) platformConfig.get(i);
73-
platform.putAll(commonCapsConfig);
74-
platform.put("browserstack.username", username);
75-
platform.put("browserstack.accessKey", accessKey);
76-
if(Objects.equals(System.getProperty("local"), "true")) {
77-
platform.put("browserstack.local", "true");
78-
HashMap<String, String> localOptions = new HashMap<>();
79-
localOptions.put("key", accessKey);
80-
//Add more local options here, e.g. forceLocal, localIdentifier, etc.
81-
SetupLocalTesting.createInstance(localOptions);
82-
}
8341
desiredCapsInvocationContexts.add(invocationContext(platform));
8442
}
8543
} catch (Exception e) {
@@ -104,10 +62,6 @@ public boolean supportsParameter(ParameterContext parameterContext,
10462
@Override
10563
public Object resolveParameter(ParameterContext parameterContext,
10664
ExtensionContext extensionContext) {
107-
String testName = extensionContext.getTestMethod().get().getName();
108-
testName += " [" + capabilitiesObject.get("os") + " " + capabilitiesObject.get("os_version");
109-
testName += " - " + capabilitiesObject.get("browser") + " " + capabilitiesObject.get("browser_version") + "]";
110-
capabilitiesObject.put("name", testName);
11165
Playwright playwright = Playwright.create();
11266
BrowserType browserType = playwright.chromium();
11367
String caps = null;

src/test/java/tests/SampleTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.microsoft.playwright.Browser;
44
import com.microsoft.playwright.Page;
55
import runners.PlaywrightTest;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
67

78
public class SampleTest {
89

@@ -14,15 +15,10 @@ void sampleTest(Browser browser) {
1415
String product_name = page.locator("//*[@id='1']/p").textContent();
1516
page.locator("//*[@id='1']/div[4]").click();
1617
page.locator(".float\\-cart__content");
17-
String product_in_cart = page.locator("//*[@id='__next']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]").textContent();
18-
// Update Test Status
19-
if (product_name.equals(product_in_cart)) {
20-
page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"" + "passed" + "\", \"reason\": \"" + "Product has been successfully added to the cart!" + "\"}}");
21-
} else {
22-
page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"" + "failed" + "\", \"reason\": \"" + "There was some issue!" + "\"}}");
23-
}
18+
String product_in_cart = page.locator("//*[@id='__next']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]")
19+
.textContent();
20+
assertEquals(product_in_cart, product_name);
2421
} catch (Exception e) {
25-
page.evaluate("_ => {}", "browserstack_executor: { \"action\": \"setSessionStatus\", \"arguments\": { \"status\": \"" + "failed" + "\", \"reason\": \"" + "There was some issue!" + "\"}}");
2622
System.out.println("Exception: " + e.getMessage());
2723
}
2824
browser.close();

src/test/java/utils/SetupLocalTesting.java

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
{
2-
"wss": "wss://cdp.browserstack.com/playwright",
3-
"userName": "BROWSERSTACK_USERNAME",
4-
"accessKey": "BROWSERSTACK_ACCESS_KEY",
5-
"capabilities": {
6-
"project": "BStack Project",
7-
"build": "browserstack-build-1",
8-
"buildTag": "Regression",
9-
"browserstack.debug": "true",
10-
"browserstack.console": "info"
11-
},
2+
"wss": "ws://127.0.0.1:64225/ec873d261d8cad986db668660c8ab412",
123
"environments": [
134
{
145
"os": "Windows",
@@ -21,12 +12,6 @@
2112
"os_version": "Monterey",
2213
"browser": "playwright-webkit",
2314
"browser_version": "latest"
24-
},
25-
{
26-
"os": "OS X",
27-
"os_version": "Ventura",
28-
"browser": "playwright-firefox",
29-
"browser_version": "latest"
3015
}
3116
]
3217
}

0 commit comments

Comments
 (0)