Skip to content
131 changes: 104 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,125 @@

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

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

## Installation

```
gem install browserstack-local
```

## API
## Example

```
require 'browserstack-local'

### Constructor
#creates an instance of Local
bs_local = BrowserStack::Local.new

* `new BrowserStack::Local`: creates an instance of Local
#replace <browserstack-accesskey> with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
bs_local_args = { "-key" => "<browserstack-accesskey>" }

### Methods
#starts the Local instance with the required arguments
bs_local.start(bs_local_args)

* `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
#check if BrowserStack local instance is running
puts bs_local.isRunning

### Options
#stop the Local instance
bs_local.stop

* `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
```

## Arguments

## Build
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 -

To build gem, `rake build`.
#### Verbose Logging
To enable verbose logging -
```
bs_local_args = { "-key" => "<browserstack-accesskey>" , "-v" => "true"}
```

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

#### Force Start
To kill other running Browserstack Local instances -
```
bs_local_args = { "-key" => "<browserstack-accesskey>" , "-force" => "true"}
```

#### Only Automate
To disable local testing for Live and Screenshots, and enable only Automate -
```
bs_local_args = { "-key" => "<browserstack-accesskey>" , "-onlyAutomate" => "true"}
```

#### Force Local
To route all traffic via local(your) machine -
```
bs_local_args = { "-key" => "<browserstack-accesskey>" , "-forcelocal" => "true"}
```

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

* 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

```
bs_local_args = { "-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 = { "-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 = { "-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 = { "-key" => "<browserstack-accesskey>" , "-v" => "true", "-logfile" => "/browserstack/logs.txt"}
```

## Contribute

### Build Instructions

## Tests
To build gem, `rake build`.

To run the test suite run, `rake test`.

### 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.
32 changes: 16 additions & 16 deletions lib/browserstack/local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,38 @@ def initialize(key = ENV["BROWSERSTACK_ACCESS_KEY"])
end

def add_args(key, value=nil)
if key == "key"
if key == "-key"
@key = value
elsif key == "v" && value.to_s != "false"
elsif key == "-v" && value.to_s != "false"
@verbose_flag = "-vvv"
elsif key == "force" && value.to_s != "false"
elsif key == "-force" && value.to_s != "false"
@force_flag = "-force"
elsif key == "only" && value.to_s != "false"
elsif key == "-only" && value.to_s != "false"
@only_flag = "-only"
elsif key == "onlyAutomate" && value.to_s != "false"
elsif key == "-onlyAutomate" && value.to_s != "false"
@only_automate_flag = "-onlyAutomate"
elsif key == "forcelocal" && value.to_s != "false"
elsif key == "-forcelocal" && value.to_s != "false"
@force_local_flag = "-forcelocal"
elsif key == "localIdentifier"
elsif key == "-localIdentifier"
@local_identifier_flag = "-localIdentifier '#{value}'"
elsif key == "f"
elsif key == "-f"
@folder_flag = "-f"
@folder_path = "'#{value}'"
elsif key == "proxyHost"
elsif key == "-proxyHost"
@proxy_host = "-proxyHost '#{value}'"
elsif key == "proxyPort"
elsif key == "-proxyPort"
@proxy_port = "-proxyPort #{value}"
elsif key == "proxyUser"
elsif key == "-proxyUser"
@proxy_user = "-proxyUser '#{value}'"
elsif key == "proxyPass"
elsif key == "-proxyPass"
@proxy_pass = "-proxyPass '#{value}'"
elsif key == "hosts"
elsif key == "-hosts"
@hosts = value
elsif key == "logfile"
elsif key == "-logfile"
@logfile = value
elsif key == "binarypath"
elsif key == "-binarypath"
@binary_path = value
elsif key == "forceproxy" && value.to_s != "false"
elsif key == "-forceproxy" && value.to_s != "false"
@force_proxy_flag = "-forceproxy"
end
end
Expand Down
30 changes: 14 additions & 16 deletions test/browserstack-local-test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,62 @@ def test_is_running
def test_multiple_binary
@bs_local.start
bs_local_2 = BrowserStack::Local.new
second_log_file = File.join(Dir.pwd, 'local2.log')
assert_raises BrowserStack::LocalException do
bs_local_2.start({'logfile' => second_log_file})
bs_local_2.start
end
File.delete(second_log_file)
end

def test_enable_verbose
@bs_local.add_args('v')
@bs_local.add_args('-v')
assert_match /\-v/, @bs_local.command
end

def test_set_folder
@bs_local.add_args 'f', "/"
@bs_local.add_args '-f', "/"
assert_match /\-f/, @bs_local.command
assert_match /\'\/\'/, @bs_local.command
end

def test_enable_force
@bs_local.add_args "force"
@bs_local.add_args "-force"
assert_match /\-force/, @bs_local.command
end

def test_enable_only
@bs_local.add_args "only"
@bs_local.add_args "-only"
assert_match /\-only/, @bs_local.command
end

def test_enable_only_automate
@bs_local.add_args "onlyAutomate"
@bs_local.add_args "-onlyAutomate"
assert_match /\-onlyAutomate/, @bs_local.command
end

def test_enable_force_local
@bs_local.add_args "forcelocal"
@bs_local.add_args "-forcelocal"
assert_match /\-forcelocal/, @bs_local.command
end

def test_set_local_identifier
@bs_local.add_args "localIdentifier", "randomString"
@bs_local.add_args "-localIdentifier", "randomString"
assert_match /\-localIdentifier \'randomString\'/, @bs_local.command
end

def test_set_proxy
@bs_local.add_args "proxyHost", "localhost"
@bs_local.add_args "proxyPort", 8080
@bs_local.add_args "proxyUser", "user"
@bs_local.add_args "proxyPass", "pass"
@bs_local.add_args "-proxyHost", "localhost"
@bs_local.add_args "-proxyPort", 8080
@bs_local.add_args "-proxyUser", "user"
@bs_local.add_args "-proxyPass", "pass"
assert_match /\-proxyHost \'localhost\' \-proxyPort 8080 \-proxyUser \'user\' \-proxyPass \'pass\'/, @bs_local.command
end

def test_force_proxy
@bs_local.add_args "forceproxy"
@bs_local.add_args "-forceproxy"
assert_match /\-forceproxy/, @bs_local.command
end

def test_hosts
@bs_local.add_args "hosts", "localhost,8080,0"
@bs_local.add_args "-hosts", "localhost,8080,0"
assert_match /localhost\,8080\,0/, @bs_local.command
end

Expand Down