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
Binary file modified BrowserStack.dll
Binary file not shown.
25 changes: 24 additions & 1 deletion BrowserStack/BrowserStack Unit Tests/LocalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void TestWorksWithBooleanOptions()
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryPath(""), Times.Once);
tunnelMock.Verify(mock => mock.addBinaryArguments(It.IsRegex("-vvv.*-force.*-forcelocal*-forceproxy.*-onlyAutomate")), Times.Once());
tunnelMock.Verify(mock => mock.addBinaryArguments(It.IsRegex("-vvv.*-force.*-forcelocal.*-forceproxy.*-onlyAutomate")), Times.Once());
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute), Times.Once());
local.stop();
}
Expand Down Expand Up @@ -160,6 +160,29 @@ public void TestWorksWithValueOptions()
local.stop();
}

[TestMethod]
public void TestWorksWithCustomOptions()
{
options = new List<KeyValuePair<string, string>>();
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
options.Add(new KeyValuePair<string, string>("customBoolKey1", "true"));
options.Add(new KeyValuePair<string, string>("customBoolKey2", "false"));
options.Add(new KeyValuePair<string, string>("customKey1", "customValue1"));
options.Add(new KeyValuePair<string, string>("customKey2", "customValue2"));

local = new LocalClass();
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>();
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute));
local.setTunnel(tunnelMock.Object);
local.start(options);
tunnelMock.Verify(mock => mock.addBinaryPath(""), Times.Once);
tunnelMock.Verify(mock => mock.addBinaryArguments(
It.IsRegex("-customBoolKey1.*customBoolKey2.*-customKey1.*'customValue1'.*-customKey2.*'customValue2'")
), Times.Once());
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute), Times.Once());
local.stop();
}

[TestMethod]
public void TestCallsFallbackOnFailure()
{
Expand Down
44 changes: 36 additions & 8 deletions BrowserStack/BrowserStack/BrowserStackTunnel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class BrowserStackTunnel : IDisposable

protected StringBuilder output;
public LocalState localState;
protected string logFilePath = "";
protected FileSystemWatcher logfileWatcher;

Job job = null;
Process process = null;
Expand Down Expand Up @@ -120,6 +122,10 @@ public virtual void Run(string accessKey, string folder, string logFilePath)
process.Close();
}

if (File.Exists(logFilePath))
{
File.WriteAllText(logFilePath, string.Empty);
}
Local.logger.Info("BrowserStackLocal binary is located at " + binaryAbsolute);
Local.logger.Info("Starting Binary with arguments " + arguments.Replace(accessKey, "<access_key>"));
ProcessStartInfo processStartInfo = new ProcessStartInfo()
Expand Down Expand Up @@ -190,8 +196,30 @@ public virtual void Run(string accessKey, string folder, string logFilePath)

private void readFile(string filename)
{
using (Stream fileStream = File.Open(filename, FileMode.Create))
fileStream.Write(new Byte[1], 0, 1);
logFilePath = filename;
if (File.Exists(filename))
{
readAlreadyPresentFile(filename);
}
else
{
logfileWatcher = new FileSystemWatcher(new FileInfo(filename).Directory.FullName);
logfileWatcher.Created += readAlreadyPresentFile;
logfileWatcher.Changed += readAlreadyPresentFile;
logfileWatcher.EnableRaisingEvents = true;
}
}

private void readAlreadyPresentFile(object sender, FileSystemEventArgs e)
{
if (e.FullPath.Equals(logFilePath))
{
readAlreadyPresentFile(e.FullPath);
}
}

private void readAlreadyPresentFile(string filename)
{
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
byte[] bytes = new byte[1024];
Expand Down Expand Up @@ -224,7 +252,6 @@ private void TunnelStateChanged(LocalState prevState, LocalState state)
{
connectingEvent.Set();
}
Local.logger.Info("Current tunnel state " + state);
}

public bool IsConnected()
Expand All @@ -236,10 +263,12 @@ public virtual void Kill()
{
try
{
this.process.Kill();
this.process = null;
this.job.Close();
this.job = null;
if (process != null)
process.Kill();
process = null;
if (job != null)
job.Close();
job = null;
}
catch (Exception e)
{
Expand All @@ -251,7 +280,6 @@ public virtual void Kill()
TunnelStateChanged(localState, LocalState.Disconnected);

localState = LocalState.Disconnected;
Local.logger.Info("TunnelState: " + localState.ToString());
}
}

Expand Down
15 changes: 15 additions & 0 deletions BrowserStack/BrowserStack/Local.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ private void addArgs(string key, string value)
else if (key.Equals("logfile"))
{
customLogPath = value;
}
else if (key.Equals("verbose"))
{

}
else
{
result = valueCommands.Find(pair => pair.Key == key);
if (!result.Equals(emptyStringPair))
{
argumentString += result.Value + " " + value + " ";
return;
}

result = booleanCommands.Find(pair => pair.Key == key);
Expand All @@ -83,8 +88,18 @@ private void addArgs(string key, string value)
if (value.Trim().ToLower() == "true")
{
argumentString += result.Value + " ";
return;
}
}

if (value.Trim().ToLower() == "true")
{
argumentString += "-" + key + " ";
}
else
{
argumentString += "-" + key + " " + value + " ";
}
}
}
private void setupLogging()
Expand Down
1 change: 1 addition & 0 deletions BrowserStackExample/BrowserStackExample/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static void Main(string[] args)

driver.Quit();
local.stop();
Console.WriteLine("Test Completed.");
Console.ReadLine();
}
}
Expand Down
136 changes: 113 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,130 @@

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

A simple C-sharp wrapper for BrowserStack Local Binary.

## Setup

Open the solution file `BrowserStack/BrowserStack.sln` in `Visual Studio`. The projects are `Visual Studio 2015` compatible.
You will need to resolve the references from the `Solution Explorer`. `Visual Studio` with automatically download the references from NuGet.

## API
## Example

```
using BrowserStack;

# creates an instance of Local
Local local = new Local();

# replace <browserstack-accesskey> with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("key", "<browserstack-accesskey>"),
}

# starts the Local instance with the required arguments
local.start(bsLocalArgs);

# check if BrowserStack local instance is running
Console.WriteLine(local.isRunning());

# stop the Local instance
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 -
```
bsLocalArgs.Add(new KeyValuePair<string, string>("v", "true"));
```

#### Folder Testing
To test local folder rather internal server, provide path to folder as value of this option -
```
bsLocalArgs.Add(new KeyValuePair<string, string>("f", "/my/awesome/folder"));
```

#### Force Start
To kill other running Browserstack Local instances -
```
bsLocalArgs.Add(new KeyValuePair<string, string>("force", "true"));
```

#### Only Automate
To disable local testing for Live and Screenshots, and enable only Automate -
```
bsLocalArgs.Add(new KeyValuePair<string, string>("onlyAutomate", "true"));
```

#### Force Local
To route all traffic via local(your) machine -
```
bsLocalArgs.Add(new KeyValuePair<string, string>("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

```
bsLocalArgs.Add(new KeyValuePair<string, string>("proxyHost", "127.0.0.1"));
bsLocalArgs.Add(new KeyValuePair<string, string>("proxyPort", "8000"));
bsLocalArgs.Add(new KeyValuePair<string, string>("proxyUser", "user"));
bsLocalArgs.Add(new KeyValuePair<string, string>("proxyPass", "password"));
```

#### Local Identifier
If doing simultaneous multiple local testing connections, set this uniquely for different processes -
```
bsLocalArgs.Add(new KeyValuePair<string, string>("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 -
```
bsLocalArgs.Add(new KeyValuePair<string, string>("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 -
```
bsLocalArgs.Add(new KeyValuePair<string, string>("v", "true"));
bsLocalArgs.Add(new KeyValuePair<string, string>("logfile", "/browserstack/logs.txt"));
```

## Contribute

### Build Instructions

To run the test suite run the nunit tests from Visual Studio.

### Reporting bugs

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

* `new Local()`: creates an instance of Local
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.

### Methods
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.

* `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 and returns a corresponding boolean value
### Pull Requests

### Options
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.

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

## Example

Expand Down