Skip to content

Commit 8a28381

Browse files
committed
First Commit
0 parents  commit 8a28381

File tree

7 files changed

+393
-0
lines changed

7 files changed

+393
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
target/**
2+
pom.xml.tag
3+
pom.xml.releaseBackup
4+
pom.xml.versionsBackup
5+
pom.xml.next
6+
release.properties
7+
dependency-reduced-pom.xml
8+
buildNumber.properties
9+
.mvn/timing.properties
10+
.DS_Store

pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.BS</groupId>
5+
<artifactId>BrowserStack-local-java</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>BrowserStack-local-java</name>
9+
<url>http://maven.apache.org</url>
10+
<dependencies>
11+
<dependency>
12+
<groupId>junit</groupId>
13+
<artifactId>junit</artifactId>
14+
<version>4.11</version>
15+
<scope>test</scope>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.seleniumhq.selenium</groupId>
19+
<artifactId>selenium-java</artifactId>
20+
<version>2.45.0</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.seleniumhq.selenium</groupId>
24+
<artifactId>selenium-server</artifactId>
25+
<version>2.45.0</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>net.lingala.zip4j</groupId>
29+
<artifactId>zip4j</artifactId>
30+
<version>1.3.2</version>
31+
</dependency>
32+
</dependencies>
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-compiler-plugin</artifactId>
38+
<version>2.3.2</version>
39+
<configuration>
40+
<source>1.6</source>
41+
<target>1.6</target>
42+
</configuration>
43+
</plugin>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-surefire-plugin</artifactId>
47+
<version>2.4.2</version>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>

src/main/java/com/BS/App.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.BS;
2+
3+
/**
4+
* Hello world!
5+
*
6+
*/
7+
public class App
8+
{
9+
public static void main( String[] args )
10+
{
11+
System.out.println( "Hello World!" );
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.BS;
2+
3+
class BrowserStackLocalException extends Exception
4+
5+
{
6+
BrowserStackLocalException(String message)
7+
{
8+
super(message);
9+
}
10+
}

src/main/java/com/BS/Local.java

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.BS;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
class Local{
13+
14+
Process BrowserStackLocal = null;
15+
List<String> command;
16+
HashMap<String, String> parameters;
17+
18+
void start(HashMap<String,String> options) throws Exception
19+
{
20+
LocalBinary lb = new LocalBinary();
21+
command = new ArrayList<String>();
22+
command.add(lb.binary_path);
23+
command.add(options.get("key"));
24+
25+
makeCommand(options);
26+
27+
if (BrowserStackLocal == null)
28+
{
29+
ProcessBuilder processBuilder = new ProcessBuilder(command);
30+
31+
System.out.println("Setting up Local Testing connection...");
32+
BrowserStackLocal = processBuilder.start();
33+
BufferedReader reader = new BufferedReader(new InputStreamReader(BrowserStackLocal.getInputStream()));
34+
String string;
35+
int j = 0;
36+
while ((string = reader.readLine()) != null)
37+
{
38+
if (string.equalsIgnoreCase("Press Ctrl-C to exit"))
39+
{
40+
System.out.println("Local Testing connection has been established.");
41+
break;
42+
}
43+
44+
if (string.contains("*** Error"))
45+
{
46+
throw new BrowserStackLocalException(string);
47+
}
48+
if (j++ > 20)
49+
{
50+
throw new BrowserStackLocalException("Could not start BrowserStackLocal");
51+
}
52+
}
53+
54+
}
55+
56+
}
57+
58+
void stop()
59+
{
60+
if (BrowserStackLocal != null)
61+
{
62+
BrowserStackLocal.destroy();
63+
System.out.println("Disconnected successfully");
64+
}
65+
}
66+
67+
boolean isRunning()
68+
{
69+
try
70+
{
71+
BrowserStackLocal.exitValue();
72+
return false;
73+
}
74+
catch (Exception e)
75+
{
76+
return true;
77+
}
78+
}
79+
80+
void init()
81+
{
82+
parameters = new HashMap<String, String>();
83+
parameters.put("v","-v");
84+
parameters.put("f","-f");
85+
parameters.put("h","-h");
86+
parameters.put("version", "-version");
87+
parameters.put("force", "-force");
88+
parameters.put("only", "-only");
89+
parameters.put("forcelocal", "-forcelocal");
90+
parameters.put("onlyAutomate", "-onlyAutomate");
91+
parameters.put("proxyHost", "-proxyHost");
92+
parameters.put("proxyPort", "-proxyPort");
93+
parameters.put("proxyUser", "-proxyUser");
94+
parameters.put("proxyPass", "-proxyPass");
95+
parameters.put("hosts", "-hosts");
96+
parameters.put("logfile", "-logfile");
97+
}
98+
99+
Local() throws Exception
100+
{
101+
init();
102+
}
103+
104+
void makeCommand(HashMap<String,String> options)
105+
{
106+
Set set = options.entrySet();
107+
Iterator i = set.iterator();
108+
while(i.hasNext())
109+
{
110+
Map.Entry me = (Map.Entry)i.next();
111+
String parameter = me.getKey().toString().trim();
112+
if(parameters.get(parameter)!=null)
113+
if(me.getValue()!=null)
114+
{
115+
command.add(parameters.get(parameter));
116+
command.add((String) me.getValue());
117+
}
118+
else
119+
command.add(parameters.get(parameter));
120+
}
121+
}
122+
}

src/main/java/com/BS/LocalBinary.java

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.BS;
2+
3+
import java.io.File;
4+
import java.net.URL;
5+
import org.apache.commons.io.FileUtils;
6+
7+
import net.lingala.zip4j.core.ZipFile;
8+
import net.lingala.zip4j.exception.ZipException;
9+
10+
class LocalBinary {
11+
12+
String http_path;
13+
String dest_parent_dir;
14+
String binary_path;
15+
String osname;
16+
String arch;
17+
String orderedPaths[] = {System.getProperty("user.home")+"/browserstack", System.getProperty("user.dir"),System.getProperty("java.io.tmpdir")};
18+
19+
LocalBinary() throws Exception
20+
{
21+
initialize();
22+
getBinary();
23+
}
24+
25+
void initialize()
26+
{
27+
osname = System.getProperty("os.name");
28+
arch = System.getProperty("os.arch");
29+
30+
if(osname.contains("Mac") || osname.contains("Darwin"))
31+
http_path="https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip";
32+
33+
else if(osname.contains("Windows"))
34+
http_path="https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip";
35+
36+
else if (osname.contains("Linux") && arch.contains("64"))
37+
http_path="https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-x64.zip";
38+
39+
else
40+
http_path="https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-ia32.zip";
41+
}
42+
43+
Boolean getBinary() throws Exception
44+
{
45+
dest_parent_dir = getAvailableDirectory();
46+
47+
if (osname.contains("Windows"))
48+
binary_path = dest_parent_dir + "/BrowserStackLocal.exe";
49+
else
50+
binary_path = dest_parent_dir + "/BrowserStackLocal";
51+
52+
if (new File(binary_path).exists())
53+
return true;
54+
else
55+
return downloadBinary(dest_parent_dir);
56+
57+
}
58+
59+
String getAvailableDirectory() throws Exception
60+
{
61+
int i=0;
62+
while(i<orderedPaths.length)
63+
{
64+
String path = orderedPaths[i];
65+
if (makePath(path))
66+
return path;
67+
else
68+
i++;
69+
}
70+
throw new BrowserStackLocalException("Error trying to download BrowserStackLocal binary");
71+
}
72+
73+
boolean makePath(String path)
74+
{
75+
try
76+
{
77+
if (!new File(path).exists())
78+
new File(path).mkdirs();
79+
return true;
80+
81+
}
82+
catch (Exception e)
83+
{
84+
return false;
85+
}
86+
}
87+
88+
Boolean downloadBinary(String dest_parent_dir) throws BrowserStackLocalException
89+
{
90+
try
91+
{
92+
if (!new File(dest_parent_dir).exists())
93+
new File(dest_parent_dir).mkdirs();
94+
95+
System.out.println("Downloading Local Testing binaries...");
96+
URL url = new URL(http_path);
97+
String source = dest_parent_dir + "/Download.zip";
98+
File f = new File(source);
99+
FileUtils.copyURLToFile(url, f);
100+
101+
System.out.println("Unzipping...");
102+
unzipFile(source, dest_parent_dir);
103+
System.out.println("Changing permissions...");
104+
changePermissions(binary_path);
105+
System.out.println("Download complete.");
106+
107+
return true;
108+
}
109+
catch (Exception e)
110+
{
111+
System.out.println(e);
112+
throw new BrowserStackLocalException("Error trying to download BrowserStackLocal binary");
113+
}
114+
}
115+
116+
void unzipFile(String source, String dest)
117+
{
118+
try
119+
{
120+
ZipFile zipFile = new ZipFile(source);
121+
zipFile.extractAll(dest);
122+
}
123+
catch (ZipException e)
124+
{
125+
e.printStackTrace();
126+
}
127+
}
128+
129+
void changePermissions (String path)
130+
{
131+
File f = new File(path);
132+
f.setExecutable(true, true);
133+
f.setReadable(true, true);
134+
f.setWritable(true, true);
135+
}
136+
}

0 commit comments

Comments
 (0)