Skip to main content
Experience faster, smarter testing with BrowserStack AI Agents. See what your workflow’s been missing. Explore now!
No Result Found
Get your setup working faster. Join our Discord for optimisation tips from elite testers. Join our DiscordJoin our Discord

Make Selenium Tests Run Faster

Learn how to speed up your slow-performing Selenium tests.

Issue

Tests running on BrowserStack Automate slows down with the error as shown in the following image:

keep-alive missing issue in Automate

Cause

The absence of the keep-alive parameter leads to slow-performing tests. When you don’t use this parameter, your client binding is using a new request every time you communicate with BrowserStack.

Why you must mitigate this issue

As an organization, optimizing your builds to run faster is crucial. BrowserStack understands the need to identify issues that might not be evident, but have a large impact on the speed and stability of your test sessions.

Use the resolutions provided to mitigate these issues so as to:

  • Improve performance
  • Reduce test flakiness
Warning: Not using the HTTP keep-alive connection degrades your test performance approximately 2-10 times.

Resolution/Workaround

Since keep-alive is an HTTP connection parameter, specifying it as a capability in the test script does not work. You need to specify this parameter at the Selenium Webdriver’s level so that the client binding uses the same connection for every request, which eventually speeds up your tests.

Use the following information to enable the keep-alive parameter in your test script:

In your test script, when initializing the driver, use the keep_alive parameter and set it to True, as shown in the following code snippet.:

driver = webdriver.Remote(
    command_executor='https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub',
    desired_capabilities=desired_cap, keep_alive=True)

Sample test script

Here is a sample test script that uses the keep_alive parameter to perform a test case:

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
desired_cap = {
 'browserName': 'iPhone',
 'device': 'iPhone 12 Pro Max',
 'realMobile': 'true',
 'os_version': '14',
 'name': 'BStack-[Python] Sample Test', # test name
 'build': 'BStack Sample Build' # CI/CD job or build name
}
driver = webdriver.Remote(
    command_executor='https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub',
    desired_capabilities=desired_cap, keep_alive=True)

driver.get("https://bstackdemo.com/")
try:
    WebDriverWait(driver, 5).until(EC.title_contains("StackDemo"))
    # Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page starts with 'StackDemo'
    driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Title contains StackDemo!"}}')
except TimeoutException:
    driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "​​Title does not contain StackDemo."}}')
driver.quit()

Verify resolution of error

Use the information in the following tabs to verify whether this error is resolved.

  1. Go to your Automate dashboard, navigate to your session, and click the Issues Detected tab.
  2. Check if the HTTP keep-alive missing error appears on the tab. Assume the error as resolved if no error is seen.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to an Expert
Download Copy Check Circle