Skip to content

Commit 08eca87

Browse files
committed
Merge pull request #10 from browserstack/custom_args
Update README and allow custom arguments to binary
2 parents eeff8a0 + b48154e commit 08eca87

File tree

3 files changed

+168
-58
lines changed

3 files changed

+168
-58
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: 24 additions & 18 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

@@ -21,19 +22,19 @@ public class Local {
2122

2223
public Local() {
2324
parameters = new HashMap<String, String>();
24-
parameters.put("-v", "-vvv");
25-
parameters.put("-f", "-f");
26-
parameters.put("-force", "-force");
27-
parameters.put("-only", "-only");
28-
parameters.put("-forcelocal", "-forcelocal");
29-
parameters.put("-localIdentifier", "-localIdentifier");
30-
parameters.put("-onlyAutomate", "-onlyAutomate");
31-
parameters.put("-proxyHost", "-proxyHost");
32-
parameters.put("-proxyPort", "-proxyPort");
33-
parameters.put("-proxyUser", "-proxyUser");
34-
parameters.put("-proxyPass", "-proxyPass");
35-
parameters.put("-forceproxy", "-forceproxy");
36-
parameters.put("-hosts", "-hosts");
25+
parameters.put("v", "-vvv");
26+
parameters.put("f", "-f");
27+
parameters.put("force", "-force");
28+
parameters.put("only", "-only");
29+
parameters.put("forcelocal", "-forcelocal");
30+
parameters.put("localIdentifier", "-localIdentifier");
31+
parameters.put("onlyAutomate", "-onlyAutomate");
32+
parameters.put("proxyHost", "-proxyHost");
33+
parameters.put("proxyPort", "-proxyPort");
34+
parameters.put("proxyUser", "-proxyUser");
35+
parameters.put("proxyPass", "-proxyPass");
36+
parameters.put("forceproxy", "-forceproxy");
37+
parameters.put("hosts", "-hosts");
3738
}
3839

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

60-
command.add(options.get("-key"));
61+
command.add(options.get("key"));
6162
makeCommand(options);
6263

6364
if (options.get("onlyCommand") != null) return;
@@ -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: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class BrowserStackLocalTest {
1818
public void setUp() throws Exception {
1919
l = new Local();
2020
options = new HashMap<String, String>();
21-
options.put("-key", System.getenv("BROWSERSTACK_ACCESS_KEY"));
21+
options.put("key", System.getenv("BROWSERSTACK_ACCESS_KEY"));
2222
}
2323

2424
@Test
@@ -42,15 +42,15 @@ public void testMultipleBinary() throws Exception {
4242

4343
@Test
4444
public void testEnableVerbose() throws Exception {
45-
options.put("-v", "");
45+
options.put("v", "true");
4646
options.put("onlyCommand", "true");
4747
l.start(options);
4848
assertTrue(l.command.contains("-vvv"));
4949
}
5050

5151
@Test
5252
public void testSetFolder() throws Exception {
53-
options.put("-f", "/var/html");
53+
options.put("f", "/var/html");
5454
options.put("onlyCommand", "true");
5555
l.start(options);
5656
assertTrue(l.command.contains("-f"));
@@ -59,47 +59,47 @@ public void testSetFolder() throws Exception {
5959

6060
@Test
6161
public void testEnableForce() throws Exception {
62-
options.put("-force", "");
62+
options.put("force", "true");
6363
options.put("onlyCommand", "true");
6464
l.start(options);
6565
assertTrue(l.command.contains("-force"));
6666
}
6767

6868
@Test
6969
public void testEnableOnly() throws Exception {
70-
options.put("-only", "");
70+
options.put("only", "true");
7171
options.put("onlyCommand", "true");
7272
l.start(options);
7373
assertTrue(l.command.contains("-only"));
7474
}
7575

7676
@Test
7777
public void testEnableOnlyAutomate() throws Exception {
78-
options.put("-onlyAutomate", "");
78+
options.put("onlyAutomate", "true");
7979
options.put("onlyCommand", "true");
8080
l.start(options);
8181
assertTrue(l.command.contains("-onlyAutomate"));
8282
}
8383

8484
@Test
8585
public void testEnableForceLocal() throws Exception {
86-
options.put("-forcelocal", "");
86+
options.put("forcelocal", "true");
8787
options.put("onlyCommand", "true");
8888
l.start(options);
8989
assertTrue(l.command.contains("-forcelocal"));
9090
}
9191

9292
@Test
9393
public void testEnableForceProxy() throws Exception {
94-
options.put("-forceproxy", "");
94+
options.put("forceproxy", "true");
9595
options.put("onlyCommand", "true");
9696
l.start(options);
9797
assertTrue(l.command.contains("-forceproxy"));
9898
}
9999

100100
@Test
101101
public void testSetLocalIdentifier() throws Exception {
102-
options.put("-localIdentifier", "abcdef");
102+
options.put("localIdentifier", "abcdef");
103103
options.put("onlyCommand", "true");
104104
l.start(options);
105105
assertTrue(l.command.contains("-localIdentifier"));
@@ -108,10 +108,10 @@ public void testSetLocalIdentifier() throws Exception {
108108

109109
@Test
110110
public void testSetProxy() throws Exception {
111-
options.put("-proxyHost", "localhost");
112-
options.put("-proxyPort", "8080");
113-
options.put("-proxyUser", "user");
114-
options.put("-proxyPass", "pass");
111+
options.put("proxyHost", "localhost");
112+
options.put("proxyPort", "8080");
113+
options.put("proxyUser", "user");
114+
options.put("proxyPass", "pass");
115115
options.put("onlyCommand", "true");
116116
l.start(options);
117117
assertTrue(l.command.contains("-proxyHost"));
@@ -126,12 +126,35 @@ public void testSetProxy() throws Exception {
126126

127127
@Test
128128
public void testSetHosts() throws Exception {
129-
options.put("-hosts", "localhost,8000,0");
129+
options.put("hosts", "localhost,8000,0");
130130
options.put("onlyCommand", "true");
131131
l.start(options);
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("customKey1", "true");
151+
options.put("customKey2", "true");
152+
options.put("onlyCommand", "true");
153+
l.start(options);
154+
assertTrue(l.command.contains("-customKey1"));
155+
assertTrue(l.command.contains("-customKey2"));
156+
}
157+
135158
@After
136159
public void tearDown() throws Exception {
137160
l.stop();

0 commit comments

Comments
 (0)