Skip to content

Commit ecd687b

Browse files
test1
0 parents  commit ecd687b

12 files changed

+226
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# behave-browserstack
2+
3+
[Behave](https://github.com/behave/behave) Integration with BrowserStack.
4+
5+
![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780)
6+
7+
## Setup
8+
9+
* Clone the repo
10+
* Install dependencies `pip install -r requirements.txt`
11+
* Update `*.json` files inside the `config/` directory with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings)
12+
13+
## Running your tests
14+
* To run a single test, run `paver run single`
15+
* To run local tests, run `paver run local`
16+
* To run parallel tests, run `paver run parallel`
17+
18+
Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github)
19+
20+
## Notes
21+
* You can view your test results on the [BrowserStack Automate dashboard](https://www.browserstack.com/automate)
22+
* To test on a different set of browsers, check out our [platform configurator](https://www.browserstack.com/automate/python#setting-os-and-browser)
23+
* You can export the environment variables for the Username and Access Key of your BrowserStack account
24+
25+
```
26+
export BROWSERSTACK_USERNAME=<browserstack-username> &&
27+
export BROWSERSTACK_ACCESS_KEY=<browserstack-access-key>
28+
```
29+
30+
## Additional Resources
31+
* [Documentation for writing Automate test scripts in Python](https://www.browserstack.com/automate/python)
32+
* [Customizing your tests on BrowserStack](https://www.browserstack.com/automate/capabilities)
33+
* [Browsers & mobile devices for selenium testing on BrowserStack](https://www.browserstack.com/list-of-browsers-and-platforms?product=automate)
34+
* [Using REST API to access information about your tests via the command-line interface](https://www.browserstack.com/automate/rest-api)

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": "bhumikababbar_AJNnOF",
3+
"key": "6pfGVnFrDLssY713tZFQ",
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from selenium import webdriver
2+
from browserstack.local import Local
3+
import os, json
4+
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)
10+
11+
bs_local = None
12+
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']
15+
16+
def start_local():
17+
"""Code to start browserstack local before start of test."""
18+
global bs_local
19+
bs_local = Local()
20+
bs_local_args = { "key": BROWSERSTACK_ACCESS_KEY, "forcelocal": "true" }
21+
bs_local.start(**bs_local_args)
22+
23+
def stop_local():
24+
"""Code to stop browserstack local after end of test."""
25+
global bs_local
26+
if bs_local is not None:
27+
bs_local.stop()
28+
29+
30+
def before_feature(context, feature):
31+
desired_capabilities = CONFIG['environments'][TASK_ID]
32+
33+
for key in CONFIG["capabilities"]:
34+
if key not in desired_capabilities:
35+
desired_capabilities[key] = CONFIG["capabilities"][key]
36+
37+
if "browserstack.local" in desired_capabilities and desired_capabilities["browserstack.local"]:
38+
start_local()
39+
40+
context.browser = webdriver.Remote(
41+
desired_capabilities=desired_capabilities,
42+
command_executor="http://%s:%[email protected]/wd/hub" % (BROWSERSTACK_USERNAME, BROWSERSTACK_ACCESS_KEY)
43+
)
44+
45+
def after_feature(context, feature):
46+
if context.failed is True:
47+
context.browser.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "At least 1 assertion failed"}}')
48+
if context.failed is not True:
49+
context.browser.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "All assertions passed"}}')
50+
context.browser.quit()
51+
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/single.feature

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Feature: Google\'s Search Functionality
2+
Scenario: can find search results
3+
When visit url "http://www.google.com/ncr"
4+
When field with name "q" is given "BrowserStack"
5+
Then title becomes "BrowserStack - Google Search"

features/steps/steps.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import time
2+
3+
@when('visit url "{url}"')
4+
def step(context, url):
5+
context.browser.get(url)
6+
7+
@when('field with name "{selector}" is given "{value}"')
8+
def step(context, selector, value):
9+
elem = context.browser.find_element_by_name(selector)
10+
elem.send_keys(value)
11+
elem.submit()
12+
time.sleep(5)
13+
14+
@then('title becomes "{title}"')
15+
def step(context, title):
16+
assert context.browser.title == title
17+
18+
@then(u'page contains "{body}"')
19+
def step(context, body):
20+
assert body in context.browser.page_source

pavement.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from paver.easy import *
2+
from paver.setuputils import setup
3+
import threading, os, platform
4+
5+
setup(
6+
name = "behave-browserstack",
7+
version = "0.1.0",
8+
author = "BrowserStack",
9+
author_email = "[email protected]",
10+
description = ("Behave Integration with BrowserStack"),
11+
license = "MIT",
12+
keywords = "example selenium browserstack",
13+
url = "https://github.com/browserstack/lettuce-browserstack",
14+
packages=['features']
15+
)
16+
17+
def run_behave_test(config, feature, task_id=0):
18+
if platform.system() == 'Windows':
19+
sh('SET CONFIG_FILE=config/%s.json & SET TASK_ID=%s & behave features/%s.feature' % (config, task_id, feature))
20+
else:
21+
sh('export CONFIG_FILE=config/%s.json && export TASK_ID=%s && behave features/%s.feature' % (config, task_id, feature))
22+
23+
@task
24+
@consume_nargs(1)
25+
def run(args):
26+
"""Run single, local and parallel test using different config."""
27+
if args[0] in ('single', 'local'):
28+
run_behave_test(args[0], args[0])
29+
else:
30+
jobs = []
31+
for i in range(4):
32+
p = threading.Thread(target=run_behave_test,args=(args[0], "single",i))
33+
jobs.append(p)
34+
p.start()
35+
36+
for th in jobs:
37+
th.join()
38+
39+
@task
40+
def test():
41+
"""Run all tests"""
42+
sh("paver run single")
43+
sh("paver run local")
44+
sh("paver run parallel")

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
behave
2+
browserstack-local
3+
paver
4+
selenium
5+
psutil

0 commit comments

Comments
 (0)