Skip to content
Open
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ target/
.vscode
browserstack.err
.idea/
logs/
/bin/
.classpath
.project
.settings
84 changes: 56 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,25 @@ Make sure `maven` is installed in your system. See if it is properly installed.
mvn --version
```

## Steps to run test

In every test file (JavaSample, JavaLocalSample, JavaParallelSample) make sure you set your credentials.
```java
public static final String AUTOMATE_USERNAME = "BROWSERSTACK_USERNAME";
public static final String AUTOMATE_ACCESS_KEY = "BROWSERSTACK_ACCESS_KEY";
```
## Steps to run local test (using Java SDK)

1. Clone and navigate to the repo.
```
git clone -b sdk https://github.com/browserstack/java-selenium-browserstack.git
cd java-selenium-browserstack
```

```
git clone https://github.com/browserstack/java-selenium-browserstack.git
cd java-selenium-browserstack
```
2. Set your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings) in [browserstack.yml](browserstack.yml)

2. Change capabilities of test.

```java
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
```
3. Add `browserstack-java-sdk` in your `pom.xml` as below.
```
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>browserstack-java-sdk</artifactId>
<version>LATEST</version>
<scope>compile</scope>
</dependency>
```

## Build and run test using maven.

Expand All @@ -47,15 +39,51 @@ mvn install

a. To run single test session.
```
mvn -Dexec.mainClass="com.browserstack.app.JavaSample" -Dexec.classpathScope=test test-compile exec:java
mvn exec:exec -Dexec.executable="java" -Dexec.classpathScope=test -Dexec.args="-cp %classpath -javaagent:/path/to/.m2/repository/browserstack-java-sdk/jar com.browserstack.app.JavaSample"
```

b. To run local test session.
- Ensure that you have set `browserstackLocal: true` in your `browserstack.yml` config.
```
mvn exec:exec -Dexec.executable="java" -Dexec.classpathScope=test -Dexec.args="-cp %classpath -javaagent:/path/to/.m2/repository/browserstack-java-sdk/jar com.browserstack.app.JavaLocalSample"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a sample command, for windows and osx as mentioned in docs.
Both for single test and local test session or let's document exec plugin command

```

b. To run parallel test session.
### Arguments

For the full list of arguments that can be passed in `browserStackLocalOptions`, refer [BrowserStack Local modifiers](https://www.browserstack.com/docs/local-testing/binary-params). For examples, refer below -

#### Verbose Logging
To enable verbose logging -
```java
browserStackLocalOptions:
v: true
logFile: /browserstack/logs.txt
```
mvn -Dexec.mainClass="com.browserstack.app.JavaParallelSample" -Dexec.classpathScope=test test-compile exec:java

#### Force Start
To kill other running Browserstack Local instances -
```java
browserStackLocalOptions:
force: true
```

c. To run local test session.
#### Only Automate
To disable local testing for Live and Screenshots, and enable only Automate -
```java
browserStackLocalOptions:
onlyAutomate: true
```
mvn -Dexec.mainClass="com.browserstack.app.JavaLocalSample" -Dexec.classpathScope=test test-compile exec:java

#### Force Local
To route all traffic via local(your) machine -
```java
browserStackLocalOptions:
forcelocal: true
```

#### Local Identifier
If doing simultaneous multiple local testing connections, set this uniquely for different processes -
```java
browserStackLocalOptions:
localIdentifier: randomstring
```
47 changes: 47 additions & 0 deletions browserstack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# =============================
# Set BrowserStack Credentials
# =============================
# Add your BrowserStack userName and accessKey here or set BROWSERSTACK_USERNAME and
# BROWSERSTACK_ACCESS_KEY as env variables
userName: BROWSERSTACK_USERNAME
accessKey: BROWSERSTACK_ACCESS_KEY

# ======================
# Organizing your tests
# ======================
# Use `projectName`, `buildName`, `name` capabilities to organise your tests
# `name` is the name of your test sessions and is automatically picked from your
# test name and doesn't need to be set manually when using BrowserStack SDK
# `buildName` is used to name your CI/CD job or the execution of your test suite.
# Ensure you add a dynamic identifier, like an incremental build number from your
# CI/CD or timestamp at the end of every build; otherwise tests from different
# executions will be grouped together on BrowserStack
buildName: browserstack-build-1
# Use `projectName` to set the name of your project. Example, Marketing Website
projectName: BrowserStack Samples

platforms:
- os: OS X
osVersion: Big Sur
browserName: Chrome
browserVersion: latest
- os: Windows
osVersion: 10
browserName: Edge
browserVersion: latest
- device: Samsung Galaxy S22 Ultra
browserName: chrome # Try 'samsung' for Samsung browser
osVersion: 12.0

# ==========================================
# BrowserStack Local
# (For localhost, staging/private websites)
# ==========================================
# Set browserStackLocal to true if your website under test is not accessible publicly over the internet
# Learn more about how BrowserStack Local works here -> https://www.browserstack.com/docs/automate/selenium/local-testing-introduction
browserstackLocal: true # <boolean> (Default false)
#browserStackLocalOptions:
# Options to be passed to BrowserStack local in-case of advanced configurations
# localIdentifier: browserStackSDK # <string> (Default: null) Needed if you need to run multiple instances of local.
# forceLocal: true # <boolean> (Default: false) Set to true if you need to resolve all your traffic via BrowserStack Local tunnel.
# Entire list of arguments available here -> https://www.browserstack.com/docs/automate/selenium/manage-incoming-connections
142 changes: 65 additions & 77 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,97 +16,85 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<browserstack.sdk.version>1.0.8</browserstack.sdk.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.0</version>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>browserstack-local-java</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<artifactId>browserstack-java-sdk</artifactId>
<version>LATEST</version>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 1.0.8 version as we are using it below in exec plugin.

<scope>compile</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<!-- To run sample-test profile, mvn exec:exec -P sample-test -->
<profile>
<id>sample-test</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<classpathScope>test</classpathScope>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>-javaagent:${settings.localRepository}/com/browserstack/browserstack-java-sdk/${browserstack.sdk.version}/browserstack-java-sdk-${browserstack.sdk.version}.jar</argument>
<argument>com.browserstack.app.JavaSample</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
<profile>
<id>single</id>
<!-- To run sample-local-test profile, mvn exec:exec -P sample-local-test -->
<id>sample-local-test</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>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<classpathScope>test</classpathScope>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>-javaagent:${settings.localRepository}/com/browserstack/browserstack-java-sdk/${browserstack.sdk.version}/browserstack-java-sdk-${browserstack.sdk.version}.jar</argument>
<argument>com.browserstack.app.JavaLocalSample</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
Expand Down
36 changes: 7 additions & 29 deletions src/test/java/com/browserstack/app/JavaLocalSample.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,31 @@
package com.browserstack.app;

//Sample test in Java to run Automate session.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

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;
import com.browserstack.local.Local;

public class JavaLocalSample {
public static final String AUTOMATE_USERNAME = System.getenv("BROWSERSTACK_USERNAME") != null ? System.getenv("BROWSERSTACK_USERNAME") : "BROWSERSTACK_USERNAME";
public static final String AUTOMATE_ACCESS_KEY = System.getenv("BROWSERSTACK_ACCESS_KEY") != null ? System.getenv("BROWSERSTACK_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 capabilities = new DesiredCapabilities();
MutableCapabilities capabilities = new MutableCapabilities();
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");
browserstackOptions.put("sessionName", "BStack-[Java] Sample Test"); // test name
browserstackOptions.put("buildName", "BStack Local Build Number 1"); // CI/CD job or build name
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);

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

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

final WebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
try {
Expand All @@ -58,10 +38,8 @@ public static void main(String[] args) throws Exception {
markTestStatus("passed", "Local Test is successful and up and running", driver);
}
} catch (Exception e) {
markTestStatus("failed", "Could'nt connect the local", driver);
markTestStatus("failed", "Couldn't connect the local", driver);
}
//Stop the Local instance
bsLocal.stop();
driver.quit();
}

Expand Down
Loading