Skip to content

Commit ae69655

Browse files
committed
added log file
1 parent 7e8de49 commit ae69655

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.BrowserStack;
22

3-
class BrowserStackLocalException extends Exception
4-
5-
{
6-
BrowserStackLocalException(String message)
7-
{
3+
class BrowserStackLocalException extends Exception {
4+
BrowserStackLocalException(String message){
85
super(message);
96
}
107
}

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,62 @@
88
import java.util.List;
99
import java.util.Map;
1010
import java.util.Set;
11+
import java.io.File;
12+
import java.io.FileReader;
13+
import java.io.FileWriter;
1114

1215
class Local {
1316

1417
Process BrowserStackLocal = null;
1518
List<String> command;
19+
String logFilePath;
1620
HashMap<String, String> parameters;
1721

1822
void start(HashMap<String,String> options) throws Exception {
19-
LocalBinary lb = new LocalBinary();
2023
command = new ArrayList<String>();
21-
command.add(lb.binary_path);
22-
command.add(options.get("key"));
24+
25+
if(options.get("binarypath") != null){
26+
command.add(options.get("binarypath"));
27+
}
28+
else {
29+
LocalBinary lb = new LocalBinary();
30+
command.add(lb.binary_path);
31+
}
32+
33+
logFilePath = options.get("logfile") == null ? (System.getProperty("user.dir") + "/local.log") : options.get("logfile");
34+
command.add("-logFile");
35+
command.add(logFilePath);
2336

37+
command.add(options.get("key"));
2438
makeCommand(options);
2539

2640
if (BrowserStackLocal == null){
2741
ProcessBuilder processBuilder = new ProcessBuilder(command);
2842

29-
System.out.println("Setting up Local Testing connection...");
43+
if((new File(logFilePath)).exists()){
44+
FileWriter f = new FileWriter(logFilePath);
45+
f.write("");
46+
f.close();
47+
}
48+
3049
BrowserStackLocal = processBuilder.start();
31-
BufferedReader reader = new BufferedReader(new InputStreamReader(BrowserStackLocal.getInputStream()));
50+
FileReader f = new FileReader(logFilePath);
51+
BufferedReader reader = new BufferedReader(f);
3252
String string;
3353
int j = 0;
34-
while ((string = reader.readLine()) != null) {
54+
while (true) {
55+
string = reader.readLine();
56+
if(string == null) continue;
57+
3558
if (string.equalsIgnoreCase("Press Ctrl-C to exit")) {
36-
System.out.println("Local Testing connection has been established.");
59+
f.close();
3760
break;
3861
}
3962

4063
if (string.contains("*** Error")){
64+
f.close();
4165
throw new BrowserStackLocalException(string);
4266
}
43-
if (j++ > 20) {
44-
throw new BrowserStackLocalException("Could not start BrowserStackLocal");
45-
}
4667
}
4768

4869
}
@@ -51,7 +72,6 @@ void start(HashMap<String,String> options) throws Exception {
5172
void stop(){
5273
if (BrowserStackLocal != null) {
5374
BrowserStackLocal.destroy();
54-
System.out.println("Disconnected successfully");
5575
}
5676
}
5777

@@ -80,7 +100,6 @@ void init(){
80100
parameters.put("proxyUser", "-proxyUser");
81101
parameters.put("proxyPass", "-proxyPass");
82102
parameters.put("hosts", "-hosts");
83-
parameters.put("logfile", "-logfile");
84103
}
85104

86105
Local() throws Exception {

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ else if (osname.contains("Linux") && arch.contains("64"))
4141
else
4242
http_path="https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-ia32.zip";
4343
}
44-
44+
4545
Boolean getBinary() throws Exception {
4646
dest_parent_dir = getAvailableDirectory();
4747

@@ -56,7 +56,7 @@ Boolean getBinary() throws Exception {
5656
return downloadBinary(dest_parent_dir);
5757

5858
}
59-
59+
6060
String getAvailableDirectory() throws Exception {
6161
int i=0;
6262
while(i<orderedPaths.length){
@@ -74,7 +74,6 @@ boolean makePath(String path) {
7474
if (!new File(path).exists())
7575
new File(path).mkdirs();
7676
return true;
77-
7877
}
7978
catch (Exception e){
8079
return false;
@@ -86,33 +85,28 @@ Boolean downloadBinary(String dest_parent_dir) throws BrowserStackLocalException
8685
if (!new File(dest_parent_dir).exists())
8786
new File(dest_parent_dir).mkdirs();
8887

89-
System.out.println("Downloading Local Testing binaries...");
9088
URL url = new URL(http_path);
9189
String source = dest_parent_dir + "/Download.zip";
9290
File f = new File(source);
9391
FileUtils.copyURLToFile(url, f);
9492

95-
System.out.println("Unzipping...");
9693
unzipFile(source, dest_parent_dir);
97-
System.out.println("Changing permissions...");
9894
changePermissions(binary_path);
99-
System.out.println("Download complete.");
10095

10196
return true;
10297
}
10398
catch (Exception e){
104-
System.out.println(e);
10599
throw new BrowserStackLocalException("Error trying to download BrowserStackLocal binary");
106100
}
107101
}
108102

109103
void unzipFile(String source, String dest) {
110104
try {
111-
ZipFile zipFile = new ZipFile(source);
112-
zipFile.extractAll(dest);
105+
ZipFile zipFile = new ZipFile(source);
106+
zipFile.extractAll(dest);
113107
}
114108
catch (ZipException e) {
115-
e.printStackTrace();
109+
e.printStackTrace();
116110
}
117111
}
118112

0 commit comments

Comments
 (0)