Skip to main content
Get Automate insights in Microsoft Teams with our new integration! Get Started!.

Test on Internal Networks

BrowserStack enables you to run your Ruby automated tests on your internal development environments, on localhost, and from behind a corporate firewall. This feature is called “Local Testing.”

Local Testing establishes a secure connection between your machine and the BrowserStack cloud. Once you set up Local Testing, all URLs work out of the box, including HTTPS URLs and those behind a proxy or firewall.

In this section, you’ll learn:

Prerequisites

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

Run your first Local test

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

  1. Clone the sample ruby-selenium-browserstack repository using the following command:
     git clone https://github.com/browserstack/ruby-selenium-browserstack.git
    
  2. Run the following commands in your command-line to install the required dependencies in the cloned repository:
     cd ruby-selenium-browserstack
     gem install selenium-webdriver -v 4.0.0
     gem install browserstack-local
    
  3. Set your BrowserStack credentials in the scripts/local.rb file as follows:
       # set your Username and Access Key. You can also set them as environment variables.
       USER_NAME = ENV['BROWSERSTACK_USERNAME'] || "YOUR_USER_NAME"
       ACCESS_KEY = ENV['BROWSERSTACK_ACCESS_KEY'] || "YOUR_ACCESS_KEY"
       # ...
    
  4. Verify that the browserstack.local capability is set to true in the scripts/local.rb file.
     bstack_options = {
     "local" => "true",
     }
    
  5. Run your Ruby test using the following command:
      cd scripts
      ruby local.rb
    
  6. 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 ruby local.rb command, the local.rb 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.rb
require 'rubygems'
require 'selenium-webdriver'
require "browserstack/local"

USER_NAME = ENV['BROWSERSTACK_USERNAME'] || "YOUR_USER_NAME"
ACCESS_KEY = ENV['BROWSERSTACK_ACCESS_KEY'] || "YOUR_ACCESS_KEY"

# Creates an instance of Local
bs_local = BrowserStack::Local.new

# You can also set an environment variable - "BROWSERSTACK_ACCESS_KEY".
bs_local_args = { "key" => ACCESS_KEY, "force" => "true" }

# Starts the Local instance with the required arguments
bs_local.start(bs_local_args)

# Check if BrowserStack local instance is running
puts bs_local.isRunning
# Input capabilities
options = Selenium::WebDriver::Options.chrome
options.browser_version = 'latest'
options.platform_name = 'MAC'
bstack_options = {
    "os" => "OS X",
    "osVersion" => "Sierra",
    "buildName" => "Final-Snippet-Test",
    "sessionName" => "Selenium-4 Ruby snippet test",
    "local" => "true",
    "seleniumVersion" => "4.0.0",
}

options.add_option('bstack:options', bstack_options)

driver = Selenium::WebDriver.for(:remote,
  :url => "https://#{USER_NAME}:#{ACCESS_KEY}@hub-cloud.browserstack.com/wd/hub",
  :capabilities => options)
begin
    # opening the bstackdemo.com website
    driver.navigate.to "http://bs-local.com:45691/check"
    wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
    
    body = driver.find_element(:css, 'body')
    wait.until { body.displayed? }
    body_text = body.text

    if body_text.eql? "Up and running"
        # marking test as 'passed' local ran succesfully
        driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Local ran successfully"}}')
    else
        # marking test as 'failed' 
        driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Local setup failed"}}')
    end
# marking test as 'failed' if test script is unable to open the bstackdemo.com website
rescue
    driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Some elements failed to load"}}')
end
driver.quit 

# Stop the Local instance
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
Talk to an Expert