diff --git a/README.md b/README.md index 66a8624..2074fe1 100644 --- a/README.md +++ b/README.md @@ -17,32 +17,14 @@ BROWSERSTACK_USERNAME="BROWSERSTACK_USERNAME" BROWSERSTACK_ACCESS_KEY="BROWSERSTACK_ACCESS_KEY" URL="https://hub.browserstack.com/wd/hub" ``` -- Change the capabilities if you wish: -(For single test session, Navigate to ./scripts/single.py) -```python -desired_cap = { - ... - 'browserName': 'iPhone', - 'device': 'iPhone 11', - 'realMobile': 'true', - 'os_version': '14.0', - 'name': 'BStack-[Python] Sample Test', # test name - 'build': 'BStack Build Number 1' # CI/CD job or build name - ... -} -``` - Run tests - a. For single + a. For parallel ``` - python3 ./scripts/single.py + python3 ./scripts/parallel.py ``` b. For local ``` python3 ./scripts/local.py ``` - c. For parallel - ``` - python3 ./scripts/parallel.py - ``` diff --git a/requirements.txt b/requirements.txt index 1580e48..d526cbc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ python-dotenv browserstack-local >= 1.2.3 -selenium == 3.4.3 +selenium == 3.141.0 diff --git a/scripts/local.py b/scripts/local.py index 0840e27..83ba200 100644 --- a/scripts/local.py +++ b/scripts/local.py @@ -25,31 +25,40 @@ print(bs_local.isRunning()) desired_cap = { - 'browserName': 'iPhone', - 'device': 'iPhone 11', - 'realMobile': 'true', - 'os_version': '14.0', - 'name': 'BStack-[Python] Sample Test', # test name - 'build': 'BStack Build Number 1', # CI/CD job or build name - 'browserstack.local': 'true', - 'browserstack.user': BROWSERSTACK_USERNAME, - 'browserstack.key': BROWSERSTACK_ACCESS_KEY + 'os': 'OS X', + 'os_version': 'Monterey', + 'browser': 'chrome', + 'browser_version': 'latest', + 'buildName': 'browserstack-build-1', + 'sessionName': 'BStack [python] Local', + 'browserstack.local': 'true', + 'browserstack.user': BROWSERSTACK_USERNAME, + 'browserstack.key': BROWSERSTACK_ACCESS_KEY, } +desired_cap['browserstack.source'] = 'python:sample-selenium-3:v1.0' driver = webdriver.Remote( command_executor=URL, - desired_capabilities=desired_cap) + desired_capabilities=desired_cap, + keep_alive=True) try: driver.get("http://bs-local.com:45691/check") - body_text = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'body'))).text + body_text = WebDriverWait(driver, 10).until( + EC.visibility_of_element_located((By.CSS_SELECTOR, 'body'))).text # Verify whether the product (iPhone 12) is added to cart if body_text == "Up and running": # Set the status of test as 'passed' or 'failed' based on the condition; if item is added to cart - driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Local Test ran successfully"}}') + driver.execute_script( + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Local Test ran successfully"}}') except NoSuchElementException: - driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Local test setup failed"}}') + driver.execute_script( + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Local test setup failed"}}') except Exception: - driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some exception occurred"}}') + driver.execute_script( + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some exception occurred"}}') + # Stop the driver driver.quit() + +# stop local binary bs_local.stop() diff --git a/scripts/parallel.py b/scripts/parallel.py index b325b51..dab151f 100644 --- a/scripts/parallel.py +++ b/scripts/parallel.py @@ -13,80 +13,78 @@ load_dotenv() BROWSERSTACK_USERNAME = os.environ.get("BROWSERSTACK_USERNAME") or "BROWSERSTACK_USERNAME" BROWSERSTACK_ACCESS_KEY = os.environ.get("BROWSERSTACK_ACCESS_KEY") or "BROWSERSTACK_ACCESS_KEY" -URL = "https://" + BROWSERSTACK_USERNAME + ":" + BROWSERSTACK_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub" +URL = "https://" + BROWSERSTACK_USERNAME + ":" + BROWSERSTACK_ACCESS_KEY + "@hub.browserstack.com/wd/hub" # This array 'caps' defines the capabilities browser, device and OS combinations where the test will run -caps=[{ - 'os_version': '10', - 'os': 'Windows', - 'browser': 'chrome', - 'browser_version': 'latest', - 'name': 'Parallel Test1', # test name - 'build': 'BStack-[Python] Sample Build' # Your tests will be organized within this build - }, - { - 'os_version': '10', - 'os': 'Windows', - 'browser': 'Edge', - 'browser_version': 'latest', - 'name': 'Parallel Test2', # test name - 'build': 'BStack-[Python] Sample Build' - }, - { - 'os_version': 'Big Sur', - 'os': 'OS X', - 'browser': 'Safari', - 'browser_version': 'latest', - 'name': 'Parallel Test3', # test name - 'build': 'BStack-[Python] Sample Build' - }, - { - 'device': 'Samsung Galaxy S20', - 'os_browser': '11.0', - 'real_mobile': 'true', - 'name': 'Parallel Test4', - 'build': 'BStack-[Python] Sample Build' - }, - { - 'device': 'iPhone 12 Pro', - 'os_browser': '14', - 'real_mobile': 'true', - 'name': 'Parallel Test5', - 'build': 'BStack-[Python] Sample Build' -}] -#run_session function adds a product in cart bstackdemo.com +caps = [ + { + 'os_version': '10', + 'os': 'Windows', + 'browser': 'firefox', + 'browser_version': 'latest', + 'name': 'BStack [python] parallel 1', # test name + 'build': 'browserstack-build-1', + }, + { + 'os_version': 'Big Sur', + 'os': 'OS X', + 'browser': 'chrome', + 'browser_version': 'latest', + 'name': 'BStack [python] parallel 2', + 'build': 'browserstack-build-1', + }, + { + 'device': 'Samsung Galaxy S20', + 'os_browser': '11.0', + 'name': 'BStack [python] parallel 3', + 'build': 'browserstack-build-1', + } +] + +# run_session function adds a product in cart bstackdemo.com + def run_session(desired_cap): - driver = webdriver.Remote( - command_executor=URL, - desired_capabilities=desired_cap) - try: - driver.get("https://bstackdemo.com/") - WebDriverWait(driver, 5).until(EC.title_contains("StackDemo")) + desired_cap['browserstack.source'] = 'python:sample-selenium-3:v1.0' + driver = webdriver.Remote( + command_executor=URL, + desired_capabilities=desired_cap, + keep_alive=True) + try: + driver.get("https://bstackdemo.com/") + WebDriverWait(driver, 5).until(EC.title_contains("StackDemo")) + + # Get text of an product - iPhone 12 + item_on_page = WebDriverWait(driver, 10).until( + EC.visibility_of_element_located((By.XPATH, '//*[@id="1"]/p'))).text + + # Check if "Add to cart" button is present + WebDriverWait(driver, 10).until(EC.visibility_of_element_located( + (By.XPATH, '//*[@id="1"]/div[4]'))).click() - # Get text of an product - iPhone 12 - item_on_page = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="1"]/p'))).text + # Check if the Cart pane is visible + WebDriverWait(driver, 30).until(EC.visibility_of_element_located( + (By.CLASS_NAME, "float-cart__content"))) - # Check if "Add to cart" button is present - WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="1"]/div[4]'))).click() + # Get text of product in cart + item_in_cart = WebDriverWait(driver, 10).until(EC.visibility_of_element_located( + (By.XPATH, '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]'))).text - # Check if the Cart pane is visible - WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.CLASS_NAME, "float-cart__content"))) + # Verify whether the product (iPhone 12) is added to cart + if item_on_page == item_in_cart: + # Set the status of test as 'passed' or 'failed' based on the condition; if item is added to cart + driver.execute_script( + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "iPhone 12 has been successfully added to the cart!"}}') + except NoSuchElementException: + driver.execute_script( + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some elements failed to load"}}') + except Exception: + driver.execute_script( + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some exception occurred"}}') - ## Get text of product in cart - item_in_cart = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]'))).text + # Stop the driver + driver.quit() - # Verify whether the product (iPhone 12) is added to cart - if item_on_page == item_in_cart: - # Set the status of test as 'passed' or 'failed' based on the condition; if item is added to cart - driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "iPhone 12 has been successfully added to the cart!"}}') - except NoSuchElementException: - driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some elements failed to load"}}') - except Exception: - driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some exception occurred"}}') - - # Stop the driver - driver.quit() -#The Thread function takes run_session function and each set of capability from the caps array as an argument to run each session in parallel +# The Thread function takes run_session function and each set of capability from the caps array as an argument to run each session in parallel for cap in caps: - Thread(target=run_session, args=(cap,)).start() + Thread(target=run_session, args=(cap,)).start() diff --git a/scripts/single.py b/scripts/single.py index 2f2aacf..6d15d67 100644 --- a/scripts/single.py +++ b/scripts/single.py @@ -12,37 +12,46 @@ URL = os.environ.get("URL") or "https://hub.browserstack.com/wd/hub" desired_cap = { - 'browserName': 'iPhone', - 'device': 'iPhone 11', - 'realMobile': 'true', - 'os_version': '14.0', - 'name': 'BStack-[Python] Sample Test', # test name - 'build': 'BStack Build Number 1', # CI/CD job or build name - 'browserstack.user': BROWSERSTACK_USERNAME, - 'browserstack.key': BROWSERSTACK_ACCESS_KEY + 'os' : 'OS X', + 'os_version' : 'Monterey', + 'browser': 'chrome', + 'browser_version': 'latest', + 'buildName' : 'browserstack-build-1', + 'sessionName' : 'BStack [python] Sample', + 'browserstack.user': BROWSERSTACK_USERNAME, + 'browserstack.key': BROWSERSTACK_ACCESS_KEY, } +desired_cap['browserstack.source'] = 'python:sample-selenium-3:v1.0' driver = webdriver.Remote( command_executor=URL, - desired_capabilities=desired_cap) + desired_capabilities=desired_cap, + keep_alive=True) try: driver.get("https://bstackdemo.com/") WebDriverWait(driver, 10).until(EC.title_contains("StackDemo")) # Get text of an product - iPhone 12 - item_on_page = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="1"]/p'))).text + item_on_page = WebDriverWait(driver, 10).until( + EC.visibility_of_element_located((By.XPATH, '//*[@id="1"]/p'))).text # Click the 'Add to cart' button if it is visible - WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="1"]/div[4]'))).click() + WebDriverWait(driver, 10).until(EC.visibility_of_element_located( + (By.XPATH, '//*[@id="1"]/div[4]'))).click() # Check if the Cart pane is visible - WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CLASS_NAME, "float-cart__content"))) - ## Get text of product in cart - item_in_cart = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]'))).text + WebDriverWait(driver, 10).until(EC.visibility_of_element_located( + (By.CLASS_NAME, "float-cart__content"))) + # Get text of product in cart + item_in_cart = WebDriverWait(driver, 10).until(EC.visibility_of_element_located( + (By.XPATH, '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]'))).text # Verify whether the product (iPhone 12) is added to cart if item_on_page == item_in_cart: # Set the status of test as 'passed' or 'failed' based on the condition; if item is added to cart - driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "iPhone 12 has been successfully added to the cart!"}}') + driver.execute_script( + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "iPhone 12 has been successfully added to the cart!"}}') except NoSuchElementException: - driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some elements failed to load"}}') + driver.execute_script( + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some elements failed to load"}}') except Exception: - driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some exception occurred"}}') + driver.execute_script( + 'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some exception occurred"}}') # Stop the driver driver.quit()