diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..67c7ba9 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "python.linting.pylintEnabled": true, + "python.pythonPath": "/usr/local/bin/python" +} diff --git a/README.md b/README.md index ae9d7a2..1b32a07 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/google-search-browserstack.py b/google-search-browserstack.py index 32be3c1..6801532 100644 --- a/google-search-browserstack.py +++ b/google-search-browserstack.py @@ -1,3 +1,4 @@ +import os import sys from selenium import webdriver @@ -5,17 +6,32 @@ 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:%s@hub.browserstack.com/wd/hub' %( + command_executor='https://%s:%s@hub.browserstack.com/wd/hub' % ( USERNAME, BROWSERSTACK_ACCESS_KEY ), - desired_capabilities=DesiredCapabilities.FIREFOX + desired_capabilities = capabilities ) driver.get("http://www.google.com") diff --git a/google-search-local.py b/google-search-local.py deleted file mode 100644 index a4b246f..0000000 --- a/google-search-local.py +++ /dev/null @@ -1,15 +0,0 @@ -from selenium import webdriver -from selenium.webdriver.common.keys import Keys -from selenium.webdriver.common.desired_capabilities import DesiredCapabilities - -driver = webdriver.Firefox() -driver.get("http://www.google.com") -if not "Google" in driver.title: - raise Exception("Unable to load google page!") - -elem = driver.find_element_by_name("q") -elem.send_keys("selenium") -elem.submit() - -print driver.title -driver.quit() diff --git a/nose-test/README.md b/nose-test/README.md new file mode 100644 index 0000000..4051e62 --- /dev/null +++ b/nose-test/README.md @@ -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 +``` diff --git a/nose-test/README.mkd b/nose-test/README.mkd deleted file mode 100644 index 9afdb02..0000000 --- a/nose-test/README.mkd +++ /dev/null @@ -1,7 +0,0 @@ -A sample to run nosetests over BrowserStack Automate. - -To run the tests, add you credentials to `google-search-browserstack.py` and execute: - -``` -nosetests google-search-browserstack.py -``` diff --git a/nose-test/google-search-browserstack.py b/nose-test/google-search-browserstack.py index 7bc615c..3e99dd7 100644 --- a/nose-test/google-search-browserstack.py +++ b/nose-test/google-search-browserstack.py @@ -1,3 +1,4 @@ +import os import sys import unittest @@ -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:%s@hub.browserstack.com/wd/hub" %( + url = "https://%s:%s@hub.browserstack.com/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): diff --git a/parallel_tests/README.md b/parallel_tests/README.md index c0bfad3..22ac72b 100644 --- a/parallel_tests/README.md +++ b/parallel_tests/README.md @@ -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://:@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 ` - 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 ` - This is the runner which runs the tests in parallel. To run the tests in parallel execute the following command: diff --git a/parallel_tests/browsers.json b/parallel_tests/browsers.json index d501620..2e4d7d4 100644 --- a/parallel_tests/browsers.json +++ b/parallel_tests/browsers.json @@ -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 + } } ] diff --git a/parallel_tests/test.py b/parallel_tests/test.py index 6aeaf03..765101a 100644 --- a/parallel_tests/test.py +++ b/parallel_tests/test.py @@ -1,30 +1,27 @@ 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://:@hub.browserstack.com/wd/hub', - desired_capabilities=caps) + command_executor='https://%s:%s@hub.browserstack.com/wd/hub' % ( + USERNAME, BROWSERSTACK_ACCESS_KEY + ), + desired_capabilities=caps) driver.get("http://www.google.com") inputElement = driver.find_element_by_name("q") @@ -32,4 +29,4 @@ inputElement.submit() print driver.title driver.quit() -#------------------------------------------------------# \ No newline at end of file +#------------------------------------------------------# diff --git a/py.test/README.md b/py.test/README.md new file mode 100644 index 0000000..23391ef --- /dev/null +++ b/py.test/README.md @@ -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 +``` diff --git a/py.test/README.mkd b/py.test/README.mkd deleted file mode 100644 index b85ed11..0000000 --- a/py.test/README.mkd +++ /dev/null @@ -1,7 +0,0 @@ -A sample to run py.test over BrowserStack Automate. - -To run the tests, add you credentials to `google-search-browserstack.py` and execute: - -``` -py.test google-search-browserstack.py -``` diff --git a/py.test/google-search-browserstack.py b/py.test/google-search-browserstack.py index 8fbc950..2e18a64 100644 --- a/py.test/google-search-browserstack.py +++ b/py.test/google-search-browserstack.py @@ -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:%s@hub.browserstack.com/wd/hub" %( + url = "https://%s:%s@hub.browserstack.com/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() diff --git a/unittest/google-search-browserstack.py b/unittest/google-search-browserstack.py index 18ddc30..8f3ec80 100644 --- a/unittest/google-search-browserstack.py +++ b/unittest/google-search-browserstack.py @@ -1,3 +1,4 @@ +import os import sys import unittest @@ -6,8 +7,8 @@ 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("Please provide the username and browserstack access key as command line arguments.") sys.exit(1) @@ -15,13 +16,25 @@ class PythonOrgSearch(unittest.TestCase): def setUp(self): - url = "http://%s:%s@hub.browserstack.com/wd/hub" %( + url = "https://%s:%s@hub.browserstack.com/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': 'unittest_single_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): @@ -32,8 +45,8 @@ def test_search_in_python_org(self): elem.submit() self.assertIn("Google", driver.title) - def tearDown(self): - self.driver.quit() + def tearDown(self): + self.driver.quit() if __name__ == "__main__": unittest.main(argv=sys.argv[:1])