Skip to content

Commit

Permalink
Merge pull request #8 from browserstack/custom_args
Browse files Browse the repository at this point in the history
Custom args
  • Loading branch information
tr4n2uil committed Apr 28, 2016
2 parents 0eaf577 + e9459e2 commit 0afdd14
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 64 deletions.
138 changes: 108 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,127 @@

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

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

## Installation

```
npm install browserstack
npm install browserstack-local
```

## API
## Example

### Constructor
```
var browserstack = require('browserstack-local');
* `new browserstack.Local()`: creates an instance of Local
# creates an instance of Local
var bs_local = new browserstack.Local();
### Methods
# replace <browserstack-accesskey> with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
var bs_local_args = { 'key': '<browserstack-accesskey>' };
* `start(options, callback)`: starts Local instance with options. The options available are detailed below.
* `stop(callback)`: stops the Local instance
* `isRunning()`: checks if Local instance is running and returns a corresponding boolean value
# starts the Local instance with the required arguments
bs_local.start(bs_local_args, function() {
console.log("Started BrowserStackLocal");
});
The first and only argument to any callback function will be an error object.
# check if BrowserStack local instance is running
console.log(bs_local.isRunning());
### Options
# stop the Local instance
bs_local.stop(function() {
console.log("Stopped BrowserStackLocal");
});
* `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

## Tests
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 run the test suite run - `npm test`.
#### Verbose Logging
To enable verbose logging -
```
bs_local_args = { 'key': '<browserstack-accesskey>', 'v': 'true' }
```

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

### Instructions

To run the test suite run, `npm 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.

To run the example run - `node node-example.js`.
* 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: 26 additions & 16 deletions lib/Local.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function Local(){
this.key = process.env.BROWSERSTACK_ACCESS_KEY;
this.logfile = path.join(process.cwd(), 'local.log');
this.exitCallback;
this.userArgs = [];

this.errorRegex = /\*\*\* Error: [^\r\n]*/i;
this.doneRegex = /Press Ctrl-C to exit/i;
Expand Down Expand Up @@ -81,88 +82,94 @@ function Local(){
var value = options[key];

switch(key){
case '-key':
case 'key':
this.key = value;
break;

case '-v':
case 'v':
if(value)
this.verboseFlag = '-vvv';
break;

case '-force':
case 'force':
if(value)
this.forceFlag = '-force';
break;

case '-only':
case 'only':
if(value)
this.onlyFlag = '-only';
break;

case '-onlyAutomate':
case 'onlyAutomate':
if(value)
this.onlyAutomateFlag = '-onlyAutomate';
break;

case '-forcelocal':
case 'forcelocal':
if(value)
this.forceLocalFlag = '-forcelocal';
break;

case '-localIdentifier':
case 'localIdentifier':
if(value)
this.localIdentifierFlag = '-localIdentifier ' + value;
break;

case '-f':
case 'f':
if(value){
this.folderFlag = '-f';
this.folderPath = value;
}
break;

case '-proxyHost':
case 'proxyHost':
if(value)
this.proxyHost = '-proxyHost ' + value;
break;

case '-proxyPort':
case 'proxyPort':
if(value)
this.proxyPort = '-proxyPort ' + value;
break;

case '-proxyUser':
case 'proxyUser':
if(value)
this.proxyUser = '-proxyUser ' + value;
break;

case '-proxyPass':
case 'proxyPass':
if(value)
this.proxyPass = '-proxyPass ' + value;
break;

case '-forceproxy':
case 'forceproxy':
if(value)
this.forceProxyFlag = '-forceproxy';
break;

case '-hosts':
case 'hosts':
if(value)
this.hosts = value;
break;

case '-logfile':
case 'logfile':
if(value)
this.logfile = value;
break;

case '-binarypath':
case 'binarypath':
if(value)
this.binaryPath = value;
break;

default:
if(value.toString().toLowerCase() == 'true'){
this.userArgs.push('-' + key);
} else {
this.userArgs.push('-' + key);
this.userArgs.push(value);
}
break;
}
}
Expand Down Expand Up @@ -208,6 +215,9 @@ function Local(){
args.push(this.verboseFlag);
if(this.hosts)
args.push(this.hosts);
for(var i in this.userArgs){
args.push(this.userArgs[i]);
}
return args;
};
}
Expand Down
Loading

0 comments on commit 0afdd14

Please sign in to comment.