Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.linting.pylintEnabled": true,
"python.pythonPath": "/usr/local/bin/python"
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
automate-python-samples
=======================
# automate-python-samples

Documentation for writing Automate test scripts in Python.

## Environment variables
To test varios sample repositories with ease, it is recommended to setup `BROWSERSTACK_USERNAME` and `BROWSERSTACK_ACCESS_KEY` environment variables. If you don't wish you can directly edit the samples and add add credentials in them.

## Install Python

### For Windows:
Expand Down
24 changes: 20 additions & 4 deletions google-search-browserstack.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
import os
import sys

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

try:
USERNAME = sys.argv[1]
BROWSERSTACK_ACCESS_KEY = sys.argv[2]
USERNAME = os.environ.get('BROWSERSTACK_USERNAME') or sys.argv[1]
BROWSERSTACK_ACCESS_KEY = os.environ.get(
'BROWSERSTACK_ACCESS_KEY') or sys.argv[2]
except IndexError:
print("Pleaes provide the username and browserstack access key as command line arguments.")
sys.exit(1)

capabilities = {
'browserName': 'Firefox',
'browserVersion': '65.0',
'browserstack.use_w3c': 'true',
'bstack:options': {
'os': 'Windows',
'buildName': 'automate-python-samples',
'osVersion': '10',
'sessionName': 'single_test',
'projectName': 'Sample project',
'debug': 'true'
}
}

driver = webdriver.Remote(
command_executor='http://%s:%[email protected]/wd/hub' %(
command_executor='https://%s:%[email protected]/wd/hub' % (
USERNAME, BROWSERSTACK_ACCESS_KEY
),
desired_capabilities=DesiredCapabilities.FIREFOX
desired_capabilities = capabilities
)

driver.get("http://www.google.com")
Expand Down
15 changes: 0 additions & 15 deletions google-search-local.py

This file was deleted.

13 changes: 13 additions & 0 deletions nose-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# A sample to run nosetests over BrowserStack Automate.

### Setup
`pip install nose2`


### Running the tests

To run the tests, add you credentials to `google-search-browserstack.py` and execute:

```
nose2 google-search-browserstack
```
7 changes: 0 additions & 7 deletions nose-test/README.mkd

This file was deleted.

26 changes: 20 additions & 6 deletions nose-test/google-search-browserstack.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
import unittest

Expand All @@ -6,23 +7,36 @@
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Edit these to match your credentials
USERNAME = None
BROWSERSTACK_ACCESS_KEY = None
USERNAME = os.environ.get('BROWSERSTACK_USERNAME') or sys.argv[3]
BROWSERSTACK_ACCESS_KEY = os.environ.get(
'BROWSERSTACK_ACCESS_KEY') or sys.argv[4]

if not (USERNAME and BROWSERSTACK_ACCESS_KEY):
raise Exception("Please provide your BrowserStack username and access key")
sys.exit(1)


class PythonOrgSearch(unittest.TestCase):

def setUp(self):
url = "http://%s:%[email protected]/wd/hub" %(
url = "https://%s:%[email protected]/wd/hub" % (
USERNAME, BROWSERSTACK_ACCESS_KEY
)

capabilities = {
'browserName': 'Firefox',
'browserVersion': '65.0',
'browserstack.use_w3c': 'true',
'bstack:options': {
'os': 'Windows',
'buildName': 'automate-python-samples',
'osVersion': '10',
'sessionName': 'nose_test',
'projectName': 'Sample project',
'debug': 'true'
}
}
self.driver = webdriver.Remote(
command_executor=url,
desired_capabilities=DesiredCapabilities.FIREFOX
desired_capabilities=capabilities
)

def test_search_in_python_org(self):
Expand Down
51 changes: 5 additions & 46 deletions parallel_tests/README.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,12 @@
This project contains 3 files. Each of the files is described below.

browsers.json - This file specifies the browser capabilites for the tests. Here, each test would be run in these two browsers.

[
{
"browserName": "iPhone",
"platform": "MAC",
"device": "iPhone 5"
},
{
"browser": "firefox",
"browser_version": "17.0",
"os": "Windows",
"os_version": "8"
}
]
# Parallel Testing on BrowserStack

This project contains 3 files. Each of the files is described below.

test.py - This file contains the selenium test which would be run in each of the browsers specificed by "browsers.json".
`browsers.json` - This file specifies the browser capabilites for the tests. Here, each test would be run in these two browsers.

from selenium import webdriver
import sys, json

json_name = sys.argv[1]
with open(json_name, "r") as f:
obj = json.loads(f.read())
instance_caps= obj[int(sys.argv[2])]
print "Test "+sys.argv[2]+" started"
caps = {}

# Mention any other capabilities required in the test
caps["browserstack.debug"] = "true"
caps["build"] = "parallel tests"

caps = dict(caps.items() + instance_caps.items())

# Sample selenium test
driver = webdriver.Remote(
command_executor='http://<browserstack_UserName>:<browserstack_AuthKey>@hub.browserstack.com/wd/hub',
desired_capabilities=caps)

driver.get("http://www.google.com")
inputElement = driver.find_element_by_name("q")
inputElement.send_keys("browserstack")
inputElement.submit()
print driver.title
driver.quit()
`python test.py <browsers.json> <capability id>` - This file contains the selenium test which would be run in each of the browsers specificed by "browsers.json".

run_parallel_tests.py - This is the runner which runs the tests in parallel.
`python run_parallel_tests.py <test.py> <browsers.json>` - This is the runner which runs the tests in parallel.

To run the tests in parallel execute the following command:

Expand Down
61 changes: 32 additions & 29 deletions parallel_tests/browsers.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
[
{
"browserName": "iPhone",
"platform": "MAC",
"device": "iPhone 5"
"browserName": "Firefox",
"browserVersion": "42.0",
"browserstack.use_w3c": true,
"bstack:options": {
"os": "OS X",
"osVersion": "Snow Leopard",
"sessionName": "parallel_test",
"buildName": "automate-python-samples",
"projectName": "Sample Project",
"debug": true
}
},
{
"browser": "firefox",
"browser_version": "17.0",
"os": "Windows",
"os_version": "8"
"browserName": "Internet Explorer",
"browserVersion": "10",
"browserstack.use_w3c": true,
"bstack:options": {
"os": "Windows",
"sessionName": "parallel_test",
"buildName": "automate-python-samples",
"projectName": "Sample Project",
"debug": true
}
},
{
"browser": "ie",
"browser_version": "8.0",
"os": "Windows",
"os_version": "7"
},
{
"browser": "ie",
"browser_version": "9.0",
"os": "Windows",
"os_version": "7"
},
{
"browser": "ie",
"browser_version": "10.0",
"os": "Windows",
"os_version": "8"
},
{
"browser": "firefox",
"browser_version": "15.0",
"os": "OS X",
"os_version": "Snow Leopard"
"browserName": "Chrome",
"browserVersion": "70.0",
"browserstack.use_w3c": true,
"bstack:options": {
"os": "Windows",
"osVersion": "7",
"sessionName": "parallel_test",
"buildName": "automate-python-samples",
"projectName": "Sample Project",
"debug": true
}
}
]
27 changes: 12 additions & 15 deletions parallel_tests/test.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
from selenium import webdriver
import sys, json
import os
import sys
import json

json_name = sys.argv[1]
USERNAME = os.environ.get('BROWSERSTACK_USERNAME') or sys.argv[2]
BROWSERSTACK_ACCESS_KEY = os.environ.get(
'BROWSERSTACK_ACCESS_KEY') or sys.argv[3]

with open(json_name, "r") as f:
obj = json.loads(f.read())

instance_caps= obj[int(sys.argv[2])]
caps = obj[int(sys.argv[2])]
print "Test "+sys.argv[2]+" started"

#------------------------------------------------------#
# Mention any other capabilities required in the test
caps = {}
caps["browserstack.debug"] = "true"
caps["build"] = "parallel tests"

#------------------------------------------------------#

caps = dict(caps.items() + instance_caps.items())

#------------------------------------------------------#
# THE TEST TO BE RUN PARALLELY GOES HERE

driver = webdriver.Remote(
command_executor='http://<browserstack_UserName>:<browserstack_AccessKeys>@hub.browserstack.com/wd/hub',
desired_capabilities=caps)
command_executor='https://%s:%[email protected]/wd/hub' % (
USERNAME, BROWSERSTACK_ACCESS_KEY
),
desired_capabilities=caps)

driver.get("http://www.google.com")
inputElement = driver.find_element_by_name("q")
inputElement.send_keys("browserstack")
inputElement.submit()
print driver.title
driver.quit()
#------------------------------------------------------#
#------------------------------------------------------#
12 changes: 12 additions & 0 deletions py.test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# A sample to run py.test over BrowserStack Automate.

## Setup

`pip install pytest`

## Execution
To run the tests, set correct environment variables or add you credentials to `google-search-browserstack.py` and execute:

```
py.test google-search-browserstack.py
```
7 changes: 0 additions & 7 deletions py.test/README.mkd

This file was deleted.

37 changes: 27 additions & 10 deletions py.test/google-search-browserstack.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
import os
import sys

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Edit these to match your credentials
USERNAME = None
BROWSERSTACK_ACCESS_KEY = None
USERNAME = os.environ.get('BROWSERSTACK_USERNAME') or sys.argv[1]
BROWSERSTACK_ACCESS_KEY = os.environ.get(
'BROWSERSTACK_ACCESS_KEY') or sys.argv[2]

if not (USERNAME and BROWSERSTACK_ACCESS_KEY):
raise Exception("Please provide your BrowserStack username and access key")
sys.exit(1)


def test_run():
url = "http://%s:%[email protected]/wd/hub" %(
url = "https://%s:%[email protected]/wd/hub" % (
USERNAME, BROWSERSTACK_ACCESS_KEY
)

driver = webdriver.Remote(command_executor=url, desired_capabilities=DesiredCapabilities.FIREFOX)
capabilities = {
'browserName': 'Firefox',
'browserVersion': '65.0',
'browserstack.use_w3c': 'true',
'bstack:options': {
'os': 'Windows',
'buildName': 'automate-python-samples',
'osVersion': '10',
'sessionName': 'unittest_single_test',
'projectName': 'Sample project',
'debug': 'true'
}
}
driver = webdriver.Remote(command_executor=url,
desired_capabilities=capabilities)
driver.get('http://www.google.com')

if not "Google" in driver.title:
raise Exception("Are you not on google? How come!")
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.submit()
driver.quit()
elem = driver.find_element_by_name("q")
elem.send_keys("selenium")
elem.submit()
driver.quit()
Loading