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));
const executorConfig = {
"action": "setSessionName",
"arguments": {
"name": "<test-name>"
}
};
await driver.executeScript('browserstack_executor: ' + JSON.stringify(executorConfig), []);
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
JsonObject executorObject = new JsonObject();
JsonObject argumentsObject = new JsonObject();
argumentsObject.Add("name", "<test-name>");
executorObject.Add("action", "setSessionName");
executorObject.Add("arguments", argumentsObject);
jse.ExecuteScript("browserstack_executor: " + executorObject.ToString());
$web_driver->executeScript('browserstack_executor: {"action": "setSessionName", "arguments": {"name":"<test-name>"}}' );
import json
executor_object = {
'action': 'setSessionName',
'arguments': {
'name': "<test-name>"
}
}
browserstack_executor = 'browserstack_executor: {}'.format(json.dumps(executor_object))
driver.execute_script(browserstack_executor)
executor_config = {
action: "setSessionName",
arguments: {
name: "<test-name>"
}
}
json = executor_config.to_json
script = 'browserstack_executor: ' + json
driver.execute_script(script)
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 eitherpassed
orfailed
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));
const executorConfig = {
"action": "setSessionStatus",
"arguments": {
"status": "<passed/failed>",
"reason": "<reason>"
}
};
await driver.executeScript('browserstack_executor: ' + JSON.stringify(executorConfig), []);
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
JsonObject executorObject = new JsonObject();
JsonObject argumentsObject = new JsonObject();
argumentsObject.Add("status", "<passed/failed>");
argumentsObject.Add("reason", "<reason>");
executorObject.Add("action", "setSessionStatus");
executorObject.Add("arguments", argumentsObject);
jse.ExecuteScript("browserstack_executor: " + executorObject.ToString());
$web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"<passed/failed>", "reason": "<reason>"}}' );
executor_object = {
'action': 'setSessionStatus',
'arguments': {
'status': "<passed/failed>",
'reason': "<reason>"
}
}
browserstack_executor = 'browserstack_executor: {}'.format(json.dumps(executor_object))
driver.execute_script(browserstack_executor)
executor_config = {
action: "setSessionStatus",
arguments: {
status: "<passed/failed>",
reason: "<reason>"
}
}
json = executor_config.to_json
script = 'browserstack_executor: ' + json
driver.execute_script(script)
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();
}
}
var webdriver = require('selenium-webdriver');
// Input capabilities
var capabilities = {
'bstack:options' : {
"os" : "Windows",
"osVersion" : "10",
},
"browserName" : "Chrome",
"browserVersion" : "latest",
}
var driver = new webdriver.Builder().
usingServer('https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub').
withCapabilities(capabilities).
build();
// Setting name of the test
driver.executeScript('browserstack_executor: {"action": "setSessionName", "arguments": {"name": "Node test"}}');
// Searching for 'BrowserStack' on google.com
driver.get('https://www.google.com').then(function(){
driver.findElement(webdriver.By.name('q')).sendKeys('BrowserStack\n').then(function(){
driver.getTitle().then(function(title) {
console.log(title);
// Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page matches 'BrowserStack - Google Search'
if(title === "BrowserStack - Google Search"){
driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Title matched!"}}');
} else {
driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Title not matched!"}}');
}
driver.quit();
});
});
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace cs_testing
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver;
// Input capabilities
ChromeOptions capabilities = new ChromeOptions();
capabilities.BrowserVersion = "102.0";
Dictionary<string, object> browserstackOptions = new Dictionary<string, object>();
browserstackOptions.Add("os", "Windows");
browserstackOptions.Add("osVersion", "11");
capabilities.AddAdditionalOption("bstack:options", browserstackOptions);
driver = new RemoteWebDriver(
new Uri("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub/"), capability
);
// Setting name of the test
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionName\", \"arguments\": {\"name\":\" C-sharp test \"}}");
// Searching for 'BrowserStack' on google.com
driver.Navigate().GoToUrl("https://www.google.com");
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("BrowserStack");
query.Submit();
Console.WriteLine(driver.Title);
// Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page matches 'BrowserStack - Google Search'
string str = "BrowserStack - Google Search";
if (string.Equals(driver.Title, str))
{
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \" Title matched!\"}}");
}
else
{
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title not matched \"}}");
}
driver.Quit();
}
}
}
<?php
require_once('vendor/autoload.php');
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
# Input capabilities
$caps = array(
'bstack:options' => array(
"os" => "Windows",
"osVersion" => "11",
),
"browserName" => "Chrome",
"browserVersion" => "102.0",
)
$web_driver = RemoteWebDriver::create("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub", $caps);
# Setting name of the test
$web_driver->executeScript('browserstack_executor: {"action": "setSessionName", "arguments": {"name":"PHP Test"}}' );
# Searching for 'BrowserStack' on google.com
$web_driver->get("https://google.com");
$element = $web_driver->findElement(WebDriverBy::name("q"));
if($element) {
$element->sendKeys("BrowserStack");
$element->submit();
}
print $web_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 ($web_driver->getTitle() == "BrowserStack - Google Search"){
$web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Title matched!"}}' );
} else {
$web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Title not matched!"}}');
}
$web_driver->quit();
?>
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# Input capabilities
desired_cap = {
'bstack:options' : {
"os" : "Windows",
"osVersion" : "11",
},
"browserName" : "Chrome",
"browserVersion" : "102.0",
}
driver = webdriver.Remote(
command_executor='https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub',
desired_capabilities=desired_cap)
# Setting a name to the test
driver.execute_script('browserstack_executor: {"action": "setSessionName", "arguments": {"name": "Python test"}}')
# Searching for 'BrowserStack' on google.com
driver.get("https://www.google.com")
if not "Google" in driver.title:
raise Exception("Unable to load google page!")
elem = driver.find_element_by_name("q")
elem.send_keys("BrowserStack")
elem.submit()
print(driver.title)
# 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.title=="BrowserStack - Google Search"):
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Title matched!"}}')
else:
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Title not matched"}}')
driver.quit()
require "rubygems"
require "selenium-webdriver"
# Input capabilities
capabilities = {
'bstack:options' => {
"os" => "Windows",
"osVersion" => "11",
},
"browserName" => "Chrome",
"browserVersion" => "102.0",
}
driver = Selenium::WebDriver.for(:remote,
:url => "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
:desired_capabilities => caps)
# Setting name of the test
driver.execute_script('browserstack_executor: {"action": "setSessionName", "arguments": {"name": "Ruby test"}}')
# Searching for 'BrowserStack' on google.com
driver.navigate.to "https://www.google.com"
element = driver.find_element(:name, "q")
element.send_keys "BrowserStack"
element.submit
puts driver.title
# 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.title=="BrowserStack - Google Search"
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Title matched!"}}')
else
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Title not matched"}}')
end
driver.quit
package bs_automate;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
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 caps = new DesiredCapabilities();
caps.setCapability("browser", "Chrome");
caps.setCapability("browser_version", "72.0");
caps.setCapability("os", "Windows");
caps.setCapability("os_version", "10");
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();
}
}
var webdriver = require('selenium-webdriver');
// Input capabilities
var capabilities = {
'browserName': 'chrome',
'browserVersion': '72.0',
'os': 'windows',
'os_version': '10'
}
var driver = new webdriver.Builder().
usingServer('https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub').
withCapabilities(capabilities).
build();
// Setting name of the test
driver.executeScript('browserstack_executor: {"action": "setSessionName", "arguments": {"name": "Node test"}}');
// Searching for 'BrowserStack' on google.com
driver.get('https://www.google.com').then(function(){
driver.findElement(webdriver.By.name('q')).sendKeys('BrowserStack\n').then(function(){
driver.getTitle().then(function(title) {
console.log(title);
// Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page matches 'BrowserStack - Google Search'
if(title === "BrowserStack - Google Search"){
driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Title matched!"}}');
} else {
driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Title not matched!"}}');
}
driver.quit();
});
});
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace cs_testing
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver;
// Input capabilities
DesiredCapabilities capability = new DesiredCapabilities();
capability.SetCapability("browser", "Chrome");
capability.SetCapability("browser_version", "72.0");
capability.SetCapability("os", "Windows");
capability.SetCapability("os_version", "10");
driver = new RemoteWebDriver(
new Uri("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub/"), capability
);
// Setting name of the test
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionName\", \"arguments\": {\"name\":\" C-sharp test \"}}");
// Searching for 'BrowserStack' on google.com
driver.Navigate().GoToUrl("https://www.google.com");
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("BrowserStack");
query.Submit();
Console.WriteLine(driver.Title);
// Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page matches 'BrowserStack - Google Search'
string str = "BrowserStack - Google Search";
if (string.Equals(driver.Title, str))
{
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \" Title matched!\"}}");
}
else
{
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Title not matched \"}}");
}
driver.Quit();
}
}
}
<?php
require_once('vendor/autoload.php');
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;
# Input capabilities
$caps = array(
"browserName"=> "chrome",
"browserVersion"=>"72.0",
"os"=>"windows",
"os_version"=>"10",
);
$web_driver = RemoteWebDriver::create("https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub", $caps);
# Setting name of the test
$web_driver->executeScript('browserstack_executor: {"action": "setSessionName", "arguments": {"name":"PHP Test"}}' );
# Searching for 'BrowserStack' on google.com
$web_driver->get("https://google.com");
$element = $web_driver->findElement(WebDriverBy::name("q"));
if($element) {
$element->sendKeys("BrowserStack");
$element->submit();
}
print $web_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 ($web_driver->getTitle() == "BrowserStack - Google Search"){
$web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Title matched!"}}' );
} else {
$web_driver->executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Title not matched!"}}');
}
$web_driver->quit();
?>
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# Input capabilities
desired_cap = {
'browserName': 'chrome',
'browserVersion': '72.0',
'os': 'windows',
'os_version': '10'
}
driver = webdriver.Remote(
command_executor=''https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub',
desired_capabilities=desired_cap)
# Setting a name to the test
driver.execute_script('browserstack_executor: {"action": "setSessionName", "arguments": {"name": "Python test"}}')
# Searching for 'BrowserStack' on google.com
driver.get("https://www.google.com")
if not "Google" in driver.title:
raise Exception("Unable to load google page!")
elem = driver.find_element_by_name("q")
elem.send_keys("BrowserStack")
elem.submit()
print(driver.title)
# 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.title=="BrowserStack - Google Search"):
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Title matched!"}}')
else:
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Title not matched"}}')
driver.quit()
require "rubygems"
require "selenium-webdriver"
# Input capabilities
caps = Selenium::WebDriver::Remote::Capabilities.new
caps["os"] = "Windows"
caps["os_version"] = "10"
caps["browser"] = "Chrome"
caps["browser_version"] = "72.0"
caps["javascriptEnabled"] = "true"
driver = Selenium::WebDriver.for(:remote,
:url => "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
:desired_capabilities => caps)
# Setting name of the test
driver.execute_script('browserstack_executor: {"action": "setSessionName", "arguments": {"name": "Ruby test"}}')
# Searching for 'BrowserStack' on google.com
driver.navigate.to "https://www.google.com"
element = driver.find_element(:name, "q")
element.send_keys "BrowserStack"
element.submit
puts driver.title
# 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.title=="BrowserStack - Google Search"
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Title matched!"}}')
else
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Title not matched"}}')
end
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
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!