From fb9c05078fc973cf15cf22c5d4965dd00b56018c Mon Sep 17 00:00:00 2001 From: Vibhaj Rajan Date: Thu, 28 Apr 2016 01:14:45 +0530 Subject: [PATCH 1/4] updated readme --- README.md | 129 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 104 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 9ebd058..c2fb468 100644 --- a/README.md +++ b/README.md @@ -2,44 +2,123 @@ [![Build Status](https://travis-ci.org/browserstack/browserstack-local-python.svg?branch=master)](https://travis-ci.org/browserstack/browserstack-local-python) -## Setup +A simple Python wrapper for BrowserStack Local Binary. + +## Installation ``` pip install browserstack-local ``` -## API +## Example + +``` +from browserstack.local import Local + +#creates an instance of Local +bs_local = Local() + +#replace with your key. You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY". +bs_local_args = { "key": "" } + +#starts the Local instance with the required arguments +bs_local.start(**bs_local_args) + +#check if BrowserStack local instance is running +print bs_local.isRunning() + +#stop the Local instance +bs_local.stop() + +``` + +## Arguments -### Constructor +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 - -* `browserstack.Local()`: creates an instance of Local +#### Verbose Logging +To enable verbose logging - +``` +bs_local_args = { "key": "" , "v": "true"} +``` -### Methods +#### Folder Testing +To test local folder rather internal server, provide path to folder as value of this option - +``` +bs_local_args = { "key": "" , "f": "/my/awesome/folder"} +``` -* `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 Start +To kill other running Browserstack Local instances - +``` +bs_local_args = { "key": "" , "force": "true"} +``` -### Options +#### Only Automate +To disable local testing for Live and Screenshots, and enable only Automate - +``` +bs_local_args = { "key": "" , "onlyAutomate": "true"} +``` -* `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 +#### Force Local +To route all traffic via local(your) machine - +``` +bs_local_args = { "key": "" , "forcelocal": "true"} +``` +#### Proxy +To use a proxy for local testing - -## Tests +* 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": "", "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": "" , "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": "" , "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": "" , "v": "true", "logfile": "/browserstack/logs.txt"} +``` + +## Contribute + +### Build Instructions To run the test suite run, `python -m unittest discover`. +### 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. From e7b1aa9edab16689b69595fc891778850a8816e1 Mon Sep 17 00:00:00 2001 From: Vibhaj Rajan Date: Thu, 28 Apr 2016 01:49:16 +0530 Subject: [PATCH 2/4] added dynamic args --- browserstack/local.py | 78 +++++++++++++------------------------------ tests/test_local.py | 32 +++++++++++++----- 2 files changed, 46 insertions(+), 64 deletions(-) diff --git a/browserstack/local.py b/browserstack/local.py index 99ed64f..645bead 100644 --- a/browserstack/local.py +++ b/browserstack/local.py @@ -3,37 +3,42 @@ from browserstack.bserrors import BrowserStackLocalError class Local: - def __init__(self, key=None, binary_path=None): - self.options = { - 'key': key, - 'logfile_flag': '-logFile', - 'logfile_path': os.path.join(os.getcwd(), 'local.log') - } - self.local_folder_path = None - self.local_logfile_path = self.options['logfile_path'] + def __init__(self, key=os.environ['BROWSERSTACK_ACCESS_KEY'], binary_path=None): + self.key = key + self.options = None + self.local_logfile_path = os.path.join(os.getcwd(), 'local.log') - def __xstr(self, obj): - if obj is None: - return '' - return str(obj) + def __xstr(self, key, value): + if key is None: + return [''] + if str(value).lower() == "true": + return ['-' + key] + else: + return ['-' + key, value] def _generate_cmd(self): - options_order = ['logfile_flag', 'logfile_path', 'folder_flag', 'key', 'folder_path', 'forcelocal', 'local_identifier', 'only', 'only_automate', 'proxy_host', 'proxy_port', 'proxy_user', 'proxy_pass', 'forceproxy', 'force', 'verbose', 'hosts'] - cmd = [self.__xstr(self.options.get(o)) for o in options_order if self.options.get(o) is not None] - return [self.binary_path] + cmd + cmd = [self.binary_path, '-logFile', self.local_logfile_path, self.key] + for o in self.options.keys(): + if self.options.get(o) is not None: + cmd = cmd + self.__xstr(o, self.options.get(o)) + return cmd def start(self, **kwargs): - for key, value in kwargs.items(): - self.__add_arg(key, value) - + self.options = kwargs + if 'binarypath' in self.options: self.binary_path = binary_path else: self.binary_path = LocalBinary().get_binary() + if 'logfile' in self.options: + self.local_logfile_path = self.options['logfile'] + del self.options['logfile'] + if "onlyCommand" in kwargs and kwargs["onlyCommand"]: return + print self._generate_cmd() self.proc = subprocess.Popen(self._generate_cmd(), stdout=subprocess.PIPE) self.stderr = self.proc.stderr @@ -56,43 +61,6 @@ def isRunning(self): return True if self.proc.poll() is None else False return False - def __add_arg(self, key, value): - if key == 'verbose' and value: - self.options['verbose'] = '-v' - elif key == 'force' and value: - self.options['force'] = '-force' - elif key == 'only' and value: - self.options['only'] = '-only' - elif key == 'onlyAutomate' and value: - self.options['only_automate'] = '-onlyAutomate' - elif key == 'forcelocal' and value: - self.options['forcelocal'] = '-forcelocal' - elif key == 'localIdentifier': - self.options['local_identifier'] = '-localIdentifier ' + str(value) - elif key == 'f': - self.options['folder_flag'] = '-f' - self.options['folder_path'] = str(value) - elif key == 'proxyHost': - self.options['proxy_host'] = '-proxyHost ' + str(value) - elif key == 'proxyPort': - self.options['proxy_port'] = '-proxyPort ' + str(value) - elif key == 'proxyUser': - self.options['proxy_user'] = '-proxyUser ' + str(value) - elif key == 'proxyPass': - self.options['proxy_pass'] = '-proxyPass ' + str(value) - elif key == 'hosts': - self.options['hosts'] = str(value) - elif key == 'forceproxy' and value: - self.options['forceproxy'] = '-forceproxy' - elif key == 'logfile': - self.options['logfile_flag'] = '-logFile' - self.options['logfile'] = str(value) - self.local_logfile_path = str(value) - elif key == 'binarypath': - self.options['binarypath'] = str(value) - elif key != 'onlyCommand': - raise BrowserStackLocalError('Attempted to pass invalid option to binary') - def stop(self): try: self.proc.terminate() diff --git a/tests/test_local.py b/tests/test_local.py index 86c8052..451f03a 100644 --- a/tests/test_local.py +++ b/tests/test_local.py @@ -13,7 +13,7 @@ def test_start_local(self): self.assertNotEqual(self.local.proc.pid, 0) def test_verbose(self): - self.local.start(verbose=True, onlyCommand=True) + self.local.start(v=True, onlyCommand=True) self.assertIn('-v', self.local._generate_cmd()) def test_local_folder(self): @@ -33,12 +33,28 @@ def test_force_local(self): self.local.start(forcelocal=True, onlyCommand=True) self.assertIn('-forcelocal', self.local._generate_cmd()) + def test_custom_boolean_argument(self): + self.local.start(boolArg1=True, boolArg2=True, onlyCommand=True) + self.assertIn('-boolArg1', self.local._generate_cmd()) + self.assertIn('-boolArg2', self.local._generate_cmd()) + + def test_custom_keyval(self): + self.local.start(customKey1="custom value1", customKey2="custom value2", onlyCommand=True) + self.assertIn('-customKey1', self.local._generate_cmd()) + self.assertIn('custom value1', self.local._generate_cmd()) + self.assertIn('-customKey2', self.local._generate_cmd()) + self.assertIn('custom value2', self.local._generate_cmd()) + def test_proxy(self): self.local.start(proxyHost='localhost', proxyPort=2000, proxyUser='hello', proxyPass='test123', onlyCommand=True) - self.assertIn('-proxyHost localhost', self.local._generate_cmd()) - self.assertIn('-proxyPort 2000', self.local._generate_cmd()) - self.assertIn('-proxyUser hello', self.local._generate_cmd()) - self.assertIn('-proxyPass test123', self.local._generate_cmd()) + self.assertIn('-proxyHost', self.local._generate_cmd()) + self.assertIn('localhost', self.local._generate_cmd()) + self.assertIn('-proxyPort', self.local._generate_cmd()) + self.assertIn(2000, self.local._generate_cmd()) + self.assertIn('-proxyUser', self.local._generate_cmd()) + self.assertIn('hello', self.local._generate_cmd()) + self.assertIn('-proxyPass', self.local._generate_cmd()) + self.assertIn('test123', self.local._generate_cmd()) def test_force_proxy(self): self.local.start(forceproxy=True, onlyCommand=True) @@ -46,10 +62,8 @@ def test_force_proxy(self): def test_local_identifier(self): self.local.start(localIdentifier='mytunnel', onlyCommand=True) - self.assertIn('-localIdentifier mytunnel', self.local._generate_cmd()) - - def test_invalid_option(self): - self.assertRaises(BrowserStackLocalError, lambda: self.local.start(random=True)) + self.assertIn('-localIdentifier', self.local._generate_cmd()) + self.assertIn('mytunnel', self.local._generate_cmd()) def test_running(self): self.assertFalse(self.local.isRunning()) From 90609bad139e43ca9a0c4ba0f9b508af12c359c6 Mon Sep 17 00:00:00 2001 From: Vibhaj Rajan Date: Thu, 28 Apr 2016 01:51:20 +0530 Subject: [PATCH 3/4] removed printf --- browserstack/local.py | 1 - 1 file changed, 1 deletion(-) diff --git a/browserstack/local.py b/browserstack/local.py index 645bead..16c7cfc 100644 --- a/browserstack/local.py +++ b/browserstack/local.py @@ -38,7 +38,6 @@ def start(self, **kwargs): if "onlyCommand" in kwargs and kwargs["onlyCommand"]: return - print self._generate_cmd() self.proc = subprocess.Popen(self._generate_cmd(), stdout=subprocess.PIPE) self.stderr = self.proc.stderr From 4106d57ec4650c97bcbc98d810cda8b18e4681f7 Mon Sep 17 00:00:00 2001 From: Vibhaj Rajan Date: Thu, 28 Apr 2016 01:56:59 +0530 Subject: [PATCH 4/4] readme fixes --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index c2fb468..6bac08f 100644 --- a/README.md +++ b/README.md @@ -103,8 +103,6 @@ bs_local_args = { "key": "" , "v": "true", "logfile": "/ ## Contribute -### Build Instructions - To run the test suite run, `python -m unittest discover`. ### Reporting bugs