Skip to content

Commit 3703a70

Browse files
committed
added unit tests
1 parent 1c6e897 commit 3703a70

File tree

5 files changed

+151
-91
lines changed

5 files changed

+151
-91
lines changed

src/main/java/com/BrowserStack/BrowserStackLocalException.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/java/com/BrowserStack/Local.java

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,28 @@
1414

1515
class Local {
1616

17-
Process BrowserStackLocal = null;
1817
List<String> command;
18+
Process proc = null;
1919
String logFilePath;
2020
HashMap<String, String> parameters;
21+
22+
Local() throws Exception {
23+
parameters = new HashMap<String, String>();
24+
parameters.put("v","-vvv");
25+
parameters.put("f","-f");
26+
parameters.put("h","-h");
27+
parameters.put("version", "-version");
28+
parameters.put("force", "-force");
29+
parameters.put("only", "-only");
30+
parameters.put("forcelocal", "-forcelocal");
31+
parameters.put("localIdentifier", "-localIdentifier");
32+
parameters.put("onlyAutomate", "-onlyAutomate");
33+
parameters.put("proxyHost", "-proxyHost");
34+
parameters.put("proxyPort", "-proxyPort");
35+
parameters.put("proxyUser", "-proxyUser");
36+
parameters.put("proxyPass", "-proxyPass");
37+
parameters.put("hosts", "-hosts");
38+
}
2139

2240
void start(HashMap<String,String> options) throws Exception {
2341
command = new ArrayList<String>();
@@ -29,15 +47,17 @@ void start(HashMap<String,String> options) throws Exception {
2947
LocalBinary lb = new LocalBinary();
3048
command.add(lb.binary_path);
3149
}
32-
50+
3351
logFilePath = options.get("logfile") == null ? (System.getProperty("user.dir") + "/local.log") : options.get("logfile");
3452
command.add("-logFile");
3553
command.add(logFilePath);
3654

3755
command.add(options.get("key"));
3856
makeCommand(options);
57+
58+
if(options.get("onlyCommand") != null) return;
3959

40-
if (BrowserStackLocal == null){
60+
if (proc == null){
4161
ProcessBuilder processBuilder = new ProcessBuilder(command);
4262

4363
if((new File(logFilePath)).exists()){
@@ -46,7 +66,7 @@ void start(HashMap<String,String> options) throws Exception {
4666
f.close();
4767
}
4868

49-
BrowserStackLocal = processBuilder.start();
69+
proc = processBuilder.start();
5070
FileReader f = new FileReader(logFilePath);
5171
BufferedReader reader = new BufferedReader(f);
5272
String string;
@@ -62,50 +82,35 @@ void start(HashMap<String,String> options) throws Exception {
6282

6383
if (string.contains("*** Error")){
6484
f.close();
65-
throw new BrowserStackLocalException(string);
85+
stop();
86+
throw new LocalException(string);
6687
}
6788
}
6889

6990
}
7091
}
7192

72-
void stop(){
73-
if (BrowserStackLocal != null) {
74-
BrowserStackLocal.destroy();
93+
void stop() throws InterruptedException {
94+
if (proc != null) {
95+
proc.destroy();
96+
while(isRunning()){
97+
Thread.sleep(1000);
98+
}
7599
}
76100
}
77101

78102
boolean isRunning(){
103+
if(proc == null) return false;
104+
79105
try {
80-
BrowserStackLocal.exitValue();
81-
return false;
106+
proc.exitValue();
107+
return false;
82108
}
83-
catch (Exception e) {
84-
return true;
109+
catch (IllegalThreadStateException e){
110+
return true;
85111
}
86112
}
87113

88-
void init(){
89-
parameters = new HashMap<String, String>();
90-
parameters.put("v","-v");
91-
parameters.put("f","-f");
92-
parameters.put("h","-h");
93-
parameters.put("version", "-version");
94-
parameters.put("force", "-force");
95-
parameters.put("only", "-only");
96-
parameters.put("forcelocal", "-forcelocal");
97-
parameters.put("onlyAutomate", "-onlyAutomate");
98-
parameters.put("proxyHost", "-proxyHost");
99-
parameters.put("proxyPort", "-proxyPort");
100-
parameters.put("proxyUser", "-proxyUser");
101-
parameters.put("proxyPass", "-proxyPass");
102-
parameters.put("hosts", "-hosts");
103-
}
104-
105-
Local() throws Exception {
106-
init();
107-
}
108-
109114
void makeCommand(HashMap<String,String> options){
110115
Set set = options.entrySet();
111116
Iterator i = set.iterator();

src/main/java/com/BrowserStack/LocalBinary.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ String getAvailableDirectory() throws Exception {
6666
else
6767
i++;
6868
}
69-
throw new BrowserStackLocalException("Error trying to download BrowserStackLocal binary");
69+
throw new LocalException("Error trying to download BrowserStackLocal binary");
7070
}
7171

7272
boolean makePath(String path) {
@@ -80,7 +80,7 @@ boolean makePath(String path) {
8080
}
8181
}
8282

83-
Boolean downloadBinary(String dest_parent_dir) throws BrowserStackLocalException{
83+
Boolean downloadBinary(String dest_parent_dir) throws LocalException{
8484
try{
8585
if (!new File(dest_parent_dir).exists())
8686
new File(dest_parent_dir).mkdirs();
@@ -96,7 +96,7 @@ Boolean downloadBinary(String dest_parent_dir) throws BrowserStackLocalException
9696
return true;
9797
}
9898
catch (Exception e){
99-
throw new BrowserStackLocalException("Error trying to download BrowserStackLocal binary");
99+
throw new LocalException("Error trying to download BrowserStackLocal binary");
100100
}
101101
}
102102

src/sample/java/com/BrowserStack/BrowserStackLocalExample.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.net.URL;
44
import java.util.HashMap;
55

6-
import org.junit.*;
7-
86
import org.openqa.selenium.WebDriver;
97
import org.openqa.selenium.remote.DesiredCapabilities;
108
import org.openqa.selenium.remote.RemoteWebDriver;
@@ -13,8 +11,7 @@ public class BrowserStackLocalExample {
1311
private WebDriver driver;
1412
Local l;
1513

16-
@Before
17-
public void setUp() throws Exception {
14+
public static void main() throws Exception {
1815
String username = System.getenv("BROWSERSTACK_USER");
1916
String access_key = System.getenv("BROWSERSTACK_ACCESS_KEY");
2017

@@ -38,18 +35,12 @@ public void setUp() throws Exception {
3835
System.out.println("Starting session");
3936
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.browserstack.com/wd/hub"), caps);
4037
System.out.println("Started session");
41-
}
42-
43-
@Test
44-
public void testSimple() throws Exception {
38+
4539
driver.get("http://localhost");
4640
System.out.println("Process is running : " + l.isRunning());
4741
System.out.println("Page title is: " + driver.getTitle());
48-
}
4942

50-
@After
51-
public void tearDown() throws Exception {
52-
driver.quit();
53-
l.stop();
54-
}
43+
driver.quit();
44+
l.stop();
45+
}
5546
}
Lines changed: 105 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,126 @@
11
package com.BrowserStack;
22

3-
import java.net.URL;
43
import java.util.HashMap;
5-
64
import org.junit.*;
5+
import static org.junit.Assert.*;
76

8-
import org.openqa.selenium.WebDriver;
9-
import org.openqa.selenium.remote.DesiredCapabilities;
10-
import org.openqa.selenium.remote.RemoteWebDriver;
11-
12-
public class BrowserStackLocalTest {
13-
private WebDriver driver;
7+
public class BrowserStackLocalTest {
148
Local l;
9+
HashMap<String, String> options;
1510

1611
@Before
1712
public void setUp() throws Exception {
18-
String username = System.getenv("BROWSERSTACK_USER");
19-
String access_key = System.getenv("BROWSERSTACK_ACCESS_KEY");
13+
l = new Local();
14+
options = new HashMap<String, String>();
15+
options.put("key", System.getenv("BROWSERSTACK_ACCESS_KEY"));
16+
}
2017

21-
DesiredCapabilities caps = new DesiredCapabilities();
22-
caps.setCapability("browser", "Firefox");
23-
caps.setCapability("browser_version", "40.0");
24-
caps.setCapability("os", "Windows");
25-
caps.setCapability("os_version", "8.1");
26-
caps.setCapability("browserstack.local", true);
18+
@Test
19+
public void testIsRunning() throws Exception {
20+
assertFalse(l.isRunning());
21+
l.start(options);
22+
assertTrue(l.isRunning());
23+
}
2724

28-
l = new Local();
29-
HashMap<String, String> options = new HashMap<String, String>();
30-
options.put("key", access_key);
31-
//options.put("only", "localhost,80,0");
32-
//options.put("forcelocal", "");
33-
//options.put("proxyHost", "127.0.0.1");
34-
//options.put("proxyPort", "8118");
35-
//options.put("xyz", "qwerty");
25+
@Test
26+
public void testMultipleBinary() throws Exception {
27+
l.start(options);
28+
assertTrue(l.isRunning());
29+
Local l2 = new Local();
30+
try {
31+
l2.start(options);
32+
}
33+
catch(LocalException e){
34+
assertFalse(l2.isRunning());
35+
}
36+
}
37+
38+
@Test
39+
public void testEnableVerbose() throws Exception {
40+
options.put("v", "");
41+
options.put("onlyCommand", "true");
3642
l.start(options);
43+
assertTrue(l.command.contains("-vvv"));
44+
}
3745

38-
System.out.println("Starting session");
39-
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.browserstack.com/wd/hub"), caps);
40-
System.out.println("Started session");
46+
@Test
47+
public void testSetFolder() throws Exception {
48+
options.put("f", "/var/html");
49+
options.put("onlyCommand", "true");
50+
l.start(options);
51+
assertTrue(l.command.contains("-f"));
52+
assertTrue(l.command.contains("/var/html"));
4153
}
4254

4355
@Test
44-
public void testSimple() throws Exception {
45-
driver.get("http://localhost");
46-
System.out.println("Process is running : " + l.isRunning());
47-
System.out.println("Page title is: " + driver.getTitle());
48-
}
56+
public void testEnableForce() throws Exception {
57+
options.put("force", "");
58+
options.put("onlyCommand", "true");
59+
l.start(options);
60+
assertTrue(l.command.contains("-force"));
61+
}
62+
63+
@Test
64+
public void testEnableOnly() throws Exception {
65+
options.put("only", "");
66+
options.put("onlyCommand", "true");
67+
l.start(options);
68+
assertTrue(l.command.contains("-only"));
69+
}
70+
71+
@Test
72+
public void testEnableOnlyAutomate() throws Exception {
73+
options.put("onlyAutomate", "");
74+
options.put("onlyCommand", "true");
75+
l.start(options);
76+
assertTrue(l.command.contains("-onlyAutomate"));
77+
}
78+
79+
@Test
80+
public void testEnableForceLocal() throws Exception {
81+
options.put("forcelocal", "");
82+
options.put("onlyCommand", "true");
83+
l.start(options);
84+
assertTrue(l.command.contains("-forcelocal"));
85+
}
86+
87+
@Test
88+
public void testSetLocalIdentifier() throws Exception {
89+
options.put("localIdentifier", "abcdef");
90+
options.put("onlyCommand", "true");
91+
l.start(options);
92+
assertTrue(l.command.contains("-localIdentifier"));
93+
assertTrue(l.command.contains("abcdef"));
94+
}
95+
96+
@Test
97+
public void testSetProxy() throws Exception {
98+
options.put("proxyHost", "localhost");
99+
options.put("proxyPort", "8080");
100+
options.put("proxyUser", "user");
101+
options.put("proxyPass", "pass");
102+
options.put("onlyCommand", "true");
103+
l.start(options);
104+
assertTrue(l.command.contains("-proxyHost"));
105+
assertTrue(l.command.contains("localhost"));
106+
assertTrue(l.command.contains("-proxyPort"));
107+
assertTrue(l.command.contains("8080"));
108+
assertTrue(l.command.contains("-proxyUser"));
109+
assertTrue(l.command.contains("user"));
110+
assertTrue(l.command.contains("-proxyPass"));
111+
assertTrue(l.command.contains("pass"));
112+
}
113+
114+
@Test
115+
public void testSetHosts() throws Exception {
116+
options.put("hosts", "localhost,8000,0");
117+
options.put("onlyCommand", "true");
118+
l.start(options);
119+
assertTrue(l.command.contains("localhost,8000,0"));
120+
}
49121

50122
@After
51123
public void tearDown() throws Exception {
52-
driver.quit();
53-
l.stop();
124+
l.stop();
54125
}
55126
}

0 commit comments

Comments
 (0)