Skip to content

Commit 06e553c

Browse files
committed
added local single and parallel tests
1 parent 490a6de commit 06e553c

14 files changed

Lines changed: 161 additions & 89 deletions

‎.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
local.log
2-
src
2+
**.pyc

‎.travis.yml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
language: python
2+
3+
python:
4+
- "2.7"
5+
- "3.2"
6+
- "3.3"
7+
- "3.4"
8+
- "3.5"
9+
10+
before_install:
11+
- true && `base64 --decode <<< ZXhwb3J0IEJST1dTRVJTVEFDS19BQ0NFU1NfS0VZPUh5VmZydXJvb3dYb041eGhLZEs2Cg==`
12+
- pip install -r requirements.txt
13+
14+
script: paver test

‎README.md‎

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
1-
# Behave-BrowserStack
1+
# behave-browserstack
22

3-
### Install dependencies
3+
[Behave](http://pythonhosted.org/behave/) Integration with BrowserStack.
44

5-
Navigate to appropriate directory for testing and then install the dependencies by running
5+
## Setup
66

7-
`pip install -r requirements.txt`
8-
9-
A sample to run behave over BrowserStack Automate.
10-
11-
### BrowserStack Authentication
12-
13-
Export the environment variables for the username and access key of your BrowserStack account.
14-
These can be found on the automate accounts page on [BrowserStack](https://www.browserstack.com/accounts/automate)
15-
16-
`export BROWSERSTACK_USERNAME=<browserstack-username>`
17-
18-
`export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>`
7+
- Clone the repo
8+
- Install dependencies `pip install -r requirements.txt`
9+
- Update `*conf.json` with your BrowserStack credentials `user` and `key`
1910

2011
### Run the tests
2112

22-
- To run series tests, run `behave`
23-
- To run series tests with local, run `TEST_TYPE=local behave`
24-
- To run tests with parallel, run `TEST_TYPE=parallel behave-parallel --processes 4 --parallel-element feature`
25-
- To run tests with parallel with local, run `TEST_TYPE=parallel,local behave-parallel --processes 4 --parallel-element feature`
26-
27-
------
28-
29-
#### How to specify the capabilities
30-
31-
The [Code Generator](https://www.browserstack.com/automate/python#setting-os-and-browser) can come in very handy when specifying the capabilities especially for mobile devices.
13+
- To run single test, run `paver run single`
14+
- To run parallel tests, run `paver run parallel`
15+
- To run local tests, run `paver run local`

‎config/local.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"user": "BROWSERSTACK_USERNAME",
3+
"key": "BROWSERSTACK_ACCESS_KEY",
4+
5+
"capabilities": {
6+
"build": "behave-browserstack",
7+
"name": "local_test",
8+
"browserstack.debug": true,
9+
"browserstack.local": true
10+
},
11+
12+
"environments": [{
13+
"browser": "chrome"
14+
}]
15+
}

‎config/parallel.json‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"user": "BROWSERSTACK_USERNAME",
3+
"key": "BROWSERSTACK_ACCESS_KEY",
4+
5+
"capabilities": {
6+
"build": "behave-browserstack",
7+
"name": "parallel_test",
8+
"browserstack.debug": true
9+
},
10+
11+
"environments": [{
12+
"browser": "chrome"
13+
},{
14+
"browser": "firefox"
15+
},{
16+
"browser": "safari"
17+
},{
18+
"browser": "internet explorer"
19+
}]
20+
}

‎config/single.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"user": "BROWSERSTACK_USERNAME",
3+
"key": "BROWSERSTACK_ACCESS_KEY",
4+
5+
"capabilities": {
6+
"build": "behave-browserstack",
7+
"name": "single_test",
8+
"browserstack.debug": true
9+
},
10+
11+
"environments": [{
12+
"browser": "chrome"
13+
}]
14+
}

‎features/environment.py‎

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
import os
21
from selenium import webdriver
32
from browserstack.local import Local
3+
import os, json
44

5-
# Edit these to match your credentials
6-
USERNAME = None
7-
BROWSERSTACK_ACCESS_KEY = None
8-
test_type = 'single'
9-
bs_local = None
5+
CONFIG_FILE = os.environ['CONFIG_FILE'] if 'CONFIG_FILE' in os.environ else 'config/single.json'
6+
TASK_ID = int(os.environ['TASK_ID']) if 'TASK_ID' in os.environ else 0
7+
8+
with open(CONFIG_FILE) as data_file:
9+
CONFIG = json.load(data_file)
1010

11-
if "BROWSERSTACK_USERNAME" in os.environ:
12-
USERNAME = os.environ["BROWSERSTACK_USERNAME"]
13-
if "BROWSERSTACK_ACCESS_KEY" in os.environ:
14-
BROWSERSTACK_ACCESS_KEY = os.environ["BROWSERSTACK_ACCESS_KEY"]
15-
if "TEST_TYPE" in os.environ:
16-
test_type = os.environ["TEST_TYPE"]
11+
bs_local = None
1712

18-
if not (USERNAME and BROWSERSTACK_ACCESS_KEY):
19-
raise Exception("Please provide your BrowserStack username and access key")
20-
sys.exit(1)
13+
BROWSERSTACK_USERNAME = os.environ['BROWSERSTACK_USERNAME'] if 'BROWSERSTACK_USERNAME' in os.environ else CONFIG['user']
14+
BROWSERSTACK_ACCESS_KEY = os.environ['BROWSERSTACK_ACCESS_KEY'] if 'BROWSERSTACK_ACCESS_KEY' in os.environ else CONFIG['key']
2115

2216
def start_local():
2317
global bs_local
@@ -30,43 +24,22 @@ def stop_local():
3024
if bs_local is not None:
3125
bs_local.stop()
3226

33-
def get_caps():
34-
desired_capabilities = {}
35-
desired_capabilities['os'] = 'OS X'
36-
desired_capabilities['os_version'] = 'El Capitan'
37-
desired_capabilities['browser'] = 'firefox'
38-
desired_capabilities['browser_version'] = '46'
39-
desired_capabilities['build'] = 'Sample behave tests'
40-
desired_capabilities['name'] = 'Sample behave test'
41-
if 'local' in test_type.lower():
42-
desired_capabilities['browserstack.local'] = True
43-
return desired_capabilities
4427

4528
def before_feature(context, feature):
46-
if 'parallel' in test_type.lower():
47-
context.browser = create_driver()
29+
desired_capabilities = CONFIG['environments'][TASK_ID]
4830

49-
def after_feature(context, feature):
50-
if 'parallel' in test_type.lower():
51-
context.browser.quit()
31+
for key in CONFIG["capabilities"]:
32+
if key not in desired_capabilities:
33+
desired_capabilities[key] = CONFIG["capabilities"][key]
5234

53-
def before_all(context):
54-
if 'local' in test_type.lower():
35+
if "browserstack.local" in desired_capabilities and desired_capabilities["browserstack.local"]:
5536
start_local()
56-
if 'parallel' not in test_type.lower():
57-
context.browser = create_driver()
5837

59-
def after_all(context):
60-
stop_local()
61-
if 'parallel' not in test_type.lower():
62-
context.browser.quit()
63-
64-
def create_driver():
65-
desired_capabilities = get_caps()
38+
context.browser = webdriver.Remote(
39+
desired_capabilities=desired_capabilities,
40+
command_executor="http://%s:%s@hub.browserstack.com/wd/hub" % (BROWSERSTACK_USERNAME, BROWSERSTACK_ACCESS_KEY)
41+
)
6642

67-
return webdriver.Remote(
68-
desired_capabilities=desired_capabilities,
69-
command_executor="http://%s:%s@hub.browserstack.com/wd/hub" % (
70-
USERNAME, BROWSERSTACK_ACCESS_KEY
71-
)
72-
)
43+
def after_feature(context, feature):
44+
context.browser.quit()
45+
stop_local()

‎features/local.feature‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Feature: BrowserStack Local Testing
2+
Scenario: can check tunnel working
3+
When visit url "http://bs-local.com:45691/check"
4+
Then page contains "Up and running"

‎features/sample.feature‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎features/sample_clone.feature‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)