TestProject Forum

Web_driver.quit() not working on browserStack

I am trying to setup my test so that the devices to be tested can be set in a json. However on browser stack the .quit() method is not working. Below is my code.

import json
from src.testproject.sdk.drivers import webdriver
from selenium.common.exceptions import NoSuchElementException
from customExceptions import MTNoVideoFound

error_devices = []


def test_mobile(url, waitTime, text):
    with open('mobile.json') as f:
        data = json.load(f)
    for device in data:
        run_test(device, url, waitTime, text)
    if len(error_devices) > 0:
        raise MTNoVideoFound(error_devices)


def run_test(device, url, waitTime, text):
    username = "my_uname"
    access_key = "my_key"
    desired_capabilities = {
        "platformName": device["platformName"],
        "browserName": device["browserName"],
        "device": device["device"],
        "realMobile": "true",
        "os_version": device["os_version"],
        "name": 'test activation show on {}'.format(device["device"]),
        "build": 'test activation shows MT',
        "nativeWebTap": True,
        'cloud:URL': "https://" + username + ":" + access_key + "@hub-cloud.browserstack.com/wd/hub",
    }
    web_driver = webdriver.Remote(desired_capabilities=desired_capabilities,
                                  token="my_token")
    web_driver.get(url)
    web_driver.implicitly_wait(waitTime)
    try:
        web_driver.execute_script('document.querySelector(\'[data-c="video"\').scrollIntoView()')
        web_driver.find_element_by_xpath("//span[contains(text(), '{}')]".format(text))
    except NoSuchElementException:
        print('could not find element')
    except:
        print('video not found')
        error_devices.append({'name': device["device"], 'os': device["os_version"], 'browser':
            device["browserName"]})
    finally:
        web_driver.quit()

Hi @zacharyedgell ,

Can you please elaborate a bit more on what exactly is not working?
Do you get any error? or it just doesn’t terminate the session?

Hi @artem.kuznetsov,
I am trying to run web test across several devices. The json contains details for each devices so I can tell browserstack which device to run. I dont get any errors the browserstack session just times out when the json contains more than one device. For example: If the json only contains details for a iPhone 11 the test will run fine and terminate the session after the test. If the json contains details for a iPhone 11 and a iPhone 12 the iPhone 11 will run the test like normal but will not terminate and will timeout then the iPhone 12 will run fine and terminate due to no devices being after it.

A example of the json is below:

    [
      {
        "os_version" : "14",
        "device" : "iPhone 12 Pro Max",
        "real_mobile" : "true",
        "browserstack.local" : "false",
        "browserName" : "iPhone",
        "platformName": "iOS"
      },
      {
        "os_version" : "14",
        "device" : "iPhone 11",
        "real_mobile" : "true",
        "browserstack.local" : "false",
        "browserName" : "iPhone",
        "platformName": "iOS"
      },
      {
        "os_version" : "12",
        "device" : "iPhone XS",
        "real_mobile" : "true",
        "browserstack.local" : "false",
        "browserName" : "iPhone",
        "platformName": "iOS"
      },
      {
        "os_version" : "12",
        "device" : "iPhone 8 Plus",
        "real_mobile" : "true",
        "browserstack.local" : "false",
        "browserName" : "iPhone",
        "platformName": "iOS"
      },
      {
        "os_version" : "10.0",
        "device" : "Samsung Galaxy S20",
        "real_mobile" : "true",
        "browserstack.local" : "false",
        "browserName" : "Android",
        "platformName": "Android"
      },
      {
        "os_version" : "10.0",
        "device" : "Samsung Galaxy S20 Ultra",
        "real_mobile" : "true",
        "browserstack.local" : "false",
        "browserName" : "Android",
        "platformName": "Android"
      },
      {
        "os_version" : "10.0",
        "device" : "Samsung Galaxy Note 20",
        "real_mobile" : "true",
        "browserstack.local" : "false",
        "browserName" : "Android",
        "platformName": "Android"
      },
      {
        "os_version" : "9.0",
        "device" : "Samsung Galaxy S10 Plus",
        "real_mobile" : "true",
        "browserstack.local" : "false",
        "browserName" : "Android",
        "platformName": "Android"
      },
      {
        "os_version" : "9.0",
        "device" : "Samsung Galaxy S9 Plus",
        "real_mobile" : "true",
        "browserstack.local" : "false",
        "browserName" : "Android",
        "platformName": "Android"
      },
      {
        "os_version" : "9.0",
        "device" : "Samsung Galaxy S8 Plus",
        "real_mobile" : "true",
        "browserstack.local" : "false",
        "browserName" : "Android",
        "platformName": "Android"
      }
    ]

Hello @zacharyedgell

We had an issue with the driver.quit() in a previous version of the openSDK,
may I ask what is the version of your openSDK?
If it isn’t the latest, I would recommend using the latest version: 0.65.3

To upgrade to the latest version, use:

pip install testproject-python-sdk --upgrade

Let me know if that solved the issue.