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
131 changes: 104 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

[![Build Status](https://travis-ci.org/browserstack/browserstack-local-php.svg?branch=master)](https://travis-ci.org/browserstack/browserstack-local-php)

## Setup
A simple PHP wrapper for BrowserStack Local Binary.

## Installation

Installation is possible using [Composer](https://getcomposer.org/).

Expand All @@ -19,41 +21,116 @@ Install all depenedencies:

Test the installation by running a simple test file, check out example.php in the main repository.

## API
## Example

```
require_once('vendor/autoload.php');
use BrowserStack\Local;

#creates an instance of Local
$bs_local = new Local();

#replace <browserstack-accesskey> with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
$bs_local_args = array("key" => "<browserstack-accesskey>");

#starts the Local instance with the required arguments
$bs_local->start(bs_local_args);

#check if BrowserStack local instance is running
echo $bs_local->isRunning();

#stop the Local instance
$bs_local->stop();

```

## Arguments

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 -

#### Verbose Logging
To enable verbose logging -
```
$bs_local_args = array("key" => "<browserstack-accesskey>", "v" => true);
```

### Constructor
#### Folder Testing
To test local folder rather internal server, provide path to folder as value of this option -
```
$bs_local_args = array("key" => "<browserstack-accesskey>", "f" => "/my/awesome/folder");
```

* `new Local()`: creates an instance of Local
#### Force Start
To kill other running Browserstack Local instances -
```
$bs_local_args = array("key" => "<browserstack-accesskey>", "force" => true);
```

### Methods
#### Only Automate
To disable local testing for Live and Screenshots, and enable only Automate -
```
$bs_local_args = array("key" => "<browserstack-accesskey>", "onlyAutomate" => true);
```

* `start(options)`: starts Local instance with options. The options available are detailed below.
* `stop()`: stops the Local instance
* `isRunning()`: checks if Local instance is running
#### Force Local
To route all traffic via local(your) machine -
```
$bs_local_args = array("key" => "<browserstack-accesskey>", "forcelocal" => true);
```

### Options
#### Proxy
To use a proxy for local testing -

* `key`: BrowserStack Access Key
* `v`: Provides verbose logging
* `f`: If you want to test local folder rather internal server, provide path to folder as value of this option
* `force`: Kill other running Browserstack Local
* `only`: Restricts Local Testing access to specified local servers and/or folders
* `forcelocal`: Route all traffic via local machine
* `onlyAutomate`: Disable Live Testing and Screenshots, just test Automate
* `proxyHost`: Hostname/IP of proxy, remaining proxy options are ignored if this option is absent
* `proxyPort`: Port for the proxy, defaults to 3128 when -proxyHost is used
* `proxyUser`: Username for connecting to proxy (Basic Auth Only)
* `proxyPass`: Password for USERNAME, will be ignored if USERNAME is empty or not specified
* `localIdentifier`: If doing simultaneous multiple local testing connections, set this uniquely for different processes
* `hosts`: List of hosts and ports where Local must be enabled for eg. localhost,3000,1,localhost,3001,0
* `logfile`: Path to file where Local logs be saved to
* `binarypath`: Optional path to Local binary
* proxyHost: Hostname/IP of proxy, remaining proxy options are ignored if this option is absent
* proxyPort: Port for the proxy, defaults to 3128 when -proxyHost is used
* proxyUser: Username for connecting to proxy (Basic Auth Only)
* proxyPass: Password for USERNAME, will be ignored if USERNAME is empty or not specified

## Test
```
$bs_local_args = array("key" => "<browserstack-accesskey>", "proxyHost" => "127.0.0.1", "proxyPort" => "8000", "proxyUser" => "user", "proxyPass" => "password");
```

#### Local Identifier
If doing simultaneous multiple local testing connections, set this uniquely for different processes -
```
$bs_local_args = array("key" => "<browserstack-accesskey>", "localIdentifier" => "randomstring");
```

## Additional Arguments

#### Binary Path

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.
Path to specify local Binary path -
```
$bs_local_args = array("key" => "<browserstack-accesskey>", "binarypath" => "/browserstack/BrowserStackLocal");
```

#### Logfile
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.
To specify the path to file where the logs will be saved -
```
$bs_local_args = array("key" => "<browserstack-accesskey>", "logfile" => "/browserstack/logs.txt");
```

## Contribute

Testing is possible using [PHPUnit](https://phpunit.de/).

To run the tests, run the command:
`phpunit`
To run the tests, run the command: `phpunit`

### Reporting bugs

You can submit bug reports either in the Github issue tracker.

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.

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.

### Pull Requests

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.

* Adhere to the coding conventions you see in the surrounding code.
* Include tests, and make sure all tests pass.
* 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.
42 changes: 25 additions & 17 deletions lib/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Local {
public function __construct() {
$this->key = getenv("BROWSERSTACK_ACCESS_KEY");
$this->logfile = getcwd() . "/local.log";
$this->user_args = array();
}

public function __destruct() {
Expand All @@ -32,40 +33,46 @@ public function isRunning() {
}

public function add_args($arg_key, $value = NULL) {
if ($arg_key == "-key")
if ($arg_key == "key")
$this->key = $value;
elseif ($arg_key == "-binaryPath")
elseif ($arg_key == "binaryPath")
$this->binary_path = $value;
elseif ($arg_key == "-logfile")
elseif ($arg_key == "logfile")
$this->logfile = $value;
elseif ($arg_key == "-v")
elseif ($arg_key == "v")
$this->verbose_flag = "-vvv";
elseif ($arg_key == "-force")
elseif ($arg_key == "force")
$this->force_flag = "-force";
elseif ($arg_key == "-only")
elseif ($arg_key == "only")
$this->only_flag = "-only";
elseif ($arg_key == "-onlyAutomate")
elseif ($arg_key == "onlyAutomate")
$this->only_automate_flag = "-onlyAutomate";
elseif ($arg_key == "-forcelocal")
elseif ($arg_key == "forcelocal")
$this->force_local_flag = "-forcelocal";
elseif ($arg_key == "-localIdentifier")
elseif ($arg_key == "localIdentifier")
$this->local_identifier_flag = "-localIdentifier $value";
elseif ($arg_key == "-proxyHost")
elseif ($arg_key == "proxyHost")
$this->proxy_host = "-proxyHost $value";
elseif ($arg_key == "-proxyPort")
elseif ($arg_key == "proxyPort")
$this->proxy_port = "-proxyPort $value";
elseif ($arg_key == "-proxyUser")
elseif ($arg_key == "proxyUser")
$this->proxy_user = "-proxyUser $value";
elseif ($arg_key == "-proxyPass")
elseif ($arg_key == "proxyPass")
$this->proxy_pass = "-proxyPass $value";
elseif ($arg_key == "-forceproxy")
elseif ($arg_key == "forceproxy")
$this->force_proxy_flag = "-forceproxy";
elseif ($arg_key == "-hosts")
elseif ($arg_key == "hosts")
$this->hosts = $value;
elseif ($arg_key == "-f") {
elseif ($arg_key == "f") {
$this->folder_flag = "-f";
$this->folder_path = $value;
}
elseif (strtolower($value) == "true"){
array_push($this->user_args, "-$arg_key");
}
else {
array_push($this->user_args, "-$arg_key '$value'");
}
}

public function start($arguments) {
Expand Down Expand Up @@ -138,7 +145,8 @@ public function command() {
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
$exec = "call";

$command = "$exec $this->binary_path -logFile $this->logfile $this->folder_flag $this->key $this->folder_path $this->force_local_flag $this->local_identifier_flag $this->only_flag $this->only_automate_flag $this->proxy_host $this->proxy_port $this->proxy_user $this->proxy_pass $this->force_proxy_flag $this->force_flag $this->verbose_flag $this->hosts";
$user_args = join(' ', $this->user_args);
$command = "$exec $this->binary_path -logFile $this->logfile $this->folder_flag $this->key $this->folder_path $this->force_local_flag $this->local_identifier_flag $this->only_flag $this->only_automate_flag $this->proxy_host $this->proxy_port $this->proxy_user $this->proxy_pass $this->force_proxy_flag $this->force_flag $this->verbose_flag $this->hosts $user_args";
$command = preg_replace('/\s+/S', " ", $command);
return $command;
}
Expand Down
47 changes: 30 additions & 17 deletions tests/LocalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,59 @@ public function tearDown(){
}

public function test_verbose() {
$this->bs_local->add_args('-v');
$this->bs_local->add_args('v');
$this->assertContains('-v',$this->bs_local->command());
}

public function test_set_folder() {
$this->bs_local->add_args('-f', "/");
$this->bs_local->add_args('f', "/");
$this->assertContains('-f',$this->bs_local->command());
$this->assertContains('/',$this->bs_local->command());
}

public function test_enable_force() {
$this->bs_local->add_args("-force");
$this->bs_local->add_args("force");
}

public function test_set_local_identifier() {
$this->bs_local->add_args("-localIdentifier", "randomString");
$this->bs_local->add_args("localIdentifier", "randomString");
$this->assertContains('-localIdentifier randomString',$this->bs_local->command());
}

public function test_enable_only() {
$this->bs_local->add_args("-only");
$this->bs_local->add_args("only");
$this->assertContains('-only',$this->bs_local->command());
}

public function test_enable_only_automate() {
$this->bs_local->add_args("-onlyAutomate");
$this->bs_local->add_args("onlyAutomate");
$this->assertContains('-onlyAutomate', $this->bs_local->command());
}

public function test_enable_force_local() {
$this->bs_local->add_args("-forcelocal");
$this->bs_local->add_args("forcelocal");
$this->assertContains('-forcelocal',$this->bs_local->command());
}

public function test_custom_boolean_argument() {
$this->bs_local->add_args("boolArg1", true);
$this->bs_local->add_args("boolArg2", true);
$this->assertContains('-boolArg1',$this->bs_local->command());
$this->assertContains('-boolArg2',$this->bs_local->command());
}

public function test_custom_keyval() {
$this->bs_local->add_args("customKey1", "custom value1");
$this->bs_local->add_args("customKey2", "custom value2");
$this->assertContains('-customKey1 \'custom value1\'',$this->bs_local->command());
$this->assertContains('-customKey2 \'custom value2\'',$this->bs_local->command());
}

public function test_set_proxy() {
$this->bs_local->add_args("-proxyHost", "localhost");
$this->bs_local->add_args("-proxyPort", 8080);
$this->bs_local->add_args("-proxyUser", "user");
$this->bs_local->add_args("-proxyPass", "pass");
$this->bs_local->add_args("proxyHost", "localhost");
$this->bs_local->add_args("proxyPort", 8080);
$this->bs_local->add_args("proxyUser", "user");
$this->bs_local->add_args("proxyPass", "pass");
$this->assertContains('-proxyHost localhost -proxyPort 8080 -proxyUser user -proxyPass pass',$this->bs_local->command());
}

Expand All @@ -68,35 +82,34 @@ public function test_enable_force_proxy() {
$this->assertContains('-forceproxy',$this->bs_local->command());
}


public function test_hosts() {
$this->bs_local->add_args("-hosts", "localhost,8080,0");
$this->assertContains('localhost,8080,0',$this->bs_local->command());
}

public function test_isRunning() {
$this->assertFalse($this->bs_local->isRunning());
$this->bs_local->start(array('-v' => true));
$this->bs_local->start(array('v' => true));
$this->assertTrue($this->bs_local->isRunning());
$this->bs_local->stop();
$this->assertFalse($this->bs_local->isRunning());
$this->bs_local->start(array('-v' => true));
$this->bs_local->start(array('v' => true));
$this->assertTrue($this->bs_local->isRunning());
}

public function test_checkPid() {
$this->assertFalse($this->bs_local->isRunning());
$this->bs_local->start(array('-v' => true));
$this->bs_local->start(array('v' => true));
$this->assertTrue($this->bs_local->pid > 0);
}

public function test_multiple_binary() {
$this->bs_local->start(array('-v' => true));
$this->bs_local->start(array('v' => true));
$bs_local_2 = new Local();
$log_file2 = getcwd(). '/log2.log';
print($log_file2);
try {
$bs_local_2->start(array('-v' => true, '-logfile' => $log_file2));
$bs_local_2->start(array('v' => true, 'logfile' => $log_file2));
$this->fail("Expected Exception has not been raised.");
} catch (LocalException $ex) {
$emessage = $ex->getMessage();
Expand Down