Skip to content

Commit 65ab463

Browse files
Merge pull request #6 from browserstack/selenium-4
Selenium 4
2 parents b5ad2e2 + d3ff07c commit 65ab463

6 files changed

Lines changed: 134 additions & 126 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
target/
22
.vscode
33
browserstack.err
4+
.idea/

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ In every test file (JavaSample, JavaLocalSample, JavaParallelSample) make sure y
2626
2. Change capabilities of test.
2727

2828
```java
29-
DesiredCapabilities caps = new DesiredCapabilities();
30-
caps.setCapability("browserName", "iPhone");
31-
caps.setCapability("device", "iPhone 11");
32-
caps.setCapability("realMobile", "true");
33-
caps.setCapability("local", "true");
34-
caps.setCapability("os_version", "14.0");
35-
caps.setCapability("name", "BStack-[Java] Sample Test"); // test name
36-
caps.setCapability("build", "BStack Build Number 1"); // CI/CD job or build name
29+
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
30+
browserstackOptions.put("os", "OS X");
31+
browserstackOptions.put("osVersion", "Sierra");
32+
browserstackOptions.put("local", "false");
33+
browserstackOptions.put("seleniumVersion", "4.0.0");
34+
capabilities.setCapability("bstack:options", browserstackOptions);
35+
capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name
36+
capabilities.setCapability("buildName", "BStack Build Number 1"); // CI/CD job or build name
3737
```
3838

3939
## Build and run test using maven.
@@ -58,4 +58,4 @@ b. To run parallel test session.
5858
c. To run local test session.
5959
```
6060
mvn -Dexec.mainClass="com.browserstack.app.JavaLocalSample" -Dexec.classpathScope=test test-compile exec:java
61-
```
61+
```

pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<dependency>
3434
<groupId>org.seleniumhq.selenium</groupId>
3535
<artifactId>selenium-java</artifactId>
36-
<version>3.1.0</version>
36+
<version>4.1.0</version>
3737
</dependency>
3838
<dependency>
3939
<groupId>com.browserstack</groupId>
@@ -92,4 +92,22 @@
9292
</plugins>
9393
</pluginManagement>
9494
</build>
95+
<profiles>
96+
<profile>
97+
<id>single</id>
98+
<build>
99+
<plugins>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-surefire-plugin</artifactId>
103+
<configuration>
104+
<includes>
105+
<include>com/browserstack/app/JavaSample.java</include>
106+
</includes>
107+
</configuration>
108+
</plugin>
109+
</plugins>
110+
</build>
111+
</profile>
112+
</profiles>
95113
</project>

src/test/java/com/browserstack/app/JavaLocalSample.java

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.openqa.selenium.remote.RemoteWebDriver;
88
import org.openqa.selenium.JavascriptExecutor;
99
import java.net.URL;
10+
import java.time.Duration;
1011
import java.util.HashMap;
1112

1213
import org.openqa.selenium.support.ui.ExpectedConditions;
@@ -16,50 +17,53 @@
1617
public class JavaLocalSample {
1718
public static final String AUTOMATE_USERNAME = "BROWSERSTACK_USERNAME";
1819
public static final String AUTOMATE_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY";
19-
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
20-
public static void main(String[] args) throws Exception {
21-
DesiredCapabilities caps = new DesiredCapabilities();
22-
caps.setCapability("os", "OS X");
23-
caps.setCapability("os_version", "Sierra");
24-
caps.setCapability("browser", "Safari");
25-
caps.setCapability("browser_version", "10.0");
26-
caps.setCapability("browserstack.local", "true");
27-
caps.setCapability("name", "BStack-[Java] Sample Local Test"); // test name
28-
caps.setCapability("build", "BStack Build Number 1"); // CI/CD job or build name
29-
30-
//Creates an instance of Local
31-
Local bsLocal = new Local();
20+
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
21+
public static void main(String[] args) throws Exception {
22+
DesiredCapabilities capabilities = new DesiredCapabilities();
23+
capabilities.setCapability("browserName", "Chrome");
24+
capabilities.setCapability("browserVersion", "latest");
25+
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
26+
browserstackOptions.put("os", "OS X");
27+
browserstackOptions.put("osVersion", "Sierra");
28+
browserstackOptions.put("local", "true");
29+
browserstackOptions.put("seleniumVersion", "4.0.0");
30+
capabilities.setCapability("bstack:options", browserstackOptions);
31+
capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name
32+
capabilities.setCapability("buildName", "BStack Local Build Number 1"); // CI/CD job or build name
33+
34+
//Creates an instance of Local
35+
Local bsLocal = new Local();
3236

33-
// You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
34-
HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
35-
bsLocalArgs.put("key", AUTOMATE_ACCESS_KEY);
37+
// You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
38+
HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
39+
bsLocalArgs.put("key", AUTOMATE_ACCESS_KEY);
3640

37-
// Starts the Local instance with the required arguments
38-
bsLocal.start(bsLocalArgs);
41+
// Starts the Local instance with the required arguments
42+
bsLocal.start(bsLocalArgs);
3943

40-
// Check if BrowserStack local instance is running
41-
System.out.println(bsLocal.isRunning());
44+
// Check if BrowserStack local instance is running
45+
System.out.println(bsLocal.isRunning());
4246

43-
final WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
44-
try {
45-
driver.get("http://bs-local.com:45691/check");
46-
final WebDriverWait wait = new WebDriverWait(driver, 10);
47-
// getting name of the product
48-
String bodyText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body"))).getText();
49-
50-
if (bodyText.equals("Up and running")) {
51-
markTestStatus("passed", "Local Test is successful and up and running", driver);
52-
}
53-
} catch (Exception e) {
54-
markTestStatus("failed", "Could'nt connect the local", driver);
55-
}
56-
//Stop the Local instance
57-
bsLocal.stop();
58-
driver.quit();
59-
}
60-
// This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack
61-
public static void markTestStatus(String status, String reason, WebDriver driver) {
62-
final JavascriptExecutor jse = (JavascriptExecutor) driver;
63-
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
64-
}
47+
final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
48+
try {
49+
driver.get("http://bs-local.com:45691/check");
50+
final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
51+
// getting name of the product
52+
String bodyText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body"))).getText();
53+
54+
if (bodyText.equals("Up and running")) {
55+
markTestStatus("passed", "Local Test is successful and up and running", driver);
56+
}
57+
} catch (Exception e) {
58+
markTestStatus("failed", "Could'nt connect the local", driver);
59+
}
60+
//Stop the Local instance
61+
bsLocal.stop();
62+
driver.quit();
63+
}
64+
// This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack
65+
public static void markTestStatus(String status, String reason, WebDriver driver) {
66+
final JavascriptExecutor jse = (JavascriptExecutor) driver;
67+
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
68+
}
6569
}
Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.browserstack.app;
2-
import java.util.*;
2+
import java.util.*;
33
import java.net.MalformedURLException;
44
import java.net.URL;
5+
import java.time.Duration;
56
import java.util.Hashtable;
6-
import java.util.Iterator;
7-
import java.util.Set;
87
import org.openqa.selenium.By;
98
import org.openqa.selenium.JavascriptExecutor;
109
import org.openqa.selenium.WebDriver;
@@ -14,36 +13,31 @@
1413
import org.openqa.selenium.support.ui.ExpectedConditions;
1514
import org.openqa.selenium.support.ui.WebDriverWait;
1615

17-
1816
class ParallelTest implements Runnable {
1917
public static final String USERNAME = "BROWSERSTACK_USERNAME";
2018
public static final String AUTOMATE_KEY = "BROWSERSTACK_ACCESS_KEY";
2119
public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
2220
Hashtable<String, String> capsHashtable;
21+
String sessionName;
2322

24-
ParallelTest(Hashtable<String, String> cap){
23+
ParallelTest(Hashtable<String, String> cap, String sessionString){
2524
capsHashtable = cap;
25+
sessionName = sessionString;
2626
}
2727

28-
@Override
2928
public void run() {
30-
String key;
31-
DesiredCapabilities caps = new DesiredCapabilities();
32-
// Iterate over the hashtable and set the capabilities
33-
Set<String> keys = capsHashtable.keySet();
34-
Iterator<String> keysIterator = keys.iterator();
35-
while (keysIterator.hasNext()) {
36-
key = keysIterator.next();
37-
caps.setCapability(key, capsHashtable.get(key));
38-
}
29+
DesiredCapabilities caps = new DesiredCapabilities();
30+
caps.setCapability("bstack:options", capsHashtable);
31+
caps.setCapability("sessionName", sessionName); // test name
32+
caps.setCapability("buildName", "BStack-[Java] Sample buildName"); // CI/CD job or build name
3933
WebDriver driver;
4034
try {
4135
driver = new RemoteWebDriver(new URL(URL), caps);
4236
final JavascriptExecutor jse = (JavascriptExecutor) driver;
4337
try {
4438
// Searching for 'BrowserStack' on google.com
4539
driver.get("https://bstackdemo.com/");
46-
WebDriverWait wait = new WebDriverWait(driver, 10);
40+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
4741
wait.until(ExpectedConditions.titleIs("StackDemo"));
4842
// Getting name of the product
4943
String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText();
@@ -68,51 +62,36 @@ public void run() {
6862
}
6963
}
7064
}
71-
public class JavaParallelSample {
7265

66+
public class JavaParallelSample {
7367
public static void main(String[] args) throws Exception {
7468
List<Hashtable<String, String>> caps = new ArrayList<Hashtable<String, String>>();
7569

7670
//device 1
7771
Hashtable<String, String> cap1 = new Hashtable<String, String>();
78-
cap1.put("device", "iPhone 12 Pro");
79-
cap1.put("real_mobile", "true");
80-
cap1.put("build", "BStack-[Java] Sample Build");
81-
cap1.put("name", "Thread 1");
72+
cap1.put("deviceName", "iPhone 12 Pro");
73+
cap1.put("realMobile", "true");
8274
caps.add(cap1);
8375

8476
//device 2
8577
Hashtable<String, String> cap2 = new Hashtable<String, String>();
86-
cap2.put("device", "Samsung Galaxy S20");
87-
cap2.put("real_mobile", "true");
88-
cap2.put("build", "BStack-[Java] Sample Build");
89-
cap2.put("name", "Thread 2");
78+
cap2.put("deviceName", "Samsung Galaxy S20");
79+
cap2.put("realMobile", "true");
9080
caps.add(cap2);
9181

9282
//device 3
9383
Hashtable<String, String> cap3 = new Hashtable<String, String>();
94-
cap3.put("browser", "safari");
95-
cap3.put("browser_version", "14");
9684
cap3.put("os", "OS X");
97-
cap3.put("os_version", "Big Sur");
98-
cap3.put("build", "BStack-[Java] Sample Build");
99-
cap3.put("name", "Thread 3");
10085
caps.add(cap3);
10186

10287
//device 4
10388
Hashtable<String, String> cap4 = new Hashtable<String, String>();
104-
cap4.put("browser", "Chrome");
105-
cap4.put("browser_version", "latest");
106-
cap4.put("os", "OS X");
107-
cap4.put("os_version", "Monterey");
108-
cap4.put("build", "BStack-[Java] Sample Build");
109-
cap4.put("name", "Thread 4");
89+
cap4.put("os", "windows");
11090
caps.add(cap4);
11191

11292
for (Hashtable<String, String> cap : caps) {
113-
Thread thread = new Thread(new ParallelTest(cap));
93+
Thread thread = new Thread(new ParallelTest(cap, "session name"));
11494
thread.start();
11595
}
11696
}
11797
}
118-

src/test/java/com/browserstack/app/JavaSample.java

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,54 @@
88
import org.openqa.selenium.remote.RemoteWebDriver;
99
import org.openqa.selenium.JavascriptExecutor;
1010
import java.net.URL;
11+
import java.time.Duration;
12+
import java.util.HashMap;
13+
1114
import org.openqa.selenium.support.ui.ExpectedConditions;
1215
import org.openqa.selenium.support.ui.WebDriverWait;
1316
public class JavaSample {
1417
public static final String AUTOMATE_USERNAME = "BROWSERSTACK_USERNAME";
1518
public static final String AUTOMATE_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY";
16-
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
17-
public static void main(String[] args) throws Exception {
18-
DesiredCapabilities caps = new DesiredCapabilities();
19-
caps.setCapability("browserName", "iPhone");
20-
caps.setCapability("device", "iPhone 11");
21-
caps.setCapability("realMobile", "true");
22-
caps.setCapability("local", "true");
23-
caps.setCapability("os_version", "14.0");
24-
caps.setCapability("name", "BStack-[Java] Sample Test"); // test name
25-
caps.setCapability("build", "BStack Build Number 1"); // CI/CD job or build name
26-
final WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
27-
try {
28-
driver.get("https://bstackdemo.com/");
29-
final WebDriverWait wait = new WebDriverWait(driver, 10);
30-
wait.until(ExpectedConditions.titleIs("StackDemo"));
31-
// getting name of the product
32-
String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText();
33-
// waiting for the Add to cart button to be clickable
34-
WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\'1\']/div[4]")));
35-
// clicking the 'Add to cart' button
36-
cart_btn.click();
37-
// checking if the Cart pane is visible
38-
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content")));
39-
// getting the product's name added in the cart
40-
final String product_in_cart = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'__next\']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).getText();
41-
// verifying whether the product added to cart is available in the cart
42-
if (product_name.equals(product_in_cart)) {
43-
markTestStatus("passed", "Product has been successfully added to the cart!", driver);
44-
}
45-
} catch (Exception e) {
46-
markTestStatus("failed", "Some elements failed to load", driver);
47-
}
48-
driver.quit();
19+
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
20+
public static void main(String[] args) throws Exception {
21+
DesiredCapabilities capabilities = new DesiredCapabilities();
22+
capabilities.setCapability("browserName", "Chrome");
23+
capabilities.setCapability("browserVersion", "latest");
24+
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
25+
browserstackOptions.put("os", "OS X");
26+
browserstackOptions.put("osVersion", "Sierra");
27+
browserstackOptions.put("local", "false");
28+
browserstackOptions.put("seleniumVersion", "4.0.0");
29+
capabilities.setCapability("bstack:options", browserstackOptions);
30+
capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name
31+
capabilities.setCapability("buildName", "BStack Build Number 1"); // CI/CD job or build name
32+
final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
33+
try {
34+
driver.get("https://bstackdemo.com/");
35+
final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
36+
wait.until(ExpectedConditions.titleIs("StackDemo"));
37+
// getting name of the product
38+
String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText();
39+
// waiting for the Add to cart button to be clickable
40+
WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\'1\']/div[4]")));
41+
// clicking the 'Add to cart' button
42+
cart_btn.click();
43+
// checking if the Cart pane is visible
44+
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content")));
45+
// getting the product's name added in the cart
46+
final String product_in_cart = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'__next\']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).getText();
47+
// verifying whether the product added to cart is available in the cart
48+
if (product_name.equals(product_in_cart)) {
49+
markTestStatus("passed", "Product has been successfully added to the cart!", driver);
50+
}
51+
} catch (Exception e) {
52+
markTestStatus("failed", "Some elements failed to load", driver);
53+
}
54+
driver.quit();
4955
}
5056
// This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack
5157
public static void markTestStatus(String status, String reason, WebDriver driver) {
52-
final JavascriptExecutor jse = (JavascriptExecutor) driver;
53-
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
54-
}
58+
final JavascriptExecutor jse = (JavascriptExecutor) driver;
59+
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
60+
}
5561
}

0 commit comments

Comments
 (0)