Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
bin/
browserstack-api/target
*.class
target/**
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.DS_Store
*.class
*.swp
.project
51 changes: 51 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.BS</groupId>
<artifactId>BrowserStack-local-java</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>BrowserStack-local-java</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
</plugin>
</plugins>
</build>
</project>
13 changes: 13 additions & 0 deletions src/main/java/com/BS/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.BS;

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/BS/BrowserStackLocalException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.BS;

class BrowserStackLocalException extends Exception

{
BrowserStackLocalException(String message)
{
super(message);
}
}
122 changes: 122 additions & 0 deletions src/main/java/com/BS/Local.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.BS;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

class Local{

Process BrowserStackLocal = null;
List<String> command;
HashMap<String, String> parameters;

void start(HashMap<String,String> options) throws Exception
{
LocalBinary lb = new LocalBinary();
command = new ArrayList<String>();
command.add(lb.binary_path);
command.add(options.get("key"));

makeCommand(options);

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

System.out.println("Setting up Local Testing connection...");
BrowserStackLocal = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(BrowserStackLocal.getInputStream()));
String string;
int j = 0;
while ((string = reader.readLine()) != null)
{
if (string.equalsIgnoreCase("Press Ctrl-C to exit"))
{
System.out.println("Local Testing connection has been established.");
break;
}

if (string.contains("*** Error"))
{
throw new BrowserStackLocalException(string);
}
if (j++ > 20)
{
throw new BrowserStackLocalException("Could not start BrowserStackLocal");
}
}

}

}

void stop()
{
if (BrowserStackLocal != null)
{
BrowserStackLocal.destroy();
System.out.println("Disconnected successfully");
}
}

boolean isRunning()
{
try
{
BrowserStackLocal.exitValue();
return false;
}
catch (Exception e)
{
return true;
}
}

void init()
{
parameters = new HashMap<String, String>();
parameters.put("v","-v");
parameters.put("f","-f");
parameters.put("h","-h");
parameters.put("version", "-version");
parameters.put("force", "-force");
parameters.put("only", "-only");
parameters.put("forcelocal", "-forcelocal");
parameters.put("onlyAutomate", "-onlyAutomate");
parameters.put("proxyHost", "-proxyHost");
parameters.put("proxyPort", "-proxyPort");
parameters.put("proxyUser", "-proxyUser");
parameters.put("proxyPass", "-proxyPass");
parameters.put("hosts", "-hosts");
parameters.put("logfile", "-logfile");
}

Local() throws Exception
{
init();
}

void makeCommand(HashMap<String,String> options)
{
Set set = options.entrySet();
Iterator i = set.iterator();
while(i.hasNext())
{
Map.Entry me = (Map.Entry)i.next();
String parameter = me.getKey().toString().trim();
if(parameters.get(parameter)!=null)
if(me.getValue()!=null)
{
command.add(parameters.get(parameter));
command.add((String) me.getValue());
}
else
command.add(parameters.get(parameter));
}
}
}
136 changes: 136 additions & 0 deletions src/main/java/com/BS/LocalBinary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.BS;

import java.io.File;
import java.net.URL;
import org.apache.commons.io.FileUtils;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;

class LocalBinary {

String http_path;
String dest_parent_dir;
String binary_path;
String osname;
String arch;
String orderedPaths[] = {System.getProperty("user.home")+"/browserstack", System.getProperty("user.dir"),System.getProperty("java.io.tmpdir")};

LocalBinary() throws Exception
{
initialize();
getBinary();
}

void initialize()
{
osname = System.getProperty("os.name");
arch = System.getProperty("os.arch");

if(osname.contains("Mac") || osname.contains("Darwin"))
http_path="https://www.browserstack.com/browserstack-local/BrowserStackLocal-darwin-x64.zip";

else if(osname.contains("Windows"))
http_path="https://www.browserstack.com/browserstack-local/BrowserStackLocal-win32.zip";

else if (osname.contains("Linux") && arch.contains("64"))
http_path="https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-x64.zip";

else
http_path="https://www.browserstack.com/browserstack-local/BrowserStackLocal-linux-ia32.zip";
}

Boolean getBinary() throws Exception
{
dest_parent_dir = getAvailableDirectory();

if (osname.contains("Windows"))
binary_path = dest_parent_dir + "/BrowserStackLocal.exe";
else
binary_path = dest_parent_dir + "/BrowserStackLocal";

if (new File(binary_path).exists())
return true;
else
return downloadBinary(dest_parent_dir);

}

String getAvailableDirectory() throws Exception
{
int i=0;
while(i<orderedPaths.length)
{
String path = orderedPaths[i];
if (makePath(path))
return path;
else
i++;
}
throw new BrowserStackLocalException("Error trying to download BrowserStackLocal binary");
}

boolean makePath(String path)
{
try
{
if (!new File(path).exists())
new File(path).mkdirs();
return true;

}
catch (Exception e)
{
return false;
}
}

Boolean downloadBinary(String dest_parent_dir) throws BrowserStackLocalException
{
try
{
if (!new File(dest_parent_dir).exists())
new File(dest_parent_dir).mkdirs();

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

System.out.println("Unzipping...");
unzipFile(source, dest_parent_dir);
System.out.println("Changing permissions...");
changePermissions(binary_path);
System.out.println("Download complete.");

return true;
}
catch (Exception e)
{
System.out.println(e);
throw new BrowserStackLocalException("Error trying to download BrowserStackLocal binary");
}
}

void unzipFile(String source, String dest)
{
try
{
ZipFile zipFile = new ZipFile(source);
zipFile.extractAll(dest);
}
catch (ZipException e)
{
e.printStackTrace();
}
}

void changePermissions (String path)
{
File f = new File(path);
f.setExecutable(true, true);
f.setReadable(true, true);
f.setWritable(true, true);
}
}
Loading