from threading import Thread
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# This array 'caps' defines the capabilities browser, device and OS combinations where the test will run
caps=[{
      'os_version': '10',
      'os': 'Windows',
      'browser': 'ie',
      'browser_version': '11.0',
      'name': 'Parallel Test1', # test name
      'build': 'browserstack-build-1' # Your tests will be organized within this build
      },
      {
      'os_version': 'Big Sur',
      'os': 'OS X',
      'browser': 'chrome',
      'browser_version': '95.0',
      'name': 'Parallel Test2',
      'build': 'browserstack-build-1'
      },
      {
      'os_version': 'Big Sur',
      'os': 'OS X',
      'browser': 'firefox',
      'browser_version': '93.0',
      'name': 'Parallel Test3',
      'build': 'browserstack-build-1'
}]	 
#run_session function searches for 'BrowserStack' on google.com
def run_session(desired_cap):
  driver = webdriver.Remote(
      command_executor='https://jessicasadler_RbBTVv:xE8t7EaT7QqcLDMfzfvz@hub-cloud.browserstack.com/wd/hub',
      desired_capabilities=desired_cap)
  driver.get("https://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("BrowserStack")
  elem.submit()
  try:
      WebDriverWait(driver, 5).until(EC.title_contains("BrowserStack"))
      driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Title matched!"}}')
  except TimeoutException:
      driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Title not matched"}}')
  print(driver.title)
  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 parallelly
for cap in caps:
  Thread(target=run_session, args=(cap,)).start()
[(36, 'selenium.webdriver.Remote', 'webdriver.Remote', False, 'from selenium import webdriver\n'), (46, 'selenium.webdriver.support.expected_conditions.title_contains', 'EC.title_contains', True, 'from selenium.webdriver.support import expected_conditions as EC\n'), (54, 'threading.Thread', 'Thread', False, 'from threading import Thread\n'), (46, 'selenium.webdriver.support.ui.WebDriverWait', 'WebDriverWait', False, 'from selenium.webdriver.support.ui import WebDriverWait\n')]