From 8c1bb8eafebd72121535a0fa20a13ecba77ce6fa Mon Sep 17 00:00:00 2001 From: Varun Garg Date: Fri, 1 Mar 2019 17:15:51 +0530 Subject: [PATCH 1/4] Selenium 4 changes Signed-off-by: Varun Garg --- google-search-browserstack.py | 19 +++++++- nose-test/google-search-browserstack.py | 22 +++++++-- parallel_tests/README.md | 19 +------- parallel_tests/browsers.json | 61 +++++++++++++------------ parallel_tests/test.py | 21 +++------ py.test/google-search-browserstack.py | 31 +++++++++---- unittest/google-search-browserstack.py | 20 ++++++-- 7 files changed, 115 insertions(+), 78 deletions(-) diff --git a/google-search-browserstack.py b/google-search-browserstack.py index 005cb5a..738451b 100644 --- a/google-search-browserstack.py +++ b/google-search-browserstack.py @@ -7,16 +7,31 @@ try: USERNAME = os.environ.get('BROWSERSTACK_USERNAME') or sys.argv[1] - BROWSERSTACK_ACCESS_KEY = os.environ.get('BROWSERSTACK_ACCESS_KEY') or sys.argv[2] + 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' % ( USERNAME, BROWSERSTACK_ACCESS_KEY ), - desired_capabilities=DesiredCapabilities.FIREFOX + desired_capabilities = capabilities ) driver.get("http://www.google.com") diff --git a/nose-test/google-search-browserstack.py b/nose-test/google-search-browserstack.py index b82b4ae..1c7e9ab 100644 --- a/nose-test/google-search-browserstack.py +++ b/nose-test/google-search-browserstack.py @@ -8,21 +8,35 @@ # Edit these to match your credentials USERNAME = os.environ.get('BROWSERSTACK_USERNAME') or sys.argv[3] -BROWSERSTACK_ACCESS_KEY = os.environ.get('BROWSERSTACK_ACCESS_KEY') or sys.argv[4] +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") + class PythonOrgSearch(unittest.TestCase): def setUp(self): - url = "http://%s:%s@hub.browserstack.com/wd/hub" %( + url = "http://%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 04260ba..22ac72b 100644 --- a/parallel_tests/README.md +++ b/parallel_tests/README.md @@ -4,24 +4,9 @@ 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" - } - ] +`python test.py ` - This file contains the selenium test which would be run in each of the browsers specificed by "browsers.json". - -`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 fa7708a..b93db97 100644 --- a/parallel_tests/test.py +++ b/parallel_tests/test.py @@ -1,26 +1,19 @@ from selenium import webdriver -import os, 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] +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 @@ -36,4 +29,4 @@ inputElement.submit() print driver.title driver.quit() -#------------------------------------------------------# \ No newline at end of file +#------------------------------------------------------# diff --git a/py.test/google-search-browserstack.py b/py.test/google-search-browserstack.py index 0a93724..e51a4a4 100644 --- a/py.test/google-search-browserstack.py +++ b/py.test/google-search-browserstack.py @@ -6,22 +6,37 @@ # Edit these to match your credentials USERNAME = os.environ.get('BROWSERSTACK_USERNAME') or sys.argv[1] -BROWSERSTACK_ACCESS_KEY = os.environ.get('BROWSERSTACK_ACCESS_KEY') or sys.argv[2] +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") + def test_run(): - url = "http://%s:%s@hub.browserstack.com/wd/hub" %( + url = "http://%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 8695749..bf6fc07 100644 --- a/unittest/google-search-browserstack.py +++ b/unittest/google-search-browserstack.py @@ -19,10 +19,22 @@ def setUp(self): url = "http://%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): @@ -33,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]) From e799b12506c8448cee2069ada5eb6aabfa467216 Mon Sep 17 00:00:00 2001 From: francisf Date: Tue, 5 Mar 2019 11:57:34 +0530 Subject: [PATCH 2/4] Apply suggestions from code review Co-Authored-By: Varun-garg --- .vscode/settings.json | 2 +- google-search-browserstack.py | 2 +- nose-test/google-search-browserstack.py | 2 +- parallel_tests/test.py | 2 +- py.test/google-search-browserstack.py | 2 +- unittest/google-search-browserstack.py | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 4e1520b..67c7ba9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,4 @@ { "python.linting.pylintEnabled": true, "python.pythonPath": "/usr/local/bin/python" -} \ No newline at end of file +} diff --git a/google-search-browserstack.py b/google-search-browserstack.py index 738451b..6801532 100644 --- a/google-search-browserstack.py +++ b/google-search-browserstack.py @@ -28,7 +28,7 @@ } 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 = capabilities diff --git a/nose-test/google-search-browserstack.py b/nose-test/google-search-browserstack.py index 1c7e9ab..3e99dd7 100644 --- a/nose-test/google-search-browserstack.py +++ b/nose-test/google-search-browserstack.py @@ -18,7 +18,7 @@ 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 = { diff --git a/parallel_tests/test.py b/parallel_tests/test.py index b93db97..765101a 100644 --- a/parallel_tests/test.py +++ b/parallel_tests/test.py @@ -18,7 +18,7 @@ # THE TEST TO BE RUN PARALLELY GOES HERE 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=caps) diff --git a/py.test/google-search-browserstack.py b/py.test/google-search-browserstack.py index e51a4a4..2e18a64 100644 --- a/py.test/google-search-browserstack.py +++ b/py.test/google-search-browserstack.py @@ -14,7 +14,7 @@ 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 ) capabilities = { diff --git a/unittest/google-search-browserstack.py b/unittest/google-search-browserstack.py index bf6fc07..8f3ec80 100644 --- a/unittest/google-search-browserstack.py +++ b/unittest/google-search-browserstack.py @@ -16,7 +16,7 @@ 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 = { From 390d7f9712e7cd6c5a8756cf71f7eef2e09701d3 Mon Sep 17 00:00:00 2001 From: francisf Date: Mon, 1 Jul 2019 14:24:14 +0530 Subject: [PATCH 3/4] spelling typo - please --- google-search-browserstack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/google-search-browserstack.py b/google-search-browserstack.py index 6801532..a4c26fc 100644 --- a/google-search-browserstack.py +++ b/google-search-browserstack.py @@ -10,7 +10,7 @@ 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.") + print("Please provide the username and browserstack access key as command line arguments.") sys.exit(1) capabilities = { From 2dc156056a5daa21c08691b434f2771e89de96cd Mon Sep 17 00:00:00 2001 From: Yash Ladha Date: Mon, 1 Jul 2019 15:10:28 +0530 Subject: [PATCH 4/4] Ported to python3 for selenium --- README.md | 4 ++-- google-search-browserstack.py | 4 ++-- parallel_tests/README.md | 4 +++- parallel_tests/run_parallel_tests.py | 10 +++++++--- parallel_tests/test.py | 15 +++++++++++---- screenshot-sample.py | 16 +++++++++------- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 1b32a07..39a1228 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,12 @@ To test varios sample repositories with ease, it is recommended to setup `BROWS ### For Windows: - - Download the latest python build for windows - http://sourceforge.net/projects/pywin32/files/pywin32/ + - Download the latest python build for windows - https://www.python.org/downloads/windows/ - Run the installer exe and follow the instructions to install python. ### For Mac and Linux: - - Run python --version to see what python version is installed and make sure it is 2.5.X and above. + - Run python --version to see what python version is installed and make sure it is 3.X and above. - Mac OS, Ubuntu and many flavors of linux come with pre-installed python. ## Install Selenium diff --git a/google-search-browserstack.py b/google-search-browserstack.py index 6801532..db9abc5 100644 --- a/google-search-browserstack.py +++ b/google-search-browserstack.py @@ -31,7 +31,7 @@ command_executor='https://%s:%s@hub.browserstack.com/wd/hub' % ( USERNAME, BROWSERSTACK_ACCESS_KEY ), - desired_capabilities = capabilities + desired_capabilities=capabilities ) driver.get("http://www.google.com") @@ -42,5 +42,5 @@ elem.send_keys("selenium") elem.submit() -print driver.title +print(driver.title) driver.quit() diff --git a/parallel_tests/README.md b/parallel_tests/README.md index 22ac72b..302bec2 100644 --- a/parallel_tests/README.md +++ b/parallel_tests/README.md @@ -10,4 +10,6 @@ This project contains 3 files. Each of the files is described below. To run the tests in parallel execute the following command: - python run_parallel_tests.py test.py browsers.json +```sh + python3 run_parallel_tests.py test.py browsers.json +``` diff --git a/parallel_tests/run_parallel_tests.py b/parallel_tests/run_parallel_tests.py index 8492d00..36a0616 100644 --- a/parallel_tests/run_parallel_tests.py +++ b/parallel_tests/run_parallel_tests.py @@ -1,7 +1,11 @@ import json, sys, subprocess -file_name = sys.argv[1] -json_name = sys.argv[2] +try: + file_name = sys.argv[1] + json_name = sys.argv[2] +except IndexError: + print("Please provide test script and browserconfig as first and second argument, respectively from command line") + sys.exit(1) with open(json_name, "r") as f: obj = json.loads(f.read()) @@ -9,7 +13,7 @@ num_of_tests = len(obj) process = [] for counter in range(num_of_tests): - cmd = "python "+str(file_name)+ " " +str(json_name)+ " " +str(counter) + cmd = "python3 %s %s %s" % (file_name, json_name, counter) process.append(subprocess.Popen(cmd, shell=True)) for counter in range(num_of_tests): diff --git a/parallel_tests/test.py b/parallel_tests/test.py index 765101a..4d91759 100644 --- a/parallel_tests/test.py +++ b/parallel_tests/test.py @@ -3,7 +3,13 @@ import sys import json -json_name = sys.argv[1] +try: + json_name = sys.argv[1] + counter_val = sys.argv[2] +except IndexError: + print('Json name and counter val must be passed as first and second argument, respectively, from the comamnd line') + sys.exit(1) + USERNAME = os.environ.get('BROWSERSTACK_USERNAME') or sys.argv[2] BROWSERSTACK_ACCESS_KEY = os.environ.get( 'BROWSERSTACK_ACCESS_KEY') or sys.argv[3] @@ -11,8 +17,8 @@ with open(json_name, "r") as f: obj = json.loads(f.read()) -caps = obj[int(sys.argv[2])] -print "Test "+sys.argv[2]+" started" +caps = obj[int(counter_val)] +print("Test %s started" % (counter_val)) #------------------------------------------------------# # THE TEST TO BE RUN PARALLELY GOES HERE @@ -21,7 +27,8 @@ command_executor='https://%s:%s@hub.browserstack.com/wd/hub' % ( USERNAME, BROWSERSTACK_ACCESS_KEY ), - desired_capabilities=caps) + desired_capabilities=caps +) driver.get("http://www.google.com") inputElement = driver.find_element_by_name("q") diff --git a/screenshot-sample.py b/screenshot-sample.py index 2cbd1d1..5860214 100644 --- a/screenshot-sample.py +++ b/screenshot-sample.py @@ -23,12 +23,13 @@ try: USERNAME = sys.argv[1] BROWSERSTACK_ACCESS_KEY = sys.argv[2] + FILENAME = sys.argv[3] except IndexError: print("Pleaes provide the username, browserstack access key and filename with which screenshot should be saved as command line arguments.") sys.exit(1) # Define take_screenshot -def take_screenshot(webdriver, file_name = "sample.png"): +def take_screenshot(webdriver, file_name="sample.png"): """ @param webdriver: WebDriver handler. @type webdriver: WebDriver @@ -37,21 +38,22 @@ def take_screenshot(webdriver, file_name = "sample.png"): """ if isinstance(webdriver, WebDriver): base64_data = webdriver.get_screenshot_as_base64() - screenshot_data = base64.decodestring(base64_data) - screenshot_file = open(file_name, "w") + screenshot_data = base64.b64decode(base64_data) + screenshot_file = open(file_name, "wb") screenshot_file.write(screenshot_data) screenshot_file.close() else: webdriver.save_screenshot(filename) driver = webdriver.Remote( - command_executor = 'http://akshaybhardwaj1:XQWDewaJsUzqYJRv8zhr@hub.browserstack.com/wd/hub', - desired_capabilities = caps) + command_executor="http://%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") inputElement.send_keys("browserstack") inputElement.submit() -print driver.title -take_screenshot(driver, './sample.png') +print(driver.title) +take_screenshot(driver, FILENAME) driver.quit()