Skip to content

Commit 9dbdf93

Browse files
committed
Merge pull request #5 from browserstack/custom_args
Custom arguments
2 parents a9fe20c + bd47eb5 commit 9dbdf93

File tree

6 files changed

+189
-32
lines changed

6 files changed

+189
-32
lines changed

BrowserStack.dll

512 Bytes
Binary file not shown.

BrowserStack/BrowserStack Unit Tests/LocalTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void TestWorksWithBooleanOptions()
130130
local.setTunnel(tunnelMock.Object);
131131
local.start(options);
132132
tunnelMock.Verify(mock => mock.addBinaryPath(""), Times.Once);
133-
tunnelMock.Verify(mock => mock.addBinaryArguments(It.IsRegex("-vvv.*-force.*-forcelocal*-forceproxy.*-onlyAutomate")), Times.Once());
133+
tunnelMock.Verify(mock => mock.addBinaryArguments(It.IsRegex("-vvv.*-force.*-forcelocal.*-forceproxy.*-onlyAutomate")), Times.Once());
134134
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute), Times.Once());
135135
local.stop();
136136
}
@@ -160,6 +160,29 @@ public void TestWorksWithValueOptions()
160160
local.stop();
161161
}
162162

163+
[TestMethod]
164+
public void TestWorksWithCustomOptions()
165+
{
166+
options = new List<KeyValuePair<string, string>>();
167+
options.Add(new KeyValuePair<string, string>("key", "dummyKey"));
168+
options.Add(new KeyValuePair<string, string>("customBoolKey1", "true"));
169+
options.Add(new KeyValuePair<string, string>("customBoolKey2", "false"));
170+
options.Add(new KeyValuePair<string, string>("customKey1", "customValue1"));
171+
options.Add(new KeyValuePair<string, string>("customKey2", "customValue2"));
172+
173+
local = new LocalClass();
174+
Mock<BrowserStackTunnel> tunnelMock = new Mock<BrowserStackTunnel>();
175+
tunnelMock.Setup(mock => mock.Run("dummyKey", "", logAbsolute));
176+
local.setTunnel(tunnelMock.Object);
177+
local.start(options);
178+
tunnelMock.Verify(mock => mock.addBinaryPath(""), Times.Once);
179+
tunnelMock.Verify(mock => mock.addBinaryArguments(
180+
It.IsRegex("-customBoolKey1.*customBoolKey2.*-customKey1.*'customValue1'.*-customKey2.*'customValue2'")
181+
), Times.Once());
182+
tunnelMock.Verify(mock => mock.Run("dummyKey", "", logAbsolute), Times.Once());
183+
local.stop();
184+
}
185+
163186
[TestMethod]
164187
public void TestCallsFallbackOnFailure()
165188
{

BrowserStack/BrowserStack/BrowserStackTunnel.cs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class BrowserStackTunnel : IDisposable
3030

3131
protected StringBuilder output;
3232
public LocalState localState;
33+
protected string logFilePath = "";
34+
protected FileSystemWatcher logfileWatcher;
3335

3436
Job job = null;
3537
Process process = null;
@@ -120,6 +122,10 @@ public virtual void Run(string accessKey, string folder, string logFilePath)
120122
process.Close();
121123
}
122124

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

191197
private void readFile(string filename)
192198
{
193-
using (Stream fileStream = File.Open(filename, FileMode.Create))
194-
fileStream.Write(new Byte[1], 0, 1);
199+
logFilePath = filename;
200+
if (File.Exists(filename))
201+
{
202+
readAlreadyPresentFile(filename);
203+
}
204+
else
205+
{
206+
logfileWatcher = new FileSystemWatcher(new FileInfo(filename).Directory.FullName);
207+
logfileWatcher.Created += readAlreadyPresentFile;
208+
logfileWatcher.Changed += readAlreadyPresentFile;
209+
logfileWatcher.EnableRaisingEvents = true;
210+
}
211+
}
212+
213+
private void readAlreadyPresentFile(object sender, FileSystemEventArgs e)
214+
{
215+
if (e.FullPath.Equals(logFilePath))
216+
{
217+
readAlreadyPresentFile(e.FullPath);
218+
}
219+
}
220+
221+
private void readAlreadyPresentFile(string filename)
222+
{
195223
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
196224
{
197225
byte[] bytes = new byte[1024];
@@ -224,7 +252,6 @@ private void TunnelStateChanged(LocalState prevState, LocalState state)
224252
{
225253
connectingEvent.Set();
226254
}
227-
Local.logger.Info("Current tunnel state " + state);
228255
}
229256

230257
public bool IsConnected()
@@ -236,10 +263,12 @@ public virtual void Kill()
236263
{
237264
try
238265
{
239-
this.process.Kill();
240-
this.process = null;
241-
this.job.Close();
242-
this.job = null;
266+
if (process != null)
267+
process.Kill();
268+
process = null;
269+
if (job != null)
270+
job.Close();
271+
job = null;
243272
}
244273
catch (Exception e)
245274
{
@@ -251,7 +280,6 @@ public virtual void Kill()
251280
TunnelStateChanged(localState, LocalState.Disconnected);
252281

253282
localState = LocalState.Disconnected;
254-
Local.logger.Info("TunnelState: " + localState.ToString());
255283
}
256284
}
257285

BrowserStack/BrowserStack/Local.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,18 @@ private void addArgs(string key, string value)
6868
else if (key.Equals("logfile"))
6969
{
7070
customLogPath = value;
71+
}
72+
else if (key.Equals("verbose"))
73+
{
74+
7175
}
7276
else
7377
{
7478
result = valueCommands.Find(pair => pair.Key == key);
7579
if (!result.Equals(emptyStringPair))
7680
{
7781
argumentString += result.Value + " " + value + " ";
82+
return;
7883
}
7984

8085
result = booleanCommands.Find(pair => pair.Key == key);
@@ -83,8 +88,18 @@ private void addArgs(string key, string value)
8388
if (value.Trim().ToLower() == "true")
8489
{
8590
argumentString += result.Value + " ";
91+
return;
8692
}
8793
}
94+
95+
if (value.Trim().ToLower() == "true")
96+
{
97+
argumentString += "-" + key + " ";
98+
}
99+
else
100+
{
101+
argumentString += "-" + key + " " + value + " ";
102+
}
88103
}
89104
}
90105
private void setupLogging()

BrowserStackExample/BrowserStackExample/Example.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static void Main(string[] args)
4646

4747
driver.Quit();
4848
local.stop();
49+
Console.WriteLine("Test Completed.");
4950
Console.ReadLine();
5051
}
5152
}

README.md

Lines changed: 113 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,130 @@
22

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

5+
A simple C-sharp wrapper for BrowserStack Local Binary.
6+
57
## Setup
68

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

10-
## API
12+
## Example
13+
14+
```
15+
using BrowserStack;
16+
17+
# creates an instance of Local
18+
Local local = new Local();
19+
20+
# replace <browserstack-accesskey> with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
21+
List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() {
22+
new KeyValuePair<string, string>("key", "<browserstack-accesskey>"),
23+
}
24+
25+
# starts the Local instance with the required arguments
26+
local.start(bsLocalArgs);
27+
28+
# check if BrowserStack local instance is running
29+
Console.WriteLine(local.isRunning());
30+
31+
# stop the Local instance
32+
local.stop();
33+
```
34+
35+
## Arguments
36+
37+
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 -
38+
39+
#### Verbose Logging
40+
To enable verbose logging -
41+
```
42+
bsLocalArgs.Add(new KeyValuePair<string, string>("v", "true"));
43+
```
44+
45+
#### Folder Testing
46+
To test local folder rather internal server, provide path to folder as value of this option -
47+
```
48+
bsLocalArgs.Add(new KeyValuePair<string, string>("f", "/my/awesome/folder"));
49+
```
50+
51+
#### Force Start
52+
To kill other running Browserstack Local instances -
53+
```
54+
bsLocalArgs.Add(new KeyValuePair<string, string>("force", "true"));
55+
```
56+
57+
#### Only Automate
58+
To disable local testing for Live and Screenshots, and enable only Automate -
59+
```
60+
bsLocalArgs.Add(new KeyValuePair<string, string>("onlyAutomate", "true"));
61+
```
62+
63+
#### Force Local
64+
To route all traffic via local(your) machine -
65+
```
66+
bsLocalArgs.Add(new KeyValuePair<string, string>("forcelocal", "true"));
67+
```
68+
69+
#### Proxy
70+
To use a proxy for local testing -
71+
72+
* proxyHost: Hostname/IP of proxy, remaining proxy options are ignored if this option is absent
73+
* proxyPort: Port for the proxy, defaults to 3128 when -proxyHost is used
74+
* proxyUser: Username for connecting to proxy (Basic Auth Only)
75+
* proxyPass: Password for USERNAME, will be ignored if USERNAME is empty or not specified
76+
77+
```
78+
bsLocalArgs.Add(new KeyValuePair<string, string>("proxyHost", "127.0.0.1"));
79+
bsLocalArgs.Add(new KeyValuePair<string, string>("proxyPort", "8000"));
80+
bsLocalArgs.Add(new KeyValuePair<string, string>("proxyUser", "user"));
81+
bsLocalArgs.Add(new KeyValuePair<string, string>("proxyPass", "password"));
82+
```
83+
84+
#### Local Identifier
85+
If doing simultaneous multiple local testing connections, set this uniquely for different processes -
86+
```
87+
bsLocalArgs.Add(new KeyValuePair<string, string>("localIdentifier", "randomstring"));
88+
```
89+
90+
## Additional Arguments
91+
92+
#### Binary Path
93+
94+
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.
95+
Path to specify local Binary path -
96+
```
97+
bsLocalArgs.Add(new KeyValuePair<string, string>("binarypath", "/browserstack/BrowserStackLocal"));
98+
```
99+
100+
#### Logfile
101+
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.
102+
To specify the path to file where the logs will be saved -
103+
```
104+
bsLocalArgs.Add(new KeyValuePair<string, string>("v", "true"));
105+
bsLocalArgs.Add(new KeyValuePair<string, string>("logfile", "/browserstack/logs.txt"));
106+
```
107+
108+
## Contribute
109+
110+
### Build Instructions
111+
112+
To run the test suite run the nunit tests from Visual Studio.
113+
114+
### Reporting bugs
11115

12-
### Constructor
116+
You can submit bug reports either in the Github issue tracker.
13117

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

16-
### Methods
120+
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.
17121

18-
* `start(options)`: starts Local instance with options. The options available are detailed below.
19-
* `stop()`: stops the Local instance
20-
* `isRunning()`: checks if Local instance is running and returns a corresponding boolean value
122+
### Pull Requests
21123

22-
### Options
124+
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.
23125

24-
* `key`: BrowserStack Access Key
25-
* `v`: Provides verbose logging
26-
* `f`: If you want to test local folder rather internal server, provide path to folder as value of this option
27-
* `force`: Kill other running Browserstack Local
28-
* `only`: Restricts Local Testing access to specified local servers and/or folders
29-
* `forcelocal`: Route all traffic via local machine
30-
* `onlyAutomate`: Disable Live Testing and Screenshots, just test Automate
31-
* `proxyHost`: Hostname/IP of proxy, remaining proxy options are ignored if this option is absent
32-
* `proxyPort`: Port for the proxy, defaults to 3128 when -proxyHost is used
33-
* `proxyUser`: Username for connecting to proxy (Basic Auth Only)
34-
* `proxyPass`: Password for USERNAME, will be ignored if USERNAME is empty or not specified
35-
* `localIdentifier`: If doing simultaneous multiple local testing connections, set this uniquely for different processes
36-
* `hosts`: List of hosts and ports where Local must be enabled for eg. localhost,3000,1,localhost,3001,0
37-
* `logfile`: Path to file where Local logs be saved to
38-
* `binarypath`: Optional path to Local binary
126+
* Adhere to the coding conventions you see in the surrounding code.
127+
* Include tests, and make sure all tests pass.
128+
* 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.
39129

40130
## Example
41131

0 commit comments

Comments
 (0)