Skip to main content

Test on Internal Networks

BrowserStack can integrate with test suites pointing to your localhost URL, staging environment, and even websites behind one or more proxies/firewalls. This is done using BrowserStack Local - a tunneling feature that establishes a secure connection between your machine and the BrowserStack Cloud.

Prerequisites

If you have already run your first test, you can skip the prerequisites.

Run your first Local test

Configure your Python tests for Local Testing using the following steps:

  1. Clone the sample python-selenium-browserstack repository using the following command:
     git clone https://github.com/browserstack/python-selenium-browserstack.git
    
  2. Run the following commands in your command-line to install the required dependencies in the cloned repository:
     cd python-selenium-browserstack
     pip install browserstack-local
    
  3. Set your BrowserStack credentials in the scripts/local.py file as follows:
     # set your Access Key
     BROWSERSTACK_USERNAME = os.environ.get("BROWSERSTACK_USERNAME") or "YOUR_USERNAME"
     BROWSERSTACK_ACCESS_KEY = os.environ.get("BROWSERSTACK_ACCESS_KEY") or "YOUR_ACCESS_KEY"
    
  4. Verify that the browserstack.local capability is set to true in the scripts/local.py file.
      desired_cap = {
       "local" : "true"
      }
    
  5. Verify that the seleniumVersion capability is set to 4.0.0 or greater version in the scripts/local.py file.
      desired_cap = {
       "seleniumVersion" : "4.0.0"
      }
    
  6. Run the Python test using the following command::
     cd scripts
     python3 local.py
    
  7. View your tests on your Automate Dashboard.
Protip: Check out our capability builder and select from a range of custom capabilities that BrowserStack supports.

Understand your Local test script

When you run the python3 local.py command, the local.py file within the scripts directory is executed. When the test is triggered, it:

  • Starts Local Testing connection
  • Opens http://bs-local.com:45691/check
  • Checks whether the web page contains the Up and running text
  • Marks the test as passed or failed based on the availability of the text
  • Stops the Local Testing connection.
local.py
    from dotenv import load_dotenv
    import os
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.chrome.options import Options as ChromeOptions
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from browserstack.local import Local
    from selenium.common.exceptions import NoSuchElementException

    load_dotenv()
    BROWSERSTACK_USERNAME = os.environ.get("BROWSERSTACK_USERNAME") or "YOUR_USERNAME"
    BROWSERSTACK_ACCESS_KEY = os.environ.get("BROWSERSTACK_ACCESS_KEY") or "YOUR_ACCESS_KEY"
    URL = os.environ.get("URL") or "https://hub.browserstack.com/wd/hub"

    # Creates an instance of Local
    bs_local = Local()

    # You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
    bs_local_args = { "key": BROWSERSTACK_ACCESS_KEY }

    # Starts the Local instance with the required arguments
    try:
      bs_local.start(**bs_local_args)
    except Exception:
      bs_local.stop()

    bs_local.start(**bs_local_args)

    # Check if BrowserStack local instance is running
    print(bs_local.isRunning())

    desired_cap = {
        "os" : "OS X",
        "osVersion" : "Sierra",
        "buildName" : "Final-Snippet-Test",
        "sessionName" : "Selenium-4 Python snippet test",
        "local" : "true",
        "seleniumVersion" : "4.0.0",
        "userName": BROWSERSTACK_USERNAME,
        "accessKey": BROWSERSTACK_ACCESS_KEY
    }

    options = ChromeOptions()
    options.set_capability('bstack:options', desired_cap)
    driver = webdriver.Remote(
        command_executor=URL,
        options=options)
    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
        # 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"}}')
    except NoSuchElementException:
        driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Local test setup failed"}}')
        bs_local.stop()

    except Exception:
        driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some exception occurred"}}')
        bs_local.stop()
    # Stop the driver
    driver.quit()
    bs_local.stop()

Next steps

After you have successfully run your first test using BrowserStack Local, you can explore the following sections:

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