Skip to content

Commit 0d2b135

Browse files
Ankit098francisf
authored andcommitted
update: sdk branch restructure
1 parent 80afc45 commit 0d2b135

File tree

7 files changed

+108
-142
lines changed

7 files changed

+108
-142
lines changed

README.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@
33
## Setup
44
* Clone the repo
55
* Install dependencies `mvn install`
6-
* Update credentials in the `/src/test/resources/*.conf.json` file with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings).
7-
* For parallel testing, control the concurrency by setting the value for `parallel.count`. Junit 5 uses the following properties for parallelism:
8-
```
9-
junit.jupiter.execution.parallel.enabled = true
10-
junit.jupiter.execution.parallel.mode.default = concurrent
11-
junit.jupiter.execution.parallel.config.strategy=fixed
12-
junit.jupiter.execution.parallel.config.fixed.parallelism=${parallel.count}
13-
```
14-
## Running your tests
15-
* To run a sample tests, run `mvn test -P sample-test`
16-
* Update `<parallel.count>` in `pom.xml` to set the number of parallel threads
6+
* Update credentials in the `browserstack.yml` file with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings).
7+
8+
## Running tests:
9+
* To run a sample tests, run `mvn test -P sample`.
10+
* To run local tests, run `mvn test -P local`.
1711

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

pom.xml

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,21 @@
6363
<artifactId>httpclient</artifactId>
6464
<version>${httpclient}</version>
6565
</dependency>
66+
<dependency>
67+
<groupId>org.yaml</groupId>
68+
<artifactId>snakeyaml</artifactId>
69+
<version>2.0</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>com.browserstack</groupId>
73+
<artifactId>browserstack-java-sdk</artifactId>
74+
<version>LATEST</version>
75+
<scope>compile</scope>
76+
</dependency>
6677
</dependencies>
6778
<profiles>
6879
<profile>
69-
<id>sample-local-test</id>
80+
<id>local</id>
7081
<build>
7182
<plugins>
7283
<plugin>
@@ -77,26 +88,17 @@
7788
<includes>
7889
<include>${tests.single}</include>
7990
</includes>
80-
<systemPropertyVariables>
81-
<config>browserstack.conf.json</config>
82-
<local>true</local>
83-
</systemPropertyVariables>
84-
<properties>
85-
<configurationParameters>
86-
junit.jupiter.execution.parallel.enabled = true
87-
junit.jupiter.execution.parallel.mode.default = concurrent
88-
junit.jupiter.execution.parallel.config.strategy=fixed
89-
junit.jupiter.execution.parallel.config.fixed.parallelism=${parallel.count}
90-
</configurationParameters>
91-
</properties>
9291
<testFailureIgnore>false</testFailureIgnore>
92+
<argLine>
93+
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
94+
</argLine>
9395
</configuration>
9496
</plugin>
9597
</plugins>
9698
</build>
9799
</profile>
98100
<profile>
99-
<id>sample-test</id>
101+
<id>sample</id>
100102
<build>
101103
<plugins>
102104
<plugin>
@@ -107,19 +109,10 @@
107109
<includes>
108110
<include>${tests.single}</include>
109111
</includes>
110-
<systemPropertyVariables>
111-
<config>browserstack.conf.json</config>
112-
<local>false</local>
113-
</systemPropertyVariables>
114-
<properties>
115-
<configurationParameters>
116-
junit.jupiter.execution.parallel.enabled = true
117-
junit.jupiter.execution.parallel.mode.default = concurrent
118-
junit.jupiter.execution.parallel.config.strategy=fixed
119-
junit.jupiter.execution.parallel.config.fixed.parallelism=${parallel.count}
120-
</configurationParameters>
121-
</properties>
122112
<testFailureIgnore>false</testFailureIgnore>
113+
<argLine>
114+
-javaagent:${com.browserstack:browserstack-java-sdk:jar}
115+
</argLine>
123116
</configuration>
124117
</plugin>
125118
</plugins>
Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,77 @@
11
package runners;
22

3-
import com.microsoft.playwright.Browser;
4-
import com.microsoft.playwright.BrowserType;
5-
import com.microsoft.playwright.Playwright;
6-
import org.json.simple.JSONArray;
7-
import org.json.simple.JSONObject;
8-
import org.json.simple.parser.JSONParser;
9-
import org.junit.jupiter.api.extension.*;
3+
import com.microsoft.playwright.*;
4+
import org.junit.jupiter.api.*;
5+
import org.yaml.snakeyaml.Yaml;
106

11-
import java.io.FileReader;
127
import java.io.UnsupportedEncodingException;
138
import java.net.URLEncoder;
9+
import org.json.JSONObject;
1410
import java.util.*;
15-
import java.util.stream.Stream;
11+
import java.io.File;
12+
import java.io.InputStream;
13+
import java.nio.file.Files;
1614

17-
public class BstackRunner implements TestTemplateInvocationContextProvider {
18-
public Browser browser;
19-
public String wss;
20-
private JSONObject mainConfig;
21-
private JSONArray platformConfig;
15+
public class BstackRunner {
16+
public static String userName, accessKey;
17+
public static Map<String, Object> browserStackYamlMap;
18+
public static final String USER_DIR = "user.dir";
19+
20+
static Playwright playwright;
21+
static Browser browser;
2222

23-
@Override
24-
public boolean supportsTestTemplate(ExtensionContext extensionContext) {
25-
return true;
23+
BrowserContext context;
24+
public Page page;
25+
26+
public BstackRunner() {
27+
File file = new File(getUserDir() + "/browserstack.yml");
28+
browserStackYamlMap = convertYamlFileToMap(file, new HashMap<>());
2629
}
2730

28-
@Override
29-
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext extensionContext) {
30-
List<TestTemplateInvocationContext> desiredCapsInvocationContexts = new ArrayList<>();
31+
@BeforeEach
32+
void launchBrowser() {
33+
playwright = Playwright.create();
34+
BrowserType browserType = playwright.chromium();
35+
String caps = null;
36+
userName = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : (String) browserStackYamlMap.get("userName");
37+
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_ACCESS_KEY") : (String) browserStackYamlMap.get("accessKey");
3138

32-
try {
33-
JSONParser parser = new JSONParser();
34-
mainConfig = (JSONObject) parser
35-
.parse(new FileReader("src/test/resources/conf/" + System.getProperty("config")));
36-
platformConfig = (JSONArray) mainConfig.get("environments");
37-
wss = (String) mainConfig.get("wss");
39+
HashMap<String, String> capabilitiesObject = new HashMap<>();
40+
capabilitiesObject.put("browserstack.user", userName);
41+
capabilitiesObject.put("browserstack.key", accessKey);
42+
capabilitiesObject.put("browserstack.source", "java-playwright-browserstack:sample-sdk:v1.0");
43+
capabilitiesObject.put("browser", "chrome");
3844

39-
for (int i = 0; i < platformConfig.size(); i++) {
40-
JSONObject platform = (JSONObject) platformConfig.get(i);
41-
desiredCapsInvocationContexts.add(invocationContext(platform));
42-
}
43-
} catch (Exception e) {
44-
e.printStackTrace();
45+
JSONObject jsonCaps = new JSONObject(capabilitiesObject);
46+
try {
47+
caps = URLEncoder.encode(jsonCaps.toString(), "utf-8");
48+
} catch (UnsupportedEncodingException e) {
49+
throw new RuntimeException(e);
4550
}
46-
return desiredCapsInvocationContexts.stream();
51+
String wsEndpoint = "wss://cdp.browserstack.com/playwright?caps=" + caps;
52+
browser = browserType.connect(wsEndpoint);
53+
page = browser.newPage();
4754
}
4855

49-
private TestTemplateInvocationContext invocationContext(JSONObject capabilitiesObject) {
50-
return new TestTemplateInvocationContext() {
51-
52-
@Override
53-
public List<Extension> getAdditionalExtensions() {
56+
@AfterEach
57+
void closeContext() {
58+
page.close();
59+
browser.close();
60+
}
5461

55-
return Collections.singletonList(new ParameterResolver() {
56-
@Override
57-
public boolean supportsParameter(ParameterContext parameterContext,
58-
ExtensionContext extensionContext) {
59-
return parameterContext.getParameter().getType().equals(Browser.class);
60-
}
62+
private String getUserDir() {
63+
return System.getProperty(USER_DIR);
64+
}
6165

62-
@Override
63-
public Object resolveParameter(ParameterContext parameterContext,
64-
ExtensionContext extensionContext) {
65-
Playwright playwright = Playwright.create();
66-
BrowserType browserType = playwright.chromium();
67-
String caps = null;
68-
try {
69-
caps = URLEncoder.encode(capabilitiesObject.toString(), "utf-8");
70-
} catch (UnsupportedEncodingException e) {
71-
throw new RuntimeException(e);
72-
}
73-
String ws_endpoint = wss + "?caps=" + caps;
74-
browser = browserType.connect(ws_endpoint);
75-
return browser;
76-
}
77-
});
78-
}
79-
};
66+
private Map<String, Object> convertYamlFileToMap(File yamlFile, Map<String, Object> map) {
67+
try {
68+
InputStream inputStream = Files.newInputStream(yamlFile.toPath());
69+
Yaml yaml = new Yaml();
70+
Map<String, Object> config = yaml.load(inputStream);
71+
map.putAll(config);
72+
} catch (Exception e) {
73+
throw new RuntimeException(String.format("Malformed browserstack.yml file - %s.", e));
74+
}
75+
return map;
8076
}
8177
}

src/test/java/runners/PlaywrightTest.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/test/java/tests/LocalTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tests;
2+
3+
import org.junit.jupiter.api.*;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import runners.BstackRunner;
7+
8+
public class LocalTest extends BstackRunner {
9+
10+
@Test
11+
void sampleTest() {
12+
try {
13+
page.navigate("http://bs-local.com:45454/");
14+
String validateContent = page.title();
15+
assertTrue(validateContent.contains("BrowserStack Local"), "Local content not validated!");
16+
} catch (Exception e) {
17+
System.out.println("Exception: " + e.getMessage());
18+
}
19+
}
20+
}

src/test/java/tests/SampleTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package tests;
22

3-
import com.microsoft.playwright.Browser;
4-
import com.microsoft.playwright.Page;
5-
import runners.PlaywrightTest;
3+
import org.junit.jupiter.api.*;
64
import static org.junit.jupiter.api.Assertions.assertEquals;
75

8-
public class SampleTest {
6+
import runners.BstackRunner;
97

10-
@PlaywrightTest
11-
void sampleTest(Browser browser) {
12-
Page page = browser.newPage();
8+
public class SampleTest extends BstackRunner {
9+
10+
@Test
11+
void sampleTest() {
1312
try {
1413
page.navigate("https://bstackdemo.com/");
1514
String product_name = page.locator("//*[@id='1']/p").textContent();
@@ -21,6 +20,5 @@ void sampleTest(Browser browser) {
2120
} catch (Exception e) {
2221
System.out.println("Exception: " + e.getMessage());
2322
}
24-
browser.close();
2523
}
2624
}

src/test/resources/conf/browserstack.conf.json

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)