Skip to content

Commit addae20

Browse files
kamal-kaur04francisf
authored andcommitted
Modify Local Spawning Logic to fix parallel execution
1 parent dc82322 commit addae20

File tree

5 files changed

+54
-20
lines changed

5 files changed

+54
-20
lines changed

config/local.testng.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
33
<suite name="Local">
4+
<parameter name="config" value="local.conf.json"/>
45
<test name="LocalTest">
5-
<parameter name="config" value="local.conf.json"/>
66
<parameter name="environment" value="chrome"/>
77
<classes>
88
<class name="com.browserstack.LocalTest"/>

config/parallel.testng.xml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
33
<suite name="Parallel" thread-count="3" parallel="tests">
4+
<parameter name="config" value="parallel.conf.json"/>
45
<test name="SingleTestEnv1">
5-
<parameter name="config" value="parallel.conf.json"/>
66
<parameter name="environment" value="env1"/>
77
<classes>
88
<class name="com.browserstack.SingleTest"/>
99
</classes>
1010
</test>
1111

1212
<test name="SingleTestEnv2">
13-
<parameter name="config" value="parallel.conf.json"/>
1413
<parameter name="environment" value="env2"/>
1514
<classes>
1615
<class name="com.browserstack.SingleTest"/>
1716
</classes>
1817
</test>
1918

2019
<test name="SingleTestEnv3">
21-
<parameter name="config" value="parallel.conf.json"/>
2220
<parameter name="environment" value="env3"/>
2321
<classes>
2422
<class name="com.browserstack.SingleTest"/>

config/single.testng.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
33
<suite name="Single">
4+
<parameter name="config" value="single.conf.json"/>
45
<test name="SingleTest">
5-
<parameter name="config" value="single.conf.json"/>
66
<parameter name="environment" value="chrome"/>
77
<classes>
88
<class name="com.browserstack.SingleTest"/>

config/suite.testng.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
33
<suite name="Pool" parallel="tests">
4+
<parameter name="config" value="suite.conf.json"/>
45
<test name="PoolTestChrome" thread-count="3" parallel="classes">
5-
<parameter name="config" value="suite.conf.json"/>
66
<parameter name="environment" value="chrome"/>
77
<classes>
88
<class name="com.browserstack.suite.SuiteTest01"/>
@@ -19,7 +19,6 @@
1919
</test>
2020

2121
<test name="PoolTestFirefox" thread-count="3" parallel="classes">
22-
<parameter name="config" value="suite.conf.json"/>
2322
<parameter name="environment" value="firefox"/>
2423
<classes>
2524
<class name="com.browserstack.suite.SuiteTest01"/>

src/test/java/com/browserstack/BrowserStackTestNGTest.java

+50-13
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,65 @@
1313
import org.openqa.selenium.WebDriver;
1414
import org.openqa.selenium.MutableCapabilities;
1515
import org.openqa.selenium.remote.RemoteWebDriver;
16-
import org.testng.annotations.AfterMethod;
17-
import org.testng.annotations.BeforeMethod;
16+
import org.testng.annotations.*;
1817

1918
public class BrowserStackTestNGTest {
2019
public WebDriver driver;
21-
private Local l;
20+
private static Local l;
21+
private static JSONObject config;
22+
private static Map<String, Object> commonCapabilities;
23+
private static String username;
24+
private static String accessKey;
25+
26+
@BeforeSuite(alwaysRun=true)
27+
@Parameters(value = { "config" })
28+
public void beforeSuite(String config_file) throws Exception {
29+
JSONParser parser = new JSONParser();
30+
config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
31+
commonCapabilities = (Map<String, Object>) config.get("capabilities");
32+
HashMap bstackOptionsMap = (HashMap) commonCapabilities.get("bstack:options");
33+
34+
35+
username = System.getenv("BROWSERSTACK_USERNAME");
36+
if (username == null) {
37+
username = (String) config.get("user");
38+
}
39+
40+
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
41+
if (accessKey == null) {
42+
accessKey = (String) config.get("key");
43+
}
44+
try {
45+
if ((bstackOptionsMap.get("local") != null &&
46+
bstackOptionsMap.get("local").toString().equalsIgnoreCase("true") && (l == null))) {
47+
l = new Local();
48+
Map<String, String> options = new HashMap<String, String>();
49+
options.put("key", accessKey);
50+
l.start(options);
51+
}
52+
} catch (Exception e) {
53+
System.out.println("Error while start local - " + e);
54+
}
55+
}
2256

2357
@BeforeMethod(alwaysRun = true)
2458
@org.testng.annotations.Parameters(value = { "config", "environment" })
2559
@SuppressWarnings("unchecked")
2660
public void setUp(String config_file, String environment) throws Exception {
2761
JSONParser parser = new JSONParser();
28-
JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
62+
config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
2963
JSONObject envs = (JSONObject) config.get("environments");
3064

3165
MutableCapabilities capabilities = new MutableCapabilities();
3266

33-
Map<String, String> envCapabilities = (Map<String, String>) envs.get(environment);
67+
Map<String, Object> envCapabilities = (Map<String, Object>) envs.get(environment);
3468
Iterator it = envCapabilities.entrySet().iterator();
3569
while (it.hasNext()) {
3670
Map.Entry pair = (Map.Entry) it.next();
3771
capabilities.setCapability(pair.getKey().toString(), pair.getValue());
3872
}
3973

40-
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
74+
commonCapabilities = (Map<String, Object>) config.get("capabilities");
4175
it = commonCapabilities.entrySet().iterator();
4276
while (it.hasNext()) {
4377
Map.Entry pair = (Map.Entry) it.next();
@@ -50,19 +84,20 @@ public void setUp(String config_file, String environment) throws Exception {
5084
}
5185
}
5286

53-
String username = System.getenv("BROWSERSTACK_USERNAME");
87+
username = System.getenv("BROWSERSTACK_USERNAME");
5488
if (username == null) {
5589
username = (String) config.get("user");
5690
}
5791

58-
String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
92+
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
5993
if (accessKey == null) {
6094
accessKey = (String) config.get("key");
6195
}
6296

6397
if (capabilities.getCapability("bstack:options") != null) {
6498
HashMap bstackOptionsMap = (HashMap) capabilities.getCapability("bstack:options");
65-
if ((bstackOptionsMap.get("local") != null && bstackOptionsMap.get("local").toString().equalsIgnoreCase("true")) || (capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local").toString().equalsIgnoreCase("true"))) {
99+
if ((bstackOptionsMap.get("local") != null &&
100+
bstackOptionsMap.get("local").toString().equalsIgnoreCase("true") && (l == null || !l.isRunning()))) {
66101
l = new Local();
67102
Map<String, String> options = new HashMap<String, String>();
68103
options.put("key", accessKey);
@@ -76,10 +111,12 @@ public void setUp(String config_file, String environment) throws Exception {
76111
}
77112

78113
@AfterMethod(alwaysRun = true)
79-
public void tearDown() throws Exception {
114+
public void tearDown() {
80115
driver.quit();
81-
if (l != null) {
82-
l.stop();
83-
}
116+
}
117+
118+
@AfterSuite(alwaysRun = true)
119+
public void afterSuite() throws Exception {
120+
if (l != null) l.stop();
84121
}
85122
}

0 commit comments

Comments
 (0)