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

Set name and status of Selenium tests

Learn how to set a name and status of Selenium tests running on BrowserStack Automate using JavascriptExecutors.

This document will guide you to provide a name to the test, know the status of the test, and recognize the reason for corresponding test status using JavascriptExecutor. This allows you to debug your test cases smoothly and rapidly. This can be achieved using REST API also.

BrowserStack has developed custom JavascriptExecutor methods which makes it possible to perform the following actions while a test is running:

  • Provide a name to the test
  • Know the status of the test (passed/failed), and the reason for corresponding test status

Setting a test name

Providing a name to the test provides an identity to each test.

Use the following syntax to set a name to the test in different languages. The argument passed in the JavaScript method for setting a name to the test is name, the value accepted by the argument is in string datatype.

final JavascriptExecutor jse = (JavascriptExecutor) driver;
JSONObject executorObject = new JSONObject();
JSONObject argumentsObject = new JSONObject();
argumentsObject.put("name", "<test-name>");
executorObject.put("action", "setSessionName");
executorObject.put("arguments", argumentsObject);
jse.executeScript(String.format("browserstack_executor: %s", executorObject));

If you are using BrowserStack SDK, the sessionName and setSessionStatus are managed autonomously by the SDK. However, this is applicable only for language frameworks. For vanilla flavours, Session name and status need to be set manually using JavaScript Executor.

Setting the test status

Setting the status allows you to identify if your testcase passed all the assertions mentioned in the script or not.

The arguments passed in the JavaScript method for setting the status and the corresponding reason of the test are status and reason.

  • status accepts either passed or failed as the value
  • reason accepts a value in string datatype

Here is the syntax for setting the status of the test in different languages.

final JavascriptExecutor jse = (JavascriptExecutor) driver;
JSONObject executorObject = new JSONObject();
JSONObject argumentsObject = new JSONObject();
argumentsObject.put("status", "<passed/failed>");
argumentsObject.put("reason", "<reason>");
executorObject.put("action", "setSessionStatus");
executorObject.put("arguments", argumentsObject);
jse.executeScript(String.format("browserstack_executor: %s", executorObject));

Example

The following test scripts includes JavascriptExecutor methods for setting a name and status of the test in various languages:

package bs_automate;
import java.net.URL;
import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class bs_automate_class {
  public static final String USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";
  public static void main(String[] args) throws Exception {
  // Input capabilities
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("browserName", "Chrome");
  capabilities.setCapability("browserVersion", "latest");
  HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
  browserstackOptions.put("os", "Windows");
  browserstackOptions.put("osVersion", "11");
  capabilities.setCapability("bstack:options", browserstackOptions);
  WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
  JavascriptExecutor jse = (JavascriptExecutor)driver;

    // Setting name of the test
    jse.executeScript("browserstack_executor: {\"action\": \"setSessionName\", \"arguments\": {\"name\":\"Java test \" }}");

    // Searching for 'BrowserStack' on google.com
    driver.get("https://www.google.com");
    WebElement element = driver.findElement(By.name("q"));
    element.sendKeys("BrowserStack");
    element.submit();
    System.out.println(driver.getTitle());

    // Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page matches 'BrowserStack - Google Search'
    if (driver.getTitle().equals("BrowserStack - Google Search")) {
      jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"Title matched!\"}}");
    }
    else {
      jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"Title not matched\"}}");
    }

    driver.quit();
  }
}

If you are using BrowserStack SDK, the sessionName and setSessionStatus are managed autonomously by the SDK. However, this is applicable only for language frameworks. For vanilla flavours, Session name and status need to be set manually using JavaScript Executor.

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