Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target/
.vscode
browserstack.err
.idea/
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ In every test file (JavaSample, JavaLocalSample, JavaParallelSample) make sure y
2. Change capabilities of test.

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

## Build and run test using maven.
Expand All @@ -58,4 +58,4 @@ b. To run parallel test session.
c. To run local test session.
```
mvn -Dexec.mainClass="com.browserstack.app.JavaLocalSample" -Dexec.classpathScope=test test-compile exec:java
```
```
20 changes: 19 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.1.0</version>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.browserstack</groupId>
Expand Down Expand Up @@ -92,4 +92,22 @@
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>single</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>com/browserstack/app/JavaSample.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
88 changes: 46 additions & 42 deletions src/test/java/com/browserstack/app/JavaLocalSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.JavascriptExecutor;
import java.net.URL;
import java.time.Duration;
import java.util.HashMap;

import org.openqa.selenium.support.ui.ExpectedConditions;
Expand All @@ -16,50 +17,53 @@
public class JavaLocalSample {
public static final String AUTOMATE_USERNAME = "BROWSERSTACK_USERNAME";
public static final String AUTOMATE_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY";
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("os", "OS X");
caps.setCapability("os_version", "Sierra");
caps.setCapability("browser", "Safari");
caps.setCapability("browser_version", "10.0");
caps.setCapability("browserstack.local", "true");
caps.setCapability("name", "BStack-[Java] Sample Local Test"); // test name
caps.setCapability("build", "BStack Build Number 1"); // CI/CD job or build name

//Creates an instance of Local
Local bsLocal = new Local();
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("browserVersion", "latest");
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
browserstackOptions.put("os", "OS X");
browserstackOptions.put("osVersion", "Sierra");
browserstackOptions.put("local", "true");
browserstackOptions.put("seleniumVersion", "4.0.0");
capabilities.setCapability("bstack:options", browserstackOptions);
capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name
capabilities.setCapability("buildName", "BStack Local Build Number 1"); // CI/CD job or build name

//Creates an instance of Local
Local bsLocal = new Local();

// You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
bsLocalArgs.put("key", AUTOMATE_ACCESS_KEY);
// You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
bsLocalArgs.put("key", AUTOMATE_ACCESS_KEY);

// Starts the Local instance with the required arguments
bsLocal.start(bsLocalArgs);
// Starts the Local instance with the required arguments
bsLocal.start(bsLocalArgs);

// Check if BrowserStack local instance is running
System.out.println(bsLocal.isRunning());
// Check if BrowserStack local instance is running
System.out.println(bsLocal.isRunning());

final WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
try {
driver.get("http://bs-local.com:45691/check");
final WebDriverWait wait = new WebDriverWait(driver, 10);
// getting name of the product
String bodyText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body"))).getText();
if (bodyText.equals("Up and running")) {
markTestStatus("passed", "Local Test is successful and up and running", driver);
}
} catch (Exception e) {
markTestStatus("failed", "Could'nt connect the local", driver);
}
//Stop the Local instance
bsLocal.stop();
driver.quit();
}
// This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack
public static void markTestStatus(String status, String reason, WebDriver driver) {
final JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
}
final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
try {
driver.get("http://bs-local.com:45691/check");
final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// getting name of the product
String bodyText = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("body"))).getText();

if (bodyText.equals("Up and running")) {
markTestStatus("passed", "Local Test is successful and up and running", driver);
}
} catch (Exception e) {
markTestStatus("failed", "Could'nt connect the local", driver);
}
//Stop the Local instance
bsLocal.stop();
driver.quit();
}
// This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack
public static void markTestStatus(String status, String reason, WebDriver driver) {
final JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
}
}
55 changes: 17 additions & 38 deletions src/test/java/com/browserstack/app/JavaParallelSample.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.browserstack.app;
import java.util.*;
import java.util.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
Expand All @@ -14,36 +13,31 @@
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


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

ParallelTest(Hashtable<String, String> cap){
ParallelTest(Hashtable<String, String> cap, String sessionString){
capsHashtable = cap;
sessionName = sessionString;
}

@Override
public void run() {
String key;
DesiredCapabilities caps = new DesiredCapabilities();
// Iterate over the hashtable and set the capabilities
Set<String> keys = capsHashtable.keySet();
Iterator<String> keysIterator = keys.iterator();
while (keysIterator.hasNext()) {
key = keysIterator.next();
caps.setCapability(key, capsHashtable.get(key));
}
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("bstack:options", capsHashtable);
caps.setCapability("sessionName", sessionName); // test name
caps.setCapability("buildName", "BStack-[Java] Sample buildName"); // CI/CD job or build name
WebDriver driver;
try {
driver = new RemoteWebDriver(new URL(URL), caps);
final JavascriptExecutor jse = (JavascriptExecutor) driver;
try {
// Searching for 'BrowserStack' on google.com
driver.get("https://bstackdemo.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.titleIs("StackDemo"));
// Getting name of the product
String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText();
Expand All @@ -68,51 +62,36 @@ public void run() {
}
}
}
public class JavaParallelSample {

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

//device 1
Hashtable<String, String> cap1 = new Hashtable<String, String>();
cap1.put("device", "iPhone 12 Pro");
cap1.put("real_mobile", "true");
cap1.put("build", "BStack-[Java] Sample Build");
cap1.put("name", "Thread 1");
cap1.put("deviceName", "iPhone 12 Pro");
cap1.put("realMobile", "true");
caps.add(cap1);

//device 2
Hashtable<String, String> cap2 = new Hashtable<String, String>();
cap2.put("device", "Samsung Galaxy S20");
cap2.put("real_mobile", "true");
cap2.put("build", "BStack-[Java] Sample Build");
cap2.put("name", "Thread 2");
cap2.put("deviceName", "Samsung Galaxy S20");
cap2.put("realMobile", "true");
caps.add(cap2);

//device 3
Hashtable<String, String> cap3 = new Hashtable<String, String>();
cap3.put("browser", "safari");
cap3.put("browser_version", "14");
cap3.put("os", "OS X");
cap3.put("os_version", "Big Sur");
cap3.put("build", "BStack-[Java] Sample Build");
cap3.put("name", "Thread 3");
caps.add(cap3);

//device 4
Hashtable<String, String> cap4 = new Hashtable<String, String>();
cap4.put("browser", "Chrome");
cap4.put("browser_version", "latest");
cap4.put("os", "OS X");
cap4.put("os_version", "Monterey");
cap4.put("build", "BStack-[Java] Sample Build");
cap4.put("name", "Thread 4");
cap4.put("os", "windows");
caps.add(cap4);

for (Hashtable<String, String> cap : caps) {
Thread thread = new Thread(new ParallelTest(cap));
Thread thread = new Thread(new ParallelTest(cap, "session name"));
thread.start();
}
}
}

78 changes: 42 additions & 36 deletions src/test/java/com/browserstack/app/JavaSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,54 @@
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.JavascriptExecutor;
import java.net.URL;
import java.time.Duration;
import java.util.HashMap;

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class JavaSample {
public static final String AUTOMATE_USERNAME = "BROWSERSTACK_USERNAME";
public static final String AUTOMATE_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY";
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("browserName", "iPhone");
caps.setCapability("device", "iPhone 11");
caps.setCapability("realMobile", "true");
caps.setCapability("local", "true");
caps.setCapability("os_version", "14.0");
caps.setCapability("name", "BStack-[Java] Sample Test"); // test name
caps.setCapability("build", "BStack Build Number 1"); // CI/CD job or build name
final WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
try {
driver.get("https://bstackdemo.com/");
final WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.titleIs("StackDemo"));
// getting name of the product
String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText();
// waiting for the Add to cart button to be clickable
WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\'1\']/div[4]")));
// clicking the 'Add to cart' button
cart_btn.click();
// checking if the Cart pane is visible
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content")));
// getting the product's name added in the cart
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();
// verifying whether the product added to cart is available in the cart
if (product_name.equals(product_in_cart)) {
markTestStatus("passed", "Product has been successfully added to the cart!", driver);
}
} catch (Exception e) {
markTestStatus("failed", "Some elements failed to load", driver);
}
driver.quit();
public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("browserVersion", "latest");
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
browserstackOptions.put("os", "OS X");
browserstackOptions.put("osVersion", "Sierra");
browserstackOptions.put("local", "false");
browserstackOptions.put("seleniumVersion", "4.0.0");
capabilities.setCapability("bstack:options", browserstackOptions);
capabilities.setCapability("sessionName", "BStack-[Java] Sample Test"); // test name
capabilities.setCapability("buildName", "BStack Build Number 1"); // CI/CD job or build name
final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
try {
driver.get("https://bstackdemo.com/");
final WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.titleIs("StackDemo"));
// getting name of the product
String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText();
// waiting for the Add to cart button to be clickable
WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\'1\']/div[4]")));
// clicking the 'Add to cart' button
cart_btn.click();
// checking if the Cart pane is visible
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content")));
// getting the product's name added in the cart
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();
// verifying whether the product added to cart is available in the cart
if (product_name.equals(product_in_cart)) {
markTestStatus("passed", "Product has been successfully added to the cart!", driver);
}
} catch (Exception e) {
markTestStatus("failed", "Some elements failed to load", driver);
}
driver.quit();
}
// This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack
public static void markTestStatus(String status, String reason, WebDriver driver) {
final JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
}
final JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
}
}