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
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
*.class
*.jar
*.war
*.ear
*.png
target/
local.log
target
.idea
*.iml
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: java

jdk:
- oraclejdk8
- oraclejdk7
- openjdk6

before_install:
- true && `base64 --decode <<< ZXhwb3J0IEJST1dTRVJTVEFDS19BQ0NFU1NfS0VZPUh5VmZydXJvb3dYb041eGhLZEs2Cg==`

after_failure: cat /home/travis/build/browserstack/browserstack-local-java/target/surefire-reports/*

scripts:
- mvn test -P single && mvn test -P local && mvn test -P parallel
43 changes: 17 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
# JBehave-Browserstack
# jbehave-browserstack

Execute [JBehave](https://github.com/jbehave/jbehave-core) scripts on BrowserStack.
[JBehave](https://github.com/jbehave/jbehave-core) Integration with BrowserStack.

## Usage
## Setting up a sample test

### Prerequisites
- Clone the repo
- Install dependencies `mvn install`
- Update `*.conf.json` files inside the `src/test/resources/conf` directory with your BrowserStack Username and Access Key. (These can be found in the [settings](https://www.browserstack.com/accounts/settings) section on BrowserStack accounts page)

Java and Maven
## Running the sample test

### Clone the repo
- To run single test, run `mvn test -P single`
- To run parallel tests, run `mvn test -P parallel`
- To run local tests, run `mvn test -P local`

`git clone https://github.com/browserstack/jbehave-browserstack.git`
## Integrating with your existing test suite

### BrowserStack Authentication
- Update `pom.xml` in your project.
- Copy `src/test/java/com/browserstack/BrowserStackJBehaveRunner.java` and `src/test/resources/conf/*` to your project.
- Adjust your configuration files as per `src/test/resources/conf/*.conf.json`. Here is the full list of [BrowserStack configuration capabilities](https://www.browserstack.com/automate/capabilities)

Export the environment variables for the username and access key of your BrowserStack account.
These can be found on the automate accounts page on [BrowserStack](https://www.browserstack.com/accounts/automate)
## Notes

`export BROWSERSTACK_USERNAME=<browserstack-username>`

`export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>`

### Run the tests

- To start a single test, run: `mvn clean install`
- To start parallel tests, run: `TEST_TYPE=parallel mvn clean install`
- To start local tests, run: `TEST_TYPE=local mvn clean install`
- To start local tests in parallel, run: `TEST_TYPE=local,parallel mvn clean install`

------

#### How to specify the capabilities

The [Code Generator](https://www.browserstack.com/automate/node#setting-os-and-browser) can come in very handy when specifying the capabilities especially for mobile devices.
- In order to test on different set of browsers, check out our [code generator](https://www.browserstack.com/automate/python#setting-os-and-browser)
- You can export the environment variables for the Username and Access Key of your BrowserStack account using `export BROWSERSTACK_USERNAME=<browserstack-username> && export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>`
167 changes: 119 additions & 48 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,51 +1,122 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.browserstack</groupId>
<artifactId>jbehave-browserstack</artifactId>
<version>0.1.0</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.35.0</version>
</dependency>
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>browserstack-local-java</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-core</artifactId>
<version>3.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<test>StoryRunner.java</test>
<parallel>methods</parallel>
</configuration>
</plugin>
</plugins>
</build>
<modelVersion>4.0.0</modelVersion>
<groupId>com.browserstack</groupId>
<artifactId>jbehave-browserstack</artifactId>
<version>0.1.0</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.35.0</version>
</dependency>
<dependency>
<groupId>com.browserstack</groupId>
<artifactId>browserstack-local-java</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave-core</artifactId>
<version>3.8</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>single</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<test>com/browserstack/BrowserStackJBehaveRunner.java</test>
<systemPropertyVariables>
<config>single.conf.json</config>
<embedder>com.browserstack.single.SingleEmbedder</embedder>
<stories>stories/single.story</stories>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>local</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<test>com/browserstack/BrowserStackJBehaveRunner.java</test>
<systemPropertyVariables>
<config>local.conf.json</config>
<embedder>com.browserstack.single.LocalEmbedder</embedder>
<stories>stories/local.story</stories>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>parallel</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<test>com/browserstack/BrowserStackJBehaveRunner.java</test>
<parallel>methods</parallel>
<systemPropertyVariables>
<config>parallel.conf.json</config>
<embedder>com.browserstack.single.SingleEmbedder</embedder>
<stories>stories/single.story</stories>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
119 changes: 119 additions & 0 deletions src/test/java/com/browserstack/BrowserStackJBehaveRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.browserstack;
import com.browserstack.local.Local;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
import java.util.Collection;
import java.lang.reflect.Constructor;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;

import org.jbehave.core.embedder.Embedder;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.Parameter;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;


@RunWith(Parameterized.class)
public class BrowserStackJBehaveRunner {

public WebDriver driver;
private Local l;

private static JSONObject config;

@Parameter(value = 0)
public int taskID;

@Parameters
public static Collection<Object[]> data() throws Exception {
List<Object[]> taskIDs = new ArrayList<Object[]>();
if(System.getProperty("config") != null) {
JSONParser parser = new JSONParser();
config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + System.getProperty("config")));
int envs = ((JSONArray)config.get("environments")).size();

for(int i=0; i<envs; i++) {
taskIDs.add(new Object[] {i});
}
}

return taskIDs;
}

@Before
public void setUp() throws Exception {
JSONArray envs = (JSONArray) config.get("environments");

DesiredCapabilities capabilities = new DesiredCapabilities();

Map<String, String> envCapabilities = (Map<String, String>) envs.get(taskID);
Iterator it = envCapabilities.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
}

Map<String, String> commonCapabilities = (Map<String, String>) config.get("capabilities");
it = commonCapabilities.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
if(capabilities.getCapability(pair.getKey().toString()) == null){
capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
}
}

String username = System.getenv("BROWSERSTACK_USERNAME");
if(username == null) {
username = (String) config.get("user");
}

String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
if(accessKey == null) {
accessKey = (String) config.get("key");
}

if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){
l = new Local();
Map<String, String> options = new HashMap<String, String>();
options.put("key", accessKey);
l.start(options);
}

driver = new RemoteWebDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
}

@After
public void tearDown() throws Exception {
driver.quit();
if(l != null) l.stop();
}

@Test
public void runStories() throws Exception {
Class<?> c = Class.forName(System.getProperty("embedder"));
Constructor<?> cons = c.getConstructor(WebDriver.class);
Embedder storyEmbedder = (Embedder) cons.newInstance(driver);

List<String> storyPaths = Arrays.asList(System.getProperty("stories"));
storyEmbedder.runStoriesAsPaths(storyPaths);
}
}
Loading