Skip to content

Update README and allow custom arguments to binary #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2016
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
133 changes: 107 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,123 @@

[![Build Status](https://travis-ci.org/browserstack/browserstack-local-java.svg?branch=master)](https://travis-ci.org/browserstack/browserstack-local-java)

## API
A simple Java wrapper for BrowserStack Local Binary.

### Constructor
## Example

* `new Local()`: creates an instance of Local
```
import com.browserstack.local.Local;

### Methods
# creates an instance of Local
Local bsLocal = new Local();

* `start(options)`: starts Local instance with options. The options available are detailed below.
* `stop()`: stops the Local instance
* `isRunning()`: checks if Local instance is running
# replace <browserstack-accesskey> with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
bsLocalArgs.put("key", "<browserstack-accesskey>");

### Options
# starts the Local instance with the required arguments
bsLocal.start(bsLocalArgs);

* `key`: BrowserStack Access Key
* `v`: Provides verbose logging
* `f`: If you want to test local folder rather internal server, provide path to folder as value of this option
* `force`: Kill other running Browserstack Local
* `only`: Restricts Local Testing access to specified local servers and/or folders
* `forcelocal`: Route all traffic via local machine
* `onlyAutomate`: Disable Live Testing and Screenshots, just test Automate
* `proxyHost`: Hostname/IP of proxy, remaining proxy options are ignored if this option is absent
* `proxyPort`: Port for the proxy, defaults to 3128 when -proxyHost is used
* `proxyUser`: Username for connecting to proxy (Basic Auth Only)
* `proxyPass`: Password for USERNAME, will be ignored if USERNAME is empty or not specified
* `localIdentifier`: If doing simultaneous multiple local testing connections, set this uniquely for different processes
* `hosts`: List of hosts and ports where Local must be enabled for eg. localhost,3000,1,localhost,3001,0
* `logfile`: Path to file where Local logs be saved to
* `binarypath`: Optional path to Local binary
# check if BrowserStack local instance is running
System.out.println(bsLocal.isRunning());

#stop the Local instance
bsLocal.stop();
```

## Build
## Arguments

To build run, `mvn compile`.
Apart from the key, all other BrowserStack Local modifiers are optional. For the full list of modifiers, refer [BrowserStack Local modifiers](https://www.browserstack.com/local-testing#modifiers). For examples, refer below -

#### Verbose Logging
To enable verbose logging -
```
bsLocalArgs.put("v", "true");
```

## Tests
#### Folder Testing
To test local folder rather internal server, provide path to folder as value of this option -
```
bsLocalArgs.put("f", "/my/awesome/folder");
```

#### Force Start
To kill other running Browserstack Local instances -
```
bsLocalArgs.put("force", "true");
```

#### Only Automate
To disable local testing for Live and Screenshots, and enable only Automate -
```
bsLocalArgs.put("onlyAutomate", "true");
```

#### Force Local
To route all traffic via local(your) machine -
```
bsLocalArgs.put("forcelocal", "true");
```

#### Proxy
To use a proxy for local testing -

* proxyHost: Hostname/IP of proxy, remaining proxy options are ignored if this option is absent
* proxyPort: Port for the proxy, defaults to 3128 when -proxyHost is used
* proxyUser: Username for connecting to proxy (Basic Auth Only)
* proxyPass: Password for USERNAME, will be ignored if USERNAME is empty or not specified

```
bsLocalArgs.put("proxyHost", "127.0.0.1");
bsLocalArgs.put("proxyPort", "8000");
bsLocalArgs.put("proxyUser", "user");
bsLocalArgs.put("proxyPass", "password");
```

#### Local Identifier
If doing simultaneous multiple local testing connections, set this uniquely for different processes -
```
bsLocalArgs.put("localIdentifier", "randomstring");
```

## Additional Arguments

#### Binary Path

By default, BrowserStack local wrappers try downloading and executing the latest version of BrowserStack binary in ~/.browserstack or the present working directory or the tmp folder by order. But you can override these by passing the -binarypath argument.
Path to specify local Binary path -
```
bsLocalArgs.put("binarypath", "/browserstack/BrowserStackLocal");
```

#### Logfile
To save the logs to the file while running with the '-v' argument, you can specify the path of the file. By default the logs are saved in the local.log file in the present woring directory.
To specify the path to file where the logs will be saved -
```
bsLocalArgs.put("v", "true");
bsLocalArgs.put("logfile", "/browserstack/logs.txt");
```

## Contribute

### Compile Instructions

To compile the package, `mvn compile`.

To run the test suite run, `mvn test`.

### Reporting bugs

You can submit bug reports either in the Github issue tracker.

Before submitting an issue please check if there is already an existing issue. If there is, please add any additional information give it a "+1" in the comments.

When submitting an issue please describe the issue clearly, including how to reproduce the bug, which situations it appears in, what you expect to happen, what actually happens, and what platform (operating system and version) you are using.

### Pull Requests

We love pull requests! We are very happy to work with you to get your changes merged in, however, please keep the following in mind.

* Adhere to the coding conventions you see in the surrounding code.
* Include tests, and make sure all tests pass.
* Before submitting a pull-request, clean up the git history by going over your commits and squashing together minor changes and fixes into the corresponding commits. You can do this using the interactive rebase command.
42 changes: 24 additions & 18 deletions src/main/java/com/browserstack/local/Local.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand All @@ -21,19 +22,19 @@ public class Local {

public Local() {
parameters = new HashMap<String, String>();
parameters.put("-v", "-vvv");
parameters.put("-f", "-f");
parameters.put("-force", "-force");
parameters.put("-only", "-only");
parameters.put("-forcelocal", "-forcelocal");
parameters.put("-localIdentifier", "-localIdentifier");
parameters.put("-onlyAutomate", "-onlyAutomate");
parameters.put("-proxyHost", "-proxyHost");
parameters.put("-proxyPort", "-proxyPort");
parameters.put("-proxyUser", "-proxyUser");
parameters.put("-proxyPass", "-proxyPass");
parameters.put("-forceproxy", "-forceproxy");
parameters.put("-hosts", "-hosts");
parameters.put("v", "-vvv");
parameters.put("f", "-f");
parameters.put("force", "-force");
parameters.put("only", "-only");
parameters.put("forcelocal", "-forcelocal");
parameters.put("localIdentifier", "-localIdentifier");
parameters.put("onlyAutomate", "-onlyAutomate");
parameters.put("proxyHost", "-proxyHost");
parameters.put("proxyPort", "-proxyPort");
parameters.put("proxyUser", "-proxyUser");
parameters.put("proxyPass", "-proxyPass");
parameters.put("forceproxy", "-forceproxy");
parameters.put("hosts", "-hosts");
}

/**
Expand All @@ -57,7 +58,7 @@ public void start(Map<String, String> options) throws Exception {
command.add("-logFile");
command.add(logFilePath);

command.add(options.get("-key"));
command.add(options.get("key"));
makeCommand(options);

if (options.get("onlyCommand") != null) return;
Expand Down Expand Up @@ -130,13 +131,18 @@ public boolean isRunning() {
*/
private void makeCommand(Map<String, String> options) {
for (Map.Entry<String, String> opt : options.entrySet()) {
List<String> ignoreKeys = Arrays.asList("key", "logfile", "binarypath");
String parameter = opt.getKey().trim();
if (ignoreKeys.contains(parameter)) {
continue;
}
if (parameters.get(parameter) != null) {
command.add(parameters.get(parameter));

if (opt.getValue() != null) {
command.add(opt.getValue().trim());
}
} else {
command.add("-" + parameter);
}
if (opt.getValue() != null) {
command.add(opt.getValue().trim());
}
}
}
Expand Down
51 changes: 37 additions & 14 deletions src/test/java/com/browserstack/local/BrowserStackLocalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class BrowserStackLocalTest {
public void setUp() throws Exception {
l = new Local();
options = new HashMap<String, String>();
options.put("-key", System.getenv("BROWSERSTACK_ACCESS_KEY"));
options.put("key", System.getenv("BROWSERSTACK_ACCESS_KEY"));
}

@Test
Expand All @@ -42,15 +42,15 @@ public void testMultipleBinary() throws Exception {

@Test
public void testEnableVerbose() throws Exception {
options.put("-v", "");
options.put("v", "true");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-vvv"));
}

@Test
public void testSetFolder() throws Exception {
options.put("-f", "/var/html");
options.put("f", "/var/html");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-f"));
Expand All @@ -59,47 +59,47 @@ public void testSetFolder() throws Exception {

@Test
public void testEnableForce() throws Exception {
options.put("-force", "");
options.put("force", "true");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-force"));
}

@Test
public void testEnableOnly() throws Exception {
options.put("-only", "");
options.put("only", "true");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-only"));
}

@Test
public void testEnableOnlyAutomate() throws Exception {
options.put("-onlyAutomate", "");
options.put("onlyAutomate", "true");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-onlyAutomate"));
}

@Test
public void testEnableForceLocal() throws Exception {
options.put("-forcelocal", "");
options.put("forcelocal", "true");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-forcelocal"));
}

@Test
public void testEnableForceProxy() throws Exception {
options.put("-forceproxy", "");
options.put("forceproxy", "true");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-forceproxy"));
}

@Test
public void testSetLocalIdentifier() throws Exception {
options.put("-localIdentifier", "abcdef");
options.put("localIdentifier", "abcdef");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-localIdentifier"));
Expand All @@ -108,10 +108,10 @@ public void testSetLocalIdentifier() throws Exception {

@Test
public void testSetProxy() throws Exception {
options.put("-proxyHost", "localhost");
options.put("-proxyPort", "8080");
options.put("-proxyUser", "user");
options.put("-proxyPass", "pass");
options.put("proxyHost", "localhost");
options.put("proxyPort", "8080");
options.put("proxyUser", "user");
options.put("proxyPass", "pass");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-proxyHost"));
Expand All @@ -126,12 +126,35 @@ public void testSetProxy() throws Exception {

@Test
public void testSetHosts() throws Exception {
options.put("-hosts", "localhost,8000,0");
options.put("hosts", "localhost,8000,0");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("localhost,8000,0"));
}

@Test
public void testCustomArguments() throws Exception {
options.put("customKey", "customValue");
options.put("customKey2", "customValue2");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-customKey"));
assertTrue(l.command.contains("customValue"));
assertTrue(l.command.contains("-customKey2"));
assertTrue(l.command.contains("customValue2"));
}


@Test
public void testCustomBoolArguments() throws Exception {
options.put("customKey1", "true");
options.put("customKey2", "true");
options.put("onlyCommand", "true");
l.start(options);
assertTrue(l.command.contains("-customKey1"));
assertTrue(l.command.contains("-customKey2"));
}

@After
public void tearDown() throws Exception {
l.stop();
Expand Down