Skip to content

Commit 7ab338f

Browse files
committed
Updated bs.json
Fixed Local Testing Logic
1 parent ca84180 commit 7ab338f

4 files changed

Lines changed: 51 additions & 69 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<test-name>End to End Scenario</test-name>
3131
<application-url>http://bstackdemo.com/</application-url>
3232
<application-localhost-url>http://localhost:3000/</application-localhost-url>
33-
<parallel-count>10</parallel-count>
33+
<parallel-count>5</parallel-count>
3434
</properties>
3535

3636
<dependencies>

src/test/java/browserstack/ParallelTestRunner.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@ public void tearDownClass() {
4141
testNGCucumberRunner.finish();
4242

4343
}
44+
45+
4446

4547
}
Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
package browserstack.stepdefs;
22

3-
import org.openqa.selenium.Capabilities;
43
import org.openqa.selenium.WebDriver;
54
import org.openqa.selenium.chrome.ChromeDriver;
6-
import org.openqa.selenium.chrome.ChromeOptions;
75
import org.openqa.selenium.remote.CapabilityType;
86
import org.openqa.selenium.remote.DesiredCapabilities;
97
import org.openqa.selenium.remote.RemoteWebDriver;
108
import org.openqa.selenium.support.ui.WebDriverWait;
11-
import org.testng.annotations.AfterClass;
12-
import org.testng.annotations.AfterMethod;
139
import org.testng.annotations.AfterSuite;
14-
import org.testng.annotations.AfterTest;
1510
import org.testng.annotations.BeforeMethod;
11+
import org.testng.annotations.BeforeSuite;
1612
import org.testng.annotations.Optional;
1713
import org.testng.annotations.Parameters;
1814
import com.browserstack.local.Local;
19-
2015
import browserstack.utils.OsUtility;
2116
import browserstack.utils.Utility;
2217
import io.cucumber.java.After;
2318
import io.cucumber.java.Before;
2419
import io.cucumber.java.Scenario;
25-
import io.cucumber.testng.TestNGCucumberRunner;
2620
import java.io.FileReader;
2721
import java.net.MalformedURLException;
2822
import java.net.URL;
2923
import java.nio.file.Paths;
3024
import java.util.HashMap;
3125
import java.util.Map;
32-
import java.util.UUID;
3326
import java.util.concurrent.TimeUnit;
34-
35-
import org.apache.commons.lang3.RandomStringUtils;
3627
import org.json.simple.JSONObject;
3728
import org.json.simple.parser.JSONParser;
3829

3930
public class BaseTest {
4031

4132
public static WebDriverWait wait;
42-
protected String driverBaseLocation = System.getProperty("user.dir") + "/src/test/resources/chromeDriver";
43-
private Local local;
33+
protected String driverBaseLocation = System.getProperty("user.dir") + "/src/test/resources";
34+
public static Local local;
4435
public static Map<String, String> options;
4536
protected static String URL = "https://bstackdemo.com";
4637
public static DesiredCapabilities caps = new DesiredCapabilities();
@@ -49,32 +40,48 @@ public class BaseTest {
4940
private static final String PASSED = "passed";
5041
private static final String FAILED = "failed";
5142
private static final String REPO_NAME = "browserstack-examples-cucumber-testng - ";
52-
private static final String WEBDRIVER_CHROME_DRIVER = "webdriver.chrome.driver";
43+
private static final String WEBDRIVER_CHROME_DRIVER = "webdriver.chrome.driver";
5344
public static String env = "";
5445
public static JSONObject config;
55-
public static ThreadLocal<Map<String, String>> envCapabilities= new ThreadLocal<Map<String,String>>();
46+
public static ThreadLocal<Map<String, String>> envCapabilities = new ThreadLocal<Map<String, String>>();
5647

5748
public WebDriver getDriver() {
5849
return ThreadLocalDriver.getWebDriver();
5950
}
6051

52+
@BeforeSuite
53+
public void before() throws Exception {
54+
55+
56+
57+
if (System.getProperty("application_endpoint") != null && System.getProperty("application_endpoint").equals("http://localhost:3000/"))
58+
{
59+
local = new Local();
60+
options = new HashMap<String, String>();
61+
accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
62+
options.put("key", accessKey);
63+
local.start(options);
64+
65+
}
66+
}
67+
6168
@BeforeMethod
6269
@Parameters({ "environment", "browser", "caps_type", "env_cap_id", "settestname" })
6370
public synchronized void setUpClass(@Optional("local") String environment, @Optional("chrome") String browser,
6471
@Optional("single") String caps_type, @Optional("2") int env_cap_id,
6572
@Optional("BStack test name") String settestname) throws Exception {
6673
JSONParser parser = new JSONParser();
67-
if(System.getProperty("application_endpoint") !=null)
68-
{
69-
URL = System.getProperty("application_endpoint");
74+
if (System.getProperty("application_endpoint") != null) {
75+
URL = System.getProperty("application_endpoint");
7076
}
7177
config = (JSONObject) parser.parse(new FileReader("src/test/resources/config/bs.json"));
7278
if (environment.equalsIgnoreCase("local")) {
73-
if (OsUtility.isWindows()) {
74-
System.setProperty(WEBDRIVER_CHROME_DRIVER, Paths.get(driverBaseLocation, "/chromedriver.exe").toString());
75-
} else {
76-
System.setProperty(WEBDRIVER_CHROME_DRIVER, Paths.get(driverBaseLocation, "/chromedriver").toString());
77-
}
79+
if (OsUtility.isWindows()) {
80+
System.setProperty(WEBDRIVER_CHROME_DRIVER,
81+
Paths.get(driverBaseLocation, "/chromedriver.exe").toString());
82+
} else {
83+
System.setProperty(WEBDRIVER_CHROME_DRIVER, Paths.get(driverBaseLocation, "/chromedriver").toString());
84+
}
7885
ThreadLocalDriver.setWebDriver(new ChromeDriver());
7986

8087
}
@@ -90,11 +97,11 @@ else if (environment.equalsIgnoreCase("docker")) {
9097
JSONObject profilesJson = (JSONObject) config.get("tests");
9198
JSONObject envs = (JSONObject) profilesJson.get(caps_type);
9299
Map<String, String> commonCapabilities = (Map<String, String>) envs.get("common_caps");
93-
envCapabilities.set((Map<String, String>) ((org.json.simple.JSONArray) envs
94-
.get("env_caps")).get(env_cap_id));
95-
100+
envCapabilities
101+
.set((Map<String, String>) ((org.json.simple.JSONArray) envs.get("env_caps")).get(env_cap_id));
102+
96103
Map<String, String> localCapabilities = (Map<String, String>) envs.get("local_binding_caps");
97-
104+
98105
caps.merge(new DesiredCapabilities(commonCapabilities));
99106
if (caps_type.equals("local")) {
100107
URL = (String) envs.get("application_endpoint");
@@ -117,21 +124,19 @@ else if (environment.equalsIgnoreCase("docker")) {
117124
public void startup(Scenario scenario) throws Exception {
118125
if (env == "remote") {
119126
DesiredCapabilities derivedCaps = new DesiredCapabilities();
120-
derivedCaps.merge(caps);
127+
derivedCaps.merge(caps);
121128
derivedCaps.merge(new DesiredCapabilities()).setCapability("name", scenario.getName());
122129
String buildName = System.getenv("BROWSERSTACK_BUILD_NAME");
123130
if (buildName == null) {
124131
buildName = Utility.getEpochTime();
125132
}
126133
derivedCaps.setCapability("build", REPO_NAME + buildName);
127-
setupLocal(derivedCaps, config, accessKey);
128134
try {
129-
ThreadLocal<DesiredCapabilities> dc = new ThreadLocal<DesiredCapabilities>();
135+
ThreadLocal<DesiredCapabilities> dc = new ThreadLocal<DesiredCapabilities>();
130136
dc.set(derivedCaps);
131137
dc.get().merge(new DesiredCapabilities(envCapabilities.get()));
132138
ThreadLocalDriver.setWebDriver(new RemoteWebDriver(
133-
new URL("https://" + username + ":" + accessKey + "@hub.browserstack.com/wd/hub"),
134-
dc.get()));
139+
new URL("https://" + username + ":" + accessKey + "@hub.browserstack.com/wd/hub"), dc.get()));
135140
} catch (MalformedURLException e) {
136141
// TODO Auto-generated catch block
137142
e.printStackTrace();
@@ -144,35 +149,9 @@ public void startup(Scenario scenario) throws Exception {
144149

145150
}
146151

147-
private void setupLocal(DesiredCapabilities caps, JSONObject testConfigs, String accessKey) throws Exception {
148-
if (caps.getCapability("browserstack.local") != null
149-
&& caps.getCapability("browserstack.local").equals("true")) {
150-
local = new Local();
151-
if(!local.isRunning())
152-
{
153-
154-
/* UUID uuid = UUID.randomUUID();
155-
caps.setCapability("browserstack.localIdentifier", uuid.toString());
156-
options = new HashMap<String, String>();
157-
options.put("key", accessKey);
158-
options.put("localIdentifier", uuid.toString());
159-
local.start(options);*/
160-
161-
// You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
162-
HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
163-
bsLocalArgs.put("key", accessKey);
164-
165-
// Starts the Local instance with the required arguments
166-
local.start(bsLocalArgs);
167-
168-
}
169-
170-
}
171-
}
172-
173152
@After
174153
public void teardown(Scenario scenario) throws Exception {
175-
if (System.getProperty("environment").equalsIgnoreCase("remote")) {
154+
if (System.getProperty("environment").equalsIgnoreCase("remote")) {
176155
if (scenario.isFailed()) {
177156
Utility.setSessionStatus(ThreadLocalDriver.getWebDriver(), FAILED,
178157
String.format("%s failed.", scenario.getName()));
@@ -185,15 +164,16 @@ public void teardown(Scenario scenario) throws Exception {
185164

186165
}
187166
ThreadLocalDriver.getWebDriver().quit();
188-
167+
189168
}
190-
191-
@AfterMethod
192-
public void tearDown() throws Exception {
193-
194-
if (local != null) {
195-
local.stop();
196-
}
169+
170+
@AfterSuite
171+
public void tearDown() throws Exception {
172+
173+
if (local != null) {
174+
local.stop();
175+
}
197176

198177
}
178+
199179
}

src/test/resources/config/bs.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"user": "sanketmali4",
3-
"key": "XaqpcHttuyFzXSzC3uNM",
2+
"user": "BROWSERSTACK_USERNAME",
3+
"key": "BROWSERSTACK_ACCESS_KEY",
44
"application_endpoint":"https://bstackdemo.com",
55
"tests":{
66
"parallel":{

0 commit comments

Comments
 (0)