Skip to content

fix: update parallel combinations, formatting #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ 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

Expand Down
36 changes: 22 additions & 14 deletions scripts/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,39 @@
print(bs_local.isRunning())

desired_cap = {
'browserName': 'iPhone',
'device': 'iPhone 11',
'realMobile': 'true',
'os_version': '14.0',
'name': 'BStack local python', # test name
'build': 'browserstack-build-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'
desired_cap['browserstack.source'] = 'python:sample-selenium-3:v1.0'

driver = webdriver.Remote(
command_executor=URL,
desired_capabilities=desired_cap)
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()
132 changes: 64 additions & 68 deletions scripts/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,81 +13,77 @@
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': 'BStack parallel python', # test name
'build': 'browserstack-build-1' # Your tests will be organized within this build
},
{
'os_version': '10',
'os': 'Windows',
'browser': 'Edge',
'browser_version': 'latest',
'name': 'BStack parallel python',
'build': 'browserstack-build-1'
},
{
'os_version': 'Big Sur',
'os': 'OS X',
'browser': 'Safari',
'browser_version': 'latest',
'name': 'BStack parallel python',
'build': 'browserstack-build-1'
},
{
'device': 'Samsung Galaxy S20',
'os_browser': '11.0',
'real_mobile': 'true',
'name': 'BStack parallel python',
'build': 'browserstack-build-1'
},
{
'device': 'iPhone 12 Pro',
'os_browser': '14',
'real_mobile': 'true',
'name': 'BStack parallel python',
'build': 'browserstack-build-1'
}]
#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):
desired_cap['browserstack.source']= 'python:sample-selenium-3:v1.0'
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)
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()
42 changes: 25 additions & 17 deletions scripts/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,45 @@
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 single python', # test name
'build': 'browserstack-build-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'
desired_cap['browserstack.source'] = 'python:sample-selenium-3:v1.0'

driver = webdriver.Remote(
command_executor=URL,
desired_capabilities=desired_cap)
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()