Skip to content

Commit e61cb39

Browse files
committed
Restructure
1 parent e00cfa6 commit e61cb39

5 files changed

Lines changed: 95 additions & 15 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
local.log
2+
src

‎README.md‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
1-
# behave-browserstack
1+
# Behave-BrowserStack
2+
3+
### Install dependencies
4+
5+
Navigate to appropriate directory for testing and then install the dependencies by running
6+
7+
`pip install -r requirements.txt`
28

39
A sample to run behave over BrowserStack Automate.
410

5-
To run the tests, add you credentials to `features/environment.py` and execute:
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_KEY=<browserstack-access-key>`
19+
20+
### Run the tests
21+
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
630

7-
```
8-
behave
9-
```
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.

‎features/environment.py‎

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,72 @@
1+
import os
12
from selenium import webdriver
3+
from browserstack.local import Local
24

35
# Edit these to match your credentials
46
USERNAME = None
57
BROWSERSTACK_KEY = None
8+
test_type = 'single'
9+
bs_local = None
10+
11+
if "BROWSERSTACK_USERNAME" in os.environ:
12+
USERNAME = os.environ["BROWSERSTACK_USERNAME"]
13+
if "BROWSERSTACK_ACCESS_KEY" in os.environ:
14+
BROWSERSTACK_KEY = os.environ["BROWSERSTACK_ACCESS_KEY"]
15+
if "TEST_TYPE" in os.environ:
16+
test_type = os.environ["TEST_TYPE"]
617

718
if not (USERNAME and BROWSERSTACK_KEY):
819
raise Exception("Please provide your BrowserStack username and access key")
920
sys.exit(1)
1021

11-
def before_all(context):
12-
desired_capabilities = webdriver.DesiredCapabilities.FIREFOX
13-
desired_capabilities['platform'] = 'WINDOWS'
22+
def start_local():
23+
global bs_local
24+
bs_local = Local()
25+
bs_local_args = { "key": BROWSERSTACK_KEY, "forcelocal": "true" }
26+
bs_local.start(**bs_local_args)
27+
28+
def stop_local():
29+
global bs_local
30+
if bs_local is not None:
31+
bs_local.stop()
32+
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
44+
45+
def before_feature(context, feature):
46+
if 'parallel' in test_type.lower():
47+
context.browser = create_driver()
1448

15-
context.browser = webdriver.Remote(
16-
desired_capabilities=desired_capabilities,
17-
command_executor="http://%s:%s@hub.browserstack.com/wd/hub" % (
18-
USERNAME, BROWSERSTACK_KEY
19-
)
20-
)
49+
def after_feature(context, feature):
50+
if 'parallel' in test_type.lower():
51+
context.browser.quit()
52+
53+
def before_all(context):
54+
if 'local' in test_type.lower():
55+
start_local()
56+
if 'parallel' not in test_type.lower():
57+
context.browser = create_driver()
2158

22-
def after_all(context):
59+
def after_all(context):
60+
stop_local()
61+
if 'parallel' not in test_type.lower():
2362
context.browser.quit()
63+
64+
def create_driver():
65+
desired_capabilities = get_caps()
66+
67+
return webdriver.Remote(
68+
desired_capabilities=desired_capabilities,
69+
command_executor="http://%s:%s@hub.browserstack.com/wd/hub" % (
70+
USERNAME, BROWSERSTACK_KEY
71+
)
72+
)

‎features/sample_clone.feature‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Feature: title test
2+
Scenario: visiting google
3+
When visiting google
4+
Then its title should be "Google"

‎requirements.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
behave
2+
-e git+git://github.com/hugeinc/behave-parallel.git@a57eff3087aae9bd7519a7d4fc15228611db20b9#egg=behave-parallel
3+
browserstack-local

0 commit comments

Comments
 (0)