Skip to main content

BrowserStack SDK - FAQs

This section addresses the most frequently asked questions about BrowserStack SDKs. If you have a question that’s not covered here, let us know:

How to toggle between BrowserStack and Local Setup when using BrowserStack SDK?

Based on the shell, use the following commands to easily toggle between local and BrowserStack runs:

  • If using Powershell
Copy icon
  • If using Command Prompt
Copy icon
  • If using Git Bash/MacOS/Linux
Copy icon

If capabilities are defined both in the browserstack.yml file and in test scripts, which ones are prioritized?

If you have capabilities declared in both the browserstack.yml file and in test scripts, the SDK merges these capabilities. However, if you have similar capabilities mentioned in both these locations, the capabilities in the browserstack.yml file take precedence.

When using BrowserStack Local, do I need to explicitly pass the local identifier?

No. The SDK handles this automatically for you.

Does the SDK support W3C or the JSON wire protocol?

The SDK supports both.

I am using local webdriver. Do I need to edit my test scripts to create remote webdriver?

No, the SDK intercepts all types of webdrivers and overrides them to point to BrowserStack’s remote grid.

Where do I add capabilities?

We recommend adding all capabilities in the browserstack.yml file, but you can also configure them in your test script. SDK uses the order of precedence logic to ensure capabilities are set correctly.

How do I integrate my CI/CD setup with BrowserStack SDK?

The steps to integrate with CI/CD tools are the same, whether you integrate using SDK or manually. Check out our documentation to learn more.

Which CLI args/ENV Variables can be used with BrowserStack SDK?

The following table displays all the supported CLI arguments with corresponding ENV variables:

CLI args list ENV Variables list
browserstack.username BROWSERSTACK_USERNAME
browserstack.user_name BROWSERSTACK_USER_NAME
browserstack.accesskey
browserstack.access_key
BROWSERSTACK_ACCESS_KEY
browserstack.projectName BROWSERSTACK_PROJECT_NAME
browserstack.buildName BROWSERSTACK_BUILD_NAME
browserstack.buildIdentifier BROWSERSTACK_BUILD_IDENTIFIER
browserstack.parallelsPerPlatform
browserstack.ppp
BROWSERSTACK_PARALLELS_PER_PLATFORM
browserstack.local BROWSERSTACK_LOCAL
browserstack.localIdentifier BROWSERSTACK_LOCAL_IDENTIFIER
browserstack.rerunTests BROWSERSTACK_RERUN_TESTS
browserstack.automation BROWSERSTACK_AUTOMATION
browserstack.app BROWSERSTACK_APP

Following is an example of how it can be used:

Using CLI args

Framework Prefix Example command
Java -D mvn test -P sample-test -Dbrowserstack.buildName="MySampleBuild"
NodeJS – – npm run sample-test -- --browserstack.buildName "MySampleBuild"
Or
npm run sample-test -- --browserstack.buildName="MySampleBuild"
Python browserstack-sdk python ./tests/test.py --browserstack.buildName "MySampleBuild"
C# dotnet browserstack-sdk --browserstack.buildName "MySampleBuild"

Using Environment Variables

OS Set env variable Set env variable in a single command
MacOS/Linux export BROWSERSTACK_BUILD_NAME="MySampleBuild" BROWSERSTACK_BUILD_NAME="MySampleBuild" <your-test-command>
Windows Powershell $env:BROWSERSTACK_BUILD_NAME "MySampleBuild" $env:BROWSERSTACK_BUILD_NAME="MySampleBuild"; <YOUR_TEST_COMMAND>
Windows cmd set BROWSERSTACK_BUILD_NAME "MySampleBuild" set BROWSERSTACK_BUILD_NAME="MySampleBuild"&& <YOUR_TEST_COMMAND>

Please note that CLI arguments take precedence over ENV variables

How to handle multiple config files in BrowserStack SDK?

The BrowserStack SDK enables you to maintain different configurations for different projects or folders within your test suites, by supporting multiple config files. This means, you can specify different platforms and capabilities for different tests, without having to update a single config file for the entire suite.
You can specify which config file is to be used using any one of the following methods:

Using CLI args

To use a specific config file with CLI args, simply add the browserstack.config flag followed by the path(relative/absolute) to the desired config file in the run command.

Framework Prefix Example command
Java -D mvn test -P sample-test -Dbrowserstack.config="customConfig.yml"
NodeJS – – npm run sample-test -- --browserstack.config "customConfig.yml"
npm run sample-test -- --browserstack.config="customConfig.yml"
Python browserstack-sdk python ./tests/test.py --browserstack.config "customConfig.yml"
C# dotnet browserstack-sdk --browserstack.config "customConfig.yml"

Using Environment Variables

To use a specific config file with environment variables, set the BROWSERSTACK_CONFIG_FILE variable to the path(relative/absolute) of the desired config file.

OS Set env variable Set env variable in a single command
MacOS/Linux export BROWSERSTACK_CONFIG_FILE="customConfig.yml" BROWSERSTACK_CONFIG_FILE="customConfig.yml" <your-test-command>
Windows Powershell $env:BROWSERSTACK_CONFIG_FILE "customConfig.yml" $env:BROWSERSTACK_CONFIG_FILE="customConfig.yml"; <YOUR_TEST_COMMAND>
Windows cmd set BROWSERSTACK_CONFIG_FILE "customConfig.yml" set BROWSERSTACK_CONFIG_FILE="customConfig.yml"&& <YOUR_TEST_COMMAND>

Please note that CLI arguments take precedence over ENV variables

Can I use custom variables as inputs for different platforms?

You can now fetch the platform details at the run-time for the current test session. You need to call getCurrentPlatform function, which will return the browser details as declared in the yml file in a readable format. You can then manipulate the returned data object to perform specific browser manipulations.

package com.browserstack; import org.junit.Test;
import java.io.IOException;
import java.net.URISyntaxException; import com.browserstack.BrowserStackSdk;
import static org.junit.Assert.assertTrue;
public class BStackLocalTest extends BrowserStackJUnitTest {
@Test
public void test() {
System.out.println(BrowserStackSdk.getCurrentPlatform()); driver.get("http://bs-local.com:45454/");
assertTrue("Local content not validated!", driver.getTitle().contains("BrowserStack Local"));
} }
var assert = require('assert');
const { Builder, Capabilities } = require("selenium-webdriver");
const { BrowserStackSdk } = require("browserstack-node-sdk");
var buildDriver = function() {
return new Builder().
usingServer('http://localhost:4444/wd/hub').
withCapabilities(Capabilities.chrome()).
build();
};
describe('BStack Local Testing', async function() {
this.timeout(0);
var driver;
before(function() {
driver = buildDriver();
});
it('check tunnel is working', async function () {
console.log(BrowserStackSdk.getCurrentPlatform());
await driver.get('http://bs-local.com:45454/');
let title = await driver.getTitle();
assert(title.match(/BrowserStack Local/i) != null);
});
after(async function() {
await driver.quit();
});
});
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using BrowserStack;
namespace BrowserStack
{
[TestFixture]
public class BrowserStackNUnitTest
{
protected RemoteWebDriver driver;
public BrowserStackNUnitTest()
{
}
[SetUp]
public void Init()
{
Dictionary<string, object> currentPlatform = BrowserStackSdk.GetCurrentPlatform();
foreach (var (Key, Value) in currentPlatform)
{
Console.WriteLine($"Key: {Key}, Value: {Value}");
}
DriverOptions capability = new OpenQA.Selenium.Chrome.ChromeOptions();
capability.BrowserVersion = "latest";
capability.AddAdditionalOption("bstack:options", capability);
driver = new RemoteWebDriver(
new Uri("http://localhost:444/wd/hub/"),
capability
);
}
[TearDown]
public void Cleanup() {
driver.Quit(); }
} }
import json
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options as ChromeOptions
from browserstack_sdk import BrowserStackSdk

# The webdriver management will be handled by the browserstack-sdk
# so this will be overridden and tests will run browserstack -
# without any changes to the test files!
options = ChromeOptions()
options.set_capability('sessionName', 'BStack Sample Test')
driver = webdriver.Chrome(options=options)

BrowserStackSdk.get_current_platform()

try:
    driver.get('https://bstackdemo.com/')
    WebDriverWait(driver, 10).until(EC.title_contains('StackDemo'))
    # Get text of an product - iPhone 12
    item_on_page = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.XPATH, '//*[@id="1"]/p'))).text
    # Click the 'Add to cart' button if it is visible
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
        (By.XPATH, '//*[@id="1"]/div[4]'))).click()
    # Check if the Cart pane is visible
    WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
        (By.CLASS_NAME, 'float-cart__content')))
    # Get text of product in cart
    item_in_cart = WebDriverWait(driver, 10).until(EC.visibility_of_element_located(
        (By.XPATH, '//*[@id="__next"]/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]'))).text
    # Verify whether the product (iPhone 12) is added to cart
    if item_on_page == item_in_cart:
        # Set the status of test as 'passed' if item is added to cart
        driver.execute_script(
            'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "iPhone 12 has been successfully added to the cart!"}}')
    else:
        # Set the status of test as 'failed' if item is not added to cart
        driver.execute_script(
            'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "iPhone 12 not added to the cart!"}}')
except NoSuchElementException as err:
    message = 'Exception: ' + str(err.__class__) + str(err.msg)
    driver.execute_script(
        'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": ' + json.dumps(message) + '}}')
except Exception as err:
    message = 'Exception: ' + str(err.__class__) + str(err.msg)
    driver.execute_script(
        'browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": ' + json.dumps(message) + '}}')

finally:
    # Stop the driver
    driver.quit()

How Can I identify the current browser/platform for which the test is being executed?

You can pass custom variables specific to different browsers within the browserstack.yml file. At the run-time you can identify these current browser instances and the corresponding custom variables that you had declared. The getCurrentPlatform function returns the browser data, and you may retrieve the custom variables related to that browser and manipulate them as needed.

browserstack.yml

Example use case: You want to test login functionality and want to pass a different set of credentials for 3 different browsers.

How to fix the Java static WebDriver issue?

Declaring WebDriver static can lead to IDLE_TIMEOUT. From browserstack-java-sdk@1.3.0 onwards, enabling the staticWebDriver: true flag in the browserstack.yml fixes this issue.

At present, the default value is set to false to make it an experimental feature.

Is the C# debugger supported with SDK?

Yes, Debugger is supported after browserstack-sdk@1.0.2 and BrowserStack.TestAdapter@0.1.0.

SDK spawns as many test processes as the number of platforms you want to test on. Debugger is attached to the first platform mentioned on browserstack.yml. To attach the debugger to 2nd platform, 1st platform needs to be removed from platforms array in browserstack.yml.

Important: The Debugger is currently only supported for Windows.

NodeJS - How to load environment variables from a .env file?

You can load custom environment variables into your Node SDK project from an external .env file. Set the path to your .env file (you can use both relative and absolute paths) in your browserstack.yml file. Here is a sample test.env file in a sample path:

envFile: \sample-path\test.env # load custom environment

What are the prerequisites for Dotnet version 8?

If you are using Dotnet V8, you need to have the latest version of BrowserStack test adapter & BrowserStack SDK installed. We have added the support from the versions BrowserStack.TestAdapter - 0.9.2 to browserstack-sdk - 1.12.2.

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