Skip to content

Commit b58ccdc

Browse files
Varun-gargfrancisf
authored andcommitted
Selenium 4 changes (#14)
* java 10 and selenium 3 Signed-off-by: Varun Garg <varun.ga@browserstack.com> * selenium 4 changes Signed-off-by: Varun Garg <varun.ga@browserstack.com> * code review changes Signed-off-by: Varun Garg <varun.ga@browserstack.com> * fix local Signed-off-by: Varun Garg <varun.ga@browserstack.com> * Apply suggestions from code review Co-Authored-By: Varun-garg <varun.10@live.com> * bump local version * throw exception in case of exception Signed-off-by: Varun Garg <varun.ga@browserstack.com> * Apply suggestions from code review Co-Authored-By: Varun-garg <varun.10@live.com> * Update src/test/resources/conf/local.conf.json Co-Authored-By: Varun-garg <varun.10@live.com> * Update src/test/resources/conf/single.conf.json Co-Authored-By: Varun-garg <varun.10@live.com>
1 parent 8e008b9 commit b58ccdc

File tree

6 files changed

+123
-90
lines changed

6 files changed

+123
-90
lines changed

pom.xml

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

55
<groupId>com.browserstack</groupId>
@@ -12,47 +12,44 @@
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.source>1.6</maven.compiler.source>
16+
<maven.compiler.target>1.6</maven.compiler.target>
1517
<surefire.version>2.19.1</surefire.version>
1618

1719
<test.file></test.file>
1820
<config.file>default</config.file>
1921
</properties>
2022

2123
<dependencies>
22-
<dependency>
23-
<groupId>org.testng</groupId>
24-
<artifactId>testng</artifactId>
25-
<version>6.9.10</version>
26-
</dependency>
27-
<dependency>
28-
<groupId>commons-io</groupId>
29-
<artifactId>commons-io</artifactId>
30-
<version>1.3.2</version>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.seleniumhq.selenium</groupId>
34-
<artifactId>selenium-java</artifactId>
35-
<version>2.52.0</version>
36-
</dependency>
37-
<dependency>
38-
<groupId>com.browserstack</groupId>
39-
<artifactId>browserstack-local-java</artifactId>
40-
<version>0.1.0</version>
41-
</dependency>
42-
<dependency>
43-
<groupId>com.googlecode.json-simple</groupId>
44-
<artifactId>json-simple</artifactId>
45-
<version>1.1.1</version>
46-
</dependency>
24+
<dependency>
25+
<groupId>org.testng</groupId>
26+
<artifactId>testng</artifactId>
27+
<version>6.9.10</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>commons-io</groupId>
31+
<artifactId>commons-io</artifactId>
32+
<version>1.3.2</version>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.seleniumhq.selenium</groupId>
36+
<artifactId>selenium-java</artifactId>
37+
<version>3.12.0</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.browserstack</groupId>
41+
<artifactId>browserstack-local-java</artifactId>
42+
<version>1.0.3</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.googlecode.json-simple</groupId>
46+
<artifactId>json-simple</artifactId>
47+
<version>1.1.1</version>
48+
</dependency>
4749
</dependencies>
4850

4951
<build>
5052
<plugins>
51-
<plugin>
52-
<groupId>org.apache.maven.plugins</groupId>
53-
<artifactId>maven-surefire-plugin</artifactId>
54-
<version>2.12.4</version>
55-
</plugin>
5653
<plugin>
5754
<groupId>org.apache.maven.plugins</groupId>
5855
<artifactId>maven-surefire-plugin</artifactId>
Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,53 @@
11
package com.browserstack;
2-
import com.browserstack.local.Local;
32

4-
import java.io.File;
53
import java.io.FileReader;
6-
import java.io.IOException;
7-
import java.io.InputStream;
84
import java.net.URL;
95
import java.util.HashMap;
10-
import java.util.Map;
11-
import java.util.ArrayList;
12-
import java.util.List;
136
import java.util.Iterator;
7+
import java.util.Map;
8+
9+
import com.browserstack.local.Local;
10+
1411
import org.json.simple.JSONObject;
15-
import org.json.simple.JSONArray;
1612
import org.json.simple.parser.JSONParser;
17-
1813
import org.openqa.selenium.WebDriver;
19-
import org.openqa.selenium.remote.RemoteWebDriver;
2014
import org.openqa.selenium.remote.DesiredCapabilities;
21-
22-
import org.testng.annotations.BeforeMethod;
15+
import org.openqa.selenium.remote.RemoteWebDriver;
2316
import org.testng.annotations.AfterMethod;
24-
import org.testng.annotations.Test;
25-
import org.testng.annotations.Parameters;
26-
import org.testng.annotations.DataProvider;
27-
import org.testng.annotations.Factory;
28-
import org.testng.Assert;
17+
import org.testng.annotations.BeforeMethod;
2918

3019

3120
public class BrowserStackTestNGTest {
3221
public WebDriver driver;
33-
private Local l;
22+
private Local bsLocal;
3423

3524
@BeforeMethod(alwaysRun=true)
3625
@org.testng.annotations.Parameters(value={"config", "environment"})
26+
@SuppressWarnings("unchecked")
3727
public void setUp(String config_file, String environment) throws Exception {
3828
JSONParser parser = new JSONParser();
3929
JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
4030
JSONObject envs = (JSONObject) config.get("environments");
4131

4232
DesiredCapabilities capabilities = new DesiredCapabilities();
4333

44-
Map<String, String> envCapabilities = (Map<String, String>) envs.get(environment);
45-
Iterator it = envCapabilities.entrySet().iterator();
34+
Map<String, Object> envCapabilities = (Map<String, Object>) envs.get(environment);
35+
Iterator<Map.Entry<String, Object>> it = envCapabilities.entrySet().iterator();
4636
while (it.hasNext()) {
47-
Map.Entry pair = (Map.Entry)it.next();
48-
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
37+
Map.Entry<String, Object> pair = (Map.Entry<String, Object>) it.next();
38+
capabilities.setCapability(pair.getKey().toString(), pair.getValue());
4939
}
50-
51-
Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
40+
41+
Map<String, Object> commonCapabilities = (Map<String, Object>) config.get("capabilities");
5242
it = commonCapabilities.entrySet().iterator();
5343
while (it.hasNext()) {
54-
Map.Entry pair = (Map.Entry)it.next();
55-
if(capabilities.getCapability(pair.getKey().toString()) == null){
56-
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
44+
Map.Entry<String, Object> pair = (Map.Entry<String, Object>) it.next();
45+
Object envData = capabilities.getCapability(pair.getKey().toString());
46+
Object resultData = pair.getValue();
47+
if (envData != null && envData.getClass() == JSONObject.class) {
48+
((JSONObject) resultData).putAll((JSONObject) envData);
5749
}
50+
capabilities.setCapability(pair.getKey().toString(), resultData);
5851
}
5952

6053
String username = System.getenv("BROWSERSTACK_USERNAME");
@@ -67,19 +60,36 @@ public void setUp(String config_file, String environment) throws Exception {
6760
accessKey = (String) config.get("key");
6861
}
6962

70-
if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){
71-
l = new Local();
63+
this.checkAndStartBrowserStackLocal(capabilities, accessKey);
64+
65+
driver = new RemoteWebDriver(new URL("https://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
66+
}
67+
68+
public void checkAndStartBrowserStackLocal(DesiredCapabilities capabilities, String accessKey) throws Exception {
69+
if (bsLocal != null) {
70+
return;
71+
}
72+
if (capabilities.getCapability("bstack:options") != null
73+
&& ((JSONObject) capabilities.getCapability("bstack:options")).get("local") != null
74+
&& ((Boolean) ((JSONObject) capabilities.getCapability("bstack:options")).get("local")) == true) {
75+
bsLocal = new Local();
7276
Map<String, String> options = new HashMap<String, String>();
7377
options.put("key", accessKey);
74-
l.start(options);
78+
try {
79+
bsLocal.start(options);
80+
} catch (Exception e) {
81+
System.out.println("Error: could not start browserstack local");
82+
e.printStackTrace();
83+
throw e;
84+
}
7585
}
76-
77-
driver = new RemoteWebDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
7886
}
7987

8088
@AfterMethod(alwaysRun=true)
8189
public void tearDown() throws Exception {
8290
driver.quit();
83-
if(l != null) l.stop();
91+
if (bsLocal != null) {
92+
bsLocal.stop();
93+
}
8494
}
8595
}

src/test/resources/conf/local.conf.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
"server": "hub-cloud.browserstack.com",
33
"user": "BROWSERSTACK_USERNAME",
44
"key": "BROWSERSTACK_ACCESS_KEY",
5-
65
"capabilities": {
7-
"build": "testng-browserstack",
8-
"name": "local_test",
9-
"browserstack.debug": true,
10-
"browserstack.local": true
6+
"browserstack.use_w3c": true,
7+
"bstack:options": {
8+
"os": "Windows",
9+
"osVersion": "7",
10+
"sessionName": "local_test",
11+
"buildName": "testng-browserstack",
12+
"projectName": "My Awesome App",
13+
"debug": true,
14+
"local": true
15+
}
1116
},
12-
1317
"environments": {
1418
"chrome": {
1519
"browser": "chrome"

src/test/resources/conf/parallel.conf.json

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
"server": "hub-cloud.browserstack.com",
33
"user": "BROWSERSTACK_USERNAME",
44
"key": "BROWSERSTACK_ACCESS_KEY",
5-
65
"capabilities": {
7-
"build": "testng-browserstack",
8-
"name": "parallel_test",
9-
"browserstack.debug": true
6+
"browserstack.use_w3c": true,
7+
"bstack:options": {
8+
"sessionName": "parallel_test",
9+
"buildName": "testng-browserstack",
10+
"projectName": "My Awesome App",
11+
"debug": true
12+
}
1013
},
11-
1214
"environments": {
1315
"chrome": {
1416
"browser": "chrome"
@@ -17,10 +19,17 @@
1719
"browser": "firefox"
1820
},
1921
"safari": {
20-
"browser": "safari"
22+
"browser": "safari",
23+
"bstack:options": {
24+
"os": "OS X",
25+
"osVersion": "High Sierra"
26+
}
2127
},
2228
"ie": {
23-
"browser": "internet explorer"
29+
"browser": "Internet Explorer",
30+
"bstack:options": {
31+
"os": "Windows",
32+
}
2433
}
2534
}
2635
}

src/test/resources/conf/single.conf.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
"server": "hub-cloud.browserstack.com",
33
"user": "BROWSERSTACK_USERNAME",
44
"key": "BROWSERSTACK_ACCESS_KEY",
5-
65
"capabilities": {
7-
"build": "testng-browserstack",
8-
"name": "single_test",
9-
"browserstack.debug": true
6+
"browserstack.use_w3c": true,
7+
"bstack:options": {
8+
"os": "Windows",
9+
"osVersion": "7",
10+
"sessionName": "single_test",
11+
"buildName": "testng-browserstack",
12+
"projectName": "My Awesome App",
13+
"debug": true
14+
}
1015
},
11-
1216
"environments": {
1317
"chrome": {
1418
"browser": "chrome"

src/test/resources/conf/suite.conf.json

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
"server": "hub-cloud.browserstack.com",
33
"user": "BROWSERSTACK_USERNAME",
44
"key": "BROWSERSTACK_ACCESS_KEY",
5-
65
"capabilities": {
7-
"build": "testng-browserstack",
8-
"name": "suite_test",
9-
"browserstack.debug": true
6+
"browserstack.use_w3c": true,
7+
"bstack:options": {
8+
"sessionName": "suite_test",
9+
"buildName": "testng-browserstack",
10+
"projectName": "My Awesome App",
11+
"debug": true
12+
}
1013
},
11-
1214
"environments": {
1315
"chrome": {
1416
"browser": "chrome"
@@ -17,10 +19,17 @@
1719
"browser": "firefox"
1820
},
1921
"safari": {
20-
"browser": "safari"
22+
"browser": "safari",
23+
"bstack:options": {
24+
"os": "OS X",
25+
"osVersion": "High Sierra"
26+
}
2127
},
2228
"ie": {
23-
"browser": "internet explorer"
29+
"browser": "Internet Explorer",
30+
"bstack:options": {
31+
"os": "Windows",
32+
}
2433
}
2534
}
2635
}

0 commit comments

Comments
 (0)