Skip to content

Commit 179f108

Browse files
committed
Update README and allow custom arguments to binary
1 parent eeff8a0 commit 179f108

File tree

3 files changed

+143
-33
lines changed

3 files changed

+143
-33
lines changed

README.md

Lines changed: 107 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,123 @@
22

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

5-
## API
5+
A simple Java wrapper for BrowserStack Local Binary.
66

7-
### Constructor
7+
## Example
88

9-
* `new Local()`: creates an instance of Local
9+
```
10+
import com.browserstack.local.Local;
1011
11-
### Methods
12+
# creates an instance of Local
13+
Local bsLocal = new Local();
1214
13-
* `start(options)`: starts Local instance with options. The options available are detailed below.
14-
* `stop()`: stops the Local instance
15-
* `isRunning()`: checks if Local instance is running
15+
# replace <browserstack-accesskey> with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
16+
HashMap<String, String> bsLocalArgs = new HashMap<String, String>();
17+
bsLocalArgs.put("key", "<browserstack-accesskey>");
1618
17-
### Options
19+
# starts the Local instance with the required arguments
20+
bsLocal.start(bsLocalArgs);
1821
19-
* `key`: BrowserStack Access Key
20-
* `v`: Provides verbose logging
21-
* `f`: If you want to test local folder rather internal server, provide path to folder as value of this option
22-
* `force`: Kill other running Browserstack Local
23-
* `only`: Restricts Local Testing access to specified local servers and/or folders
24-
* `forcelocal`: Route all traffic via local machine
25-
* `onlyAutomate`: Disable Live Testing and Screenshots, just test Automate
26-
* `proxyHost`: Hostname/IP of proxy, remaining proxy options are ignored if this option is absent
27-
* `proxyPort`: Port for the proxy, defaults to 3128 when -proxyHost is used
28-
* `proxyUser`: Username for connecting to proxy (Basic Auth Only)
29-
* `proxyPass`: Password for USERNAME, will be ignored if USERNAME is empty or not specified
30-
* `localIdentifier`: If doing simultaneous multiple local testing connections, set this uniquely for different processes
31-
* `hosts`: List of hosts and ports where Local must be enabled for eg. localhost,3000,1,localhost,3001,0
32-
* `logfile`: Path to file where Local logs be saved to
33-
* `binarypath`: Optional path to Local binary
22+
# check if BrowserStack local instance is running
23+
System.out.println(bsLocal.isRunning());
3424
25+
#stop the Local instance
26+
bsLocal.stop();
27+
```
3528

36-
## Build
29+
## Arguments
3730

38-
To build run, `mvn compile`.
31+
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 -
3932

33+
#### Verbose Logging
34+
To enable verbose logging -
35+
```
36+
bsLocalArgs.put("v", "true");
37+
```
4038

41-
## Tests
39+
#### Folder Testing
40+
To test local folder rather internal server, provide path to folder as value of this option -
41+
```
42+
bsLocalArgs.put("f", "/my/awesome/folder");
43+
```
44+
45+
#### Force Start
46+
To kill other running Browserstack Local instances -
47+
```
48+
bsLocalArgs.put("force", "true");
49+
```
50+
51+
#### Only Automate
52+
To disable local testing for Live and Screenshots, and enable only Automate -
53+
```
54+
bsLocalArgs.put("onlyAutomate", "true");
55+
```
56+
57+
#### Force Local
58+
To route all traffic via local(your) machine -
59+
```
60+
bsLocalArgs.put("forcelocal", "true");
61+
```
62+
63+
#### Proxy
64+
To use a proxy for local testing -
65+
66+
* proxyHost: Hostname/IP of proxy, remaining proxy options are ignored if this option is absent
67+
* proxyPort: Port for the proxy, defaults to 3128 when -proxyHost is used
68+
* proxyUser: Username for connecting to proxy (Basic Auth Only)
69+
* proxyPass: Password for USERNAME, will be ignored if USERNAME is empty or not specified
70+
71+
```
72+
bsLocalArgs.put("proxyHost", "127.0.0.1");
73+
bsLocalArgs.put("proxyPort", "8000");
74+
bsLocalArgs.put("proxyUser", "user");
75+
bsLocalArgs.put("proxyPass", "password");
76+
```
77+
78+
#### Local Identifier
79+
If doing simultaneous multiple local testing connections, set this uniquely for different processes -
80+
```
81+
bsLocalArgs.put("localIdentifier", "randomstring");
82+
```
83+
84+
## Additional Arguments
85+
86+
#### Binary Path
87+
88+
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.
89+
Path to specify local Binary path -
90+
```
91+
bsLocalArgs.put("binarypath", "/browserstack/BrowserStackLocal");
92+
```
93+
94+
#### Logfile
95+
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.
96+
To specify the path to file where the logs will be saved -
97+
```
98+
bsLocalArgs.put("v", "true");
99+
bsLocalArgs.put("logfile", "/browserstack/logs.txt");
100+
```
101+
102+
## Contribute
103+
104+
### Compile Instructions
105+
106+
To compile the package, `mvn compile`.
42107

43108
To run the test suite run, `mvn test`.
109+
110+
### Reporting bugs
111+
112+
You can submit bug reports either in the Github issue tracker.
113+
114+
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.
115+
116+
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.
117+
118+
### Pull Requests
119+
120+
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.
121+
122+
* Adhere to the coding conventions you see in the surrounding code.
123+
* Include tests, and make sure all tests pass.
124+
* 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.

src/main/java/com/browserstack/local/Local.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.FileWriter;
66
import java.util.ArrayList;
77
import java.util.HashMap;
8+
import java.util.Arrays;
89
import java.util.List;
910
import java.util.Map;
1011

@@ -45,14 +46,14 @@ public Local() {
4546
public void start(Map<String, String> options) throws Exception {
4647
command = new ArrayList<String>();
4748

48-
if (options.get("binarypath") != null) {
49-
command.add(options.get("binarypath"));
49+
if (options.get("-binarypath") != null) {
50+
command.add(options.get("-binarypath"));
5051
} else {
5152
LocalBinary lb = new LocalBinary();
5253
command.add(lb.getBinaryPath());
5354
}
5455

55-
String logFilePath = options.get("logfile") == null ?
56+
String logFilePath = options.get("-logfile") == null ?
5657
(System.getProperty("user.dir") + "/local.log") : options.get("logfile");
5758
command.add("-logFile");
5859
command.add(logFilePath);
@@ -130,13 +131,18 @@ public boolean isRunning() {
130131
*/
131132
private void makeCommand(Map<String, String> options) {
132133
for (Map.Entry<String, String> opt : options.entrySet()) {
134+
List<String> ignoreKeys = Arrays.asList("-key", "-logfile", "-binarypath");
133135
String parameter = opt.getKey().trim();
136+
if (ignoreKeys.contains(parameter)) {
137+
continue;
138+
}
134139
if (parameters.get(parameter) != null) {
135140
command.add(parameters.get(parameter));
136-
137-
if (opt.getValue() != null) {
138-
command.add(opt.getValue().trim());
139-
}
141+
} else {
142+
command.add(parameter);
143+
}
144+
if (opt.getValue() != null) {
145+
command.add(opt.getValue().trim());
140146
}
141147
}
142148
}

src/test/java/com/browserstack/local/BrowserStackLocalTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ public void testSetHosts() throws Exception {
132132
assertTrue(l.command.contains("localhost,8000,0"));
133133
}
134134

135+
@Test
136+
public void testCustomArguments() throws Exception {
137+
options.put("-customKey", "customValue");
138+
options.put("-customKey2", "customValue2");
139+
options.put("onlyCommand", "true");
140+
l.start(options);
141+
assertTrue(l.command.contains("-customKey"));
142+
assertTrue(l.command.contains("customValue"));
143+
assertTrue(l.command.contains("-customKey2"));
144+
assertTrue(l.command.contains("customValue2"));
145+
}
146+
147+
148+
@Test
149+
public void testCustomBoolArguments() throws Exception {
150+
options.put("-customKey", "");
151+
options.put("-customKey2", "");
152+
options.put("onlyCommand", "true");
153+
l.start(options);
154+
assertTrue(l.command.contains("-customKey"));
155+
assertTrue(l.command.contains("-customKey2"));
156+
}
157+
135158
@After
136159
public void tearDown() throws Exception {
137160
l.stop();

0 commit comments

Comments
 (0)