diff --git a/.browserstack b/.browserstack deleted file mode 100644 index 2eb6b4b..0000000 --- a/.browserstack +++ /dev/null @@ -1,3 +0,0 @@ -[credentials] -username = BROWSERSTACK_USERNAME -key = BROWSERSTACK_ACCESS_KEY diff --git a/README.md b/README.md index 3c1c7d1..59795ee 100644 --- a/README.md +++ b/README.md @@ -3,22 +3,22 @@ PyTest Integration with BrowserStack. ![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780) +## Prerequisite +* Python3 ## Setup * Clone the repo * Install dependencies `pip install -r requirements.txt` -* To run your automated tests using BrowserStack, you must provide a valid username and access key. This can be done either by using a .browserstack configuration file in the working directory or your home directory, or by setting the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables. +* To run your automated tests using BrowserStack, you must provide a valid username and access key. This can be done either by using a .browserstack configuration file in the working directory or your home directory, by setting the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables, or by adding user & key to config file. -## Running your single tests -* To run single tests, run `paver run single` -## Running your local tests -* To run a local test, (if you have not set the BROWSERSTACK_ACCESS_KEY environment variable) first go to conftest.py then edit access_key on line 10 +## Run tests on locally hosted websites +* To run a local test, (if you have not set the BROWSERSTACK_ACCESS_KEY environment variable) first go to config/local.json then edit key on line 3 * Run `paver run local` -## Running your parallel tests -* To run parallel tests, run `paver run parallel` +## Run sample tests +* To run parallel tests, run `paver run sample` Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github) diff --git a/config/local.json b/config/local.json index cad383c..da87635 100644 --- a/config/local.json +++ b/config/local.json @@ -1,23 +1,20 @@ { - "capabilities": { - "bstack:options": { - "projectName": "Pytest Browserstack", - "buildName": "pytest-browserstack", - "sessionName": "local_test", - "local": true, - "debug" : "true", - "networkLogs": "true", - "seleniumVersion": "3.14.0" - } - }, - "environments": [ - { - "bstack:options": { - "os": "Windows", - "osVersion": "11" - }, - "browserName": "chrome", - "browserVersion": "latest" - } - ] + "user": "BROWSERSTACK_USERNAME", + "key": "BROWSERSTACK_ACCESS_KEY", + "capabilities": { + "project": "Pytest Browserstack", + "build": "browserstack-build-1", + "name": "BStack local pytest", + "browserstack.local": true, + "browserstack.debug": "true", + "browserstack.networkLogs": "true" + }, + "environments": [ + { + "os": "Windows", + "os_version": "11", + "browser": "Chrome", + "browser_version": "latest" + } + ] } diff --git a/config/parallel.json b/config/parallel.json deleted file mode 100644 index a2ad611..0000000 --- a/config/parallel.json +++ /dev/null @@ -1,39 +0,0 @@ -{ - "capabilities": { - "bstack:options": { - "projectName": "Pytest Browserstack", - "buildName": "pytest-browserstack", - "sessionName": "parallel_test", - "local": false, - "debug" : "true", - "networkLogs": "true", - "seleniumVersion": "3.14.0" - } - }, - "environments": [ - { - "bstack:options": { - "os": "Windows", - "osVersion": "11" - }, - "browser": "chrome", - "browserVersion": "latest" - }, - { - "bstack:options": { - "os" : "OS X", - "osVersion" : "Monterey" - }, - "browser": "firefox", - "browserVersion": "latest" - }, - { - "bstack:options": { - "osVersion" : "10.0", - "deviceName" : "Samsung Galaxy S20" - }, - "browserName": "firefox", - "browserVersion": "latest" - } - ] -} diff --git a/config/sample.json b/config/sample.json new file mode 100644 index 0000000..4c89764 --- /dev/null +++ b/config/sample.json @@ -0,0 +1,30 @@ +{ + "user":"BROWSERSTACK_USERNAME", + "key":"BROWSERSTACK_ACCESS_KEY", + "capabilities": { + "project": "Pytest Browserstack", + "build": "browserstack-build-1", + "name": "BStack sample pytest", + "browserstack.local": false, + "browserstack.debug": "true", + "browserstack.networkLogs": "true" + }, + "environments": [ + { + "os": "Windows", + "os_version": "11", + "browser": "chrome", + "browser_version": "latest" + }, + { + "os": "OS X", + "os_version": "Monterey", + "browser": "firefox", + "browser_version": "latest" + }, + { + "os_version": "10.0", + "device": "Samsung Galaxy S20" + } + ] +} diff --git a/config/single.json b/config/single.json deleted file mode 100644 index 0a9bc48..0000000 --- a/config/single.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "capabilities": { - "bstack:options": { - "projectName": "Pytest Browserstack", - "buildName": "pytest-browserstack", - "sessionName": "single_test", - "local": false, - "debug" : "true", - "networkLogs": "true", - "seleniumVersion": "3.14.0" - } - }, - "environments": [ - { - "bstack:options": { - "os": "Windows", - "osVersion": "11" - }, - "browserName": "chrome", - "browserVersion": "latest" - } - ] -} diff --git a/conftest.py b/conftest.py index d22343e..c59b077 100644 --- a/conftest.py +++ b/conftest.py @@ -1,4 +1,3 @@ -from pprint import pprint import pytest from browserstack.local import Local import os, json @@ -11,7 +10,8 @@ bs_local = None -BROWSERSTACK_ACCESS_KEY = os.environ['BROWSERSTACK_ACCESS_KEY'] if 'BROWSERSTACK_ACCESS_KEY' in os.environ else None +BROWSERSTACK_USERNAME = os.environ['BROWSERSTACK_USERNAME'] if 'BROWSERSTACK_USERNAME' in os.environ else CONFIG["user"] +BROWSERSTACK_ACCESS_KEY = os.environ['BROWSERSTACK_ACCESS_KEY'] if 'BROWSERSTACK_ACCESS_KEY' in os.environ else CONFIG["key"] def start_local(): """Code to start browserstack local before start of test.""" @@ -28,10 +28,13 @@ def stop_local(): @pytest.fixture(scope='session') def session_capabilities(): - desired_capabilities = merge(CONFIG['environments'][TASK_ID],CONFIG["capabilities"]) - if "local" in desired_capabilities['bstack:options'] and desired_capabilities['bstack:options']['local']: + capabilities = merge(CONFIG['environments'][TASK_ID],CONFIG["capabilities"]) + capabilities['browserstack.user'] = BROWSERSTACK_USERNAME + capabilities['browserstack.key'] = BROWSERSTACK_ACCESS_KEY + capabilities['browserstack.source'] = 'pytest:sample-selenium-3:v1.0' + if "browserstack.local" in capabilities and capabilities['browserstack.local']: start_local() - return desired_capabilities + return capabilities def pytest_sessionfinish(session, exitstatus): diff --git a/pavement.py b/pavement.py index 472bf96..3accb7c 100644 --- a/pavement.py +++ b/pavement.py @@ -35,10 +35,3 @@ def run(args): p = Process(target=run_py_test, args=(args[0], i)) jobs.append(p) p.start() - -@task -def test(): - """Run all tests""" - sh("paver run single") - sh("paver run local") - sh("paver run parallel") diff --git a/requirements.txt b/requirements.txt index 408bf8b..64cf362 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,7 +2,7 @@ browserstack-local jsonmerge multiprocess paver -selenium +selenium==3.141.0 psutil pytest pytest-variables diff --git a/tests/test_parallel.py b/tests/test_sample.py similarity index 100% rename from tests/test_parallel.py rename to tests/test_sample.py diff --git a/tests/test_single.py b/tests/test_single.py deleted file mode 100644 index 7ba9692..0000000 --- a/tests/test_single.py +++ /dev/null @@ -1,25 +0,0 @@ -from browserstack.local import Local -from selenium import webdriver -import pytest -from selenium.webdriver.common.by import By - -def test_example(selenium): - selenium.get('https://bstackdemo.com/') - - # locating product on webpage and getting name of the product - productText = selenium.find_element(By.XPATH, '//*[@id="1"]/p').text - - # clicking the 'Add to cart' button - selenium.find_element(By.XPATH, '//*[@id="1"]/div[4]').click() - - # waiting until the Cart pane has been displayed on the webpage - selenium.find_element(By.CLASS_NAME, 'float-cart__content') - - # locating product in cart and getting name of the product in cart - productCartText = selenium.find_element(By.XPATH, '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]').text - - # checking whether product has been added to cart by comparing product name and marking test pass or fail - if productText == productCartText: - selenium.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Test Passed Successfully"}}') - else: - selenium.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Product added to the cart not same as selected"}}')