Get your setup working faster. Join our Discord for optimisation tips from elite testers.Join our Discord
Mark Appium tests as passed or failed
Mark your Appium tests as passed or failed on BrowserStack App Automate. If a test has failed, record the reason for failure for easy troubleshooting.
BrowserStack does not know whether your test’s assertions have passed or failed. Therefore, based on your test script’s assertions, you can explicitly inform BrowserStack whether your tests have passed or failed, and the test status can be marked accordingly. This document will help you in updating your test status.
Once you have executed the steps given below, the status and reason are visible on the test session details page of the App Automate dashboard as shown below:
You can mark the status of your test along with the reason using the following methods:
Mark test status from within test script using JavascriptExecutor
Your test script might contain a lot of assertions and you may choose to mark the status of the test based on any/all assertions, as per your test logic using a JavascriptExecutor during test execution.
The following arguments are required to be passed while using the JavascriptExecutor:
status accepts either passed or failed as the value
reason accepts a value in string datatype. This can be useful to record a reason for test failure.
Use the code snippet below to mark the status of the test using JavascriptExecutor.
caps["javascriptEnabled"]="true"#Additionally, include this capability for JavaScript Executors to workdriver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"<passed/failed>", "reason": "<reason>"}}')
If you are using BrowserStack SDK, setSessionStatus is managed autonomously by the SDK. However, this is applicable only for language frameworks. For vanilla flavours, Session status needs to be set manually using JavaScript Executor.
Following is a sample test script shows the marking of test status and the associated reason:
packageandroid;importjava.net.URL;importjava.util.List;importjava.net.MalformedURLException;importio.appium.java_client.MobileBy;importio.appium.java_client.android.AndroidDriver;importio.appium.java_client.android.AndroidElement;importorg.openqa.selenium.JavascriptExecutor;importorg.openqa.selenium.support.ui.ExpectedConditions;importorg.openqa.selenium.support.ui.WebDriverWait;importorg.openqa.selenium.remote.DesiredCapabilities;publicclassBrowserStackSample{publicstaticvoidmain(String[]args)throwsMalformedURLException,InterruptedException{DesiredCapabilitiescapabilities=newDesiredCapabilities();capabilities.setCapability("platformName","android");capabilities.setCapability("platformVersion","9.0");capabilities.setCapability("deviceName","Google Pixel 3");capabilities.setCapability("app","bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32");AndroidDriver<AndroidElement>driver=newAndroidDriver<AndroidElement>(newURL("https://#YOUR_USERNAME:#YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub"),capabilities);JavascriptExecutorjse=(JavascriptExecutor)driver;AndroidElementsearchElement=(AndroidElement)newWebDriverWait(driver,30).until(ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia")));searchElement.click();AndroidElementinsertTextElement=(AndroidElement)newWebDriverWait(driver,30).until(ExpectedConditions.elementToBeClickable(MobileBy.id("org.wikipedia.alpha:id/search_src_text")));insertTextElement.sendKeys("BrowserStack");Thread.sleep(5000);List<AndroidElement>allProductsName=driver.findElementsByClassName("android.widget.TextView");// Setting the status of test as 'passed' or 'failed' based on the condition: if results are found for the searchif(allProductsName.size()>0){jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"Results found!\"}}");}else{jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"Results not found\"}}");}driver.quit();}}
letwd=require('wd');letassert=require('assert');letasserters=wd.asserters;varCaps={"platformName":"android","platformVersion":"9.0","deviceName":"Google Pixel 3","app":"bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32",};driver=wd.promiseRemote("https://#YOUR_USERNAME:#YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub");driver.init(desiredCaps).then(function(){returndriver.waitForElementByAccessibilityId('Search Wikipedia',asserters.isDisplayed&&asserters.isEnabled,30000);}).then(function(searchElement){returnsearchElement.click();}).then(function(){returndriver.waitForElementById('org.wikipedia.alpha:id/search_src_text',asserters.isDisplayed&&asserters.isEnabled,30000);}).then(function(searchInput){returnsearchInput.sendKeys("BrowserStack");}).then(function(){returndriver.elementsByClassName('android.widget.TextView');}).then(function(search_results){// Setting the status of test as 'passed' or 'failed' based on the condition if results are found for the searchif(search_results.length>0){returndriver.execute('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Results found!"}}');}else{driver.execute('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "No results available!"}}');}}).fin(function(){returndriver.quit();}).done();
usingSystem;usingOpenQA.Selenium.Appium;usingOpenQA.Selenium.Appium.Android;usingOpenQA.Selenium.Remote;usingOpenQA.Selenium.Support.UI;usingSystem.Collections.Generic;usingOpenQA.Selenium;namespacecsharp_appium_first_test_browserstack{classProgram{staticvoidMain(string[]args){AppiumOptionscapabilities=newAppiumOptions();// Set URL of the application under testcapabilities.AddAdditionalCapability("bstack:options",browserstackOptions);capabilities.AddAdditionalCapability("platformName","android");capabilities.AddAdditionalCapability("platformVersion","9.0");capabilities.AddAdditionalCapability("appium:deviceName","Google Pixel 3");capabilities.AddAdditionalCapability("appium:app","bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32");AndroidDriver<AndroidElement>driver=newAndroidDriver<AndroidElement>(newUri("https://#YOUR_USERNAME:#YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub"),capabilities);AndroidElementsearchElement=(AndroidElement)newWebDriverWait(driver,TimeSpan.FromSeconds(30)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia")));searchElement.Click();AndroidElementinsertTextElement=(AndroidElement)newWebDriverWait(driver,TimeSpan.FromSeconds(30)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(MobileBy.Id("org.wikipedia.alpha:id/search_src_text")));insertTextElement.SendKeys("BrowserStack");System.Threading.Thread.Sleep(5000);IReadOnlyList<AndroidElement>allTextViewElements=driver.FindElementsByClassName("android.widget.TextView");// Setting the status of test as 'passed' or 'failed' based on the condition if results are found for the searchif((allTextViewElements.Count>0)){((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \" Results found! \"}}");}else{((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Results not found!\"}}");}driver.Quit();}}}
<?phprequire_once('vendor/autoload.php');useFacebook\WebDriver\Remote\DesiredCapabilities;useFacebook\WebDriver\Remote\RemoteWebDriver;useFacebook\WebDriver\WebDriverBy;$caps=array("platformName"=>"android","platformVersion"=>"9.0","deviceName"=>"Google Pixel 3","app"=>"bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32",)$driver=RemoteWebDriver::create("https://#YOUR_USERNAME:#YOUR_ACCESSKEY@hub-cloud.browserstack.com/wd/hub",$caps);$driver->getTitle();# Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page matches 'Wikipedia'if($driver->getTitle()=="Wikipedia"){$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();?>
fromappiumimportwebdriverfromappium.webdriver.common.mobilebyimportMobileByfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasECimporttimedesired_cap={"platformName":"android","platformVersion":"9.0","deviceName":"Google Pixel 3","app":"bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32",}driver=webdriver.Remote(command_executor="https://#YOUR_USERNAME:#YOUR_ACCESSKEY@hub-cloud.browserstack.com/wd/hub",desired_capabilities=desired_cap)search_element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID,"Search Wikipedia")))search_element.click()search_input=WebDriverWait(driver,30).until(EC.element_to_be_clickable((MobileBy.ID,"org.wikipedia.alpha:id/search_src_text")))search_input.send_keys("BrowserStack")time.sleep(5)# Setting the status of test as 'passed' or 'failed' based on the condition if results are found for the search
search_results=driver.find_elements_by_class_name("android.widget.TextView")if(len(search_results)>0):driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Results found!"}}')else:driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "No results found"}}')driver.quit()
require'rubygems'require'appium_lib'caps={"platformName"=>"android","platformVersion"=>"9.0","deviceName"=>"Google Pixel 3","app"=>"bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32",}appium_driver=Appium::Driver.new({'caps'=>caps,'appium_lib'=>{:server_url=>"https://#YOUR_USERNAME:#YOUR_ACCESSKEY@hub-cloud.browserstack.com/wd/hub"}},true)driver=appium_driver.start_driverwait=Selenium::WebDriver::Wait.new(:timeout=>30)wait.until{driver.find_element(:accessibility_id,"Search Wikipedia").displayed?}element=driver.find_element(:accessibility_id,"Search Wikipedia")element.clickwait.until{driver.find_element(:id,"org.wikipedia.alpha:id/search_src_text").displayed?}search_box=driver.find_element(:id,"org.wikipedia.alpha:id/search_src_text")search_box.send_keys("BrowserStack")sleep5# Setting the status of test as 'passed' or 'failed' based on the condition if results are found for the searchresults=driver.find_elements(:class,"android.widget.TextView")ifresults.count>0driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Results found"}}')elsedriver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "No results found"}}')enddriver.quit
packageandroid;importjava.net.URL;importjava.util.List;importjava.net.MalformedURLException;importio.appium.java_client.MobileBy;importio.appium.java_client.android.AndroidDriver;importio.appium.java_client.android.AndroidElement;importorg.openqa.selenium.JavascriptExecutor;importorg.openqa.selenium.support.ui.ExpectedConditions;importorg.openqa.selenium.support.ui.WebDriverWait;importorg.openqa.selenium.remote.DesiredCapabilities;publicclassBrowserStackSample{publicstaticvoidmain(String[]args)throwsMalformedURLException,InterruptedException{DesiredCapabilitiescapabilities=newDesiredCapabilities();capabilities.setCapability("os_version","9.0");capabilities.setCapability("device","Google Pixel 3");capabilities.setCapability("app","bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32");AndroidDriver<AndroidElement>driver=newAndroidDriver<AndroidElement>(newURL("https://#YOUR_USERNAME:#YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub"),capabilities);JavascriptExecutorjse=(JavascriptExecutor)driver;AndroidElementsearchElement=(AndroidElement)newWebDriverWait(driver,30).until(ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia")));searchElement.click();AndroidElementinsertTextElement=(AndroidElement)newWebDriverWait(driver,30).until(ExpectedConditions.elementToBeClickable(MobileBy.id("org.wikipedia.alpha:id/search_src_text")));insertTextElement.sendKeys("BrowserStack");Thread.sleep(5000);List<AndroidElement>allProductsName=driver.findElementsByClassName("android.widget.TextView");// Setting the status of test as 'passed' or 'failed' based on the condition: if results are found for the searchif(allProductsName.size()>0){jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"Results found!\"}}");}else{jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"Results not found\"}}");}driver.quit();}}
letwd=require('wd');letassert=require('assert');letasserters=wd.asserters;varcapabilities={"os_version":"9.0","device":"Google Pixel 3","app":"bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32",}driver=wd.promiseRemote("https://#YOUR_USERNAME:#YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub");driver.init(desiredCaps).then(function(){returndriver.waitForElementByAccessibilityId('Search Wikipedia',asserters.isDisplayed&&asserters.isEnabled,30000);}).then(function(searchElement){returnsearchElement.click();}).then(function(){returndriver.waitForElementById('org.wikipedia.alpha:id/search_src_text',asserters.isDisplayed&&asserters.isEnabled,30000);}).then(function(searchInput){returnsearchInput.sendKeys("BrowserStack");}).then(function(){returndriver.elementsByClassName('android.widget.TextView');}).then(function(search_results){// Setting the status of test as 'passed' or 'failed' based on the condition if results are found for the searchif(search_results.length>0){returndriver.execute('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "Results found!"}}');}else{driver.execute('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "No results available!"}}');}}).fin(function(){returndriver.quit();}).done();
usingSystem;usingOpenQA.Selenium.Appium;usingOpenQA.Selenium.Appium.Android;usingOpenQA.Selenium.Remote;usingOpenQA.Selenium.Support.UI;usingSystem.Collections.Generic;usingOpenQA.Selenium;namespacecsharp_appium_first_test_browserstack{classProgram{staticvoidMain(string[]args){AppiumOptionscapabilities=newAppiumOptions();capabilities.AddAdditionalCapability("os_version","9.0");capabilities.AddAdditionalCapability("device","Google Pixel 3");capabilities.AddAdditionalCapability("app","bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32");capabilities.AddAdditionalCapability("browserstack.user","YOUR_USERNAME");capabilities.AddAdditionalCapability("browserstack.key","YOUR_ACCESS_KEY");AndroidDriver<AndroidElement>driver=newAndroidDriver<AndroidElement>(newUri("https://#YOUR_USERNAME:#YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub"),capabilities);AndroidElementsearchElement=(AndroidElement)newWebDriverWait(driver,TimeSpan.FromSeconds(30)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia")));searchElement.Click();AndroidElementinsertTextElement=(AndroidElement)newWebDriverWait(driver,TimeSpan.FromSeconds(30)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(MobileBy.Id("org.wikipedia.alpha:id/search_src_text")));insertTextElement.SendKeys("BrowserStack");System.Threading.Thread.Sleep(5000);IReadOnlyList<AndroidElement>allTextViewElements=driver.FindElementsByClassName("android.widget.TextView");// Setting the status of test as 'passed' or 'failed' based on the condition if results are found for the searchif((allTextViewElements.Count>0)){((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \" Results found! \"}}");}else{((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \" Results not found!\"}}");}driver.Quit();}}}
<?phprequire_once('vendor/autoload.php');useFacebook\WebDriver\Remote\DesiredCapabilities;useFacebook\WebDriver\Remote\RemoteWebDriver;useFacebook\WebDriver\WebDriverBy;$caps=array("os_version"=>"9.0","device"=>"Google Pixel 3","app"=>"bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32");$driver=RemoteWebDriver::create("https://#YOUR_USERNAME:#YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",$caps);$driver->getTitle();# Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page matches 'Wikipedia'if($driver->getTitle()=="Wikipedia"){$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();?>
fromappiumimportwebdriverfromappium.webdriver.common.mobilebyimportMobileByfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdriver.supportimportexpected_conditionsasECimporttimedesired_cap={"os_version":"9.0","device":"Google Pixel 3","app":"bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32"}driver=webdriver.Remote(command_executor="https://#YOUR_USERNAME:#YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",desired_capabilities=desired_cap)search_element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID,"Search Wikipedia")))search_element.click()search_input=WebDriverWait(driver,30).until(EC.element_to_be_clickable((MobileBy.ID,"org.wikipedia.alpha:id/search_src_text")))search_input.send_keys("BrowserStack")time.sleep(5)# Setting the status of test as 'passed' or 'failed' based on the condition if results are found for the search
search_results=driver.find_elements_by_class_name("android.widget.TextView")if(len(search_results)>0):driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Results found!"}}')else:driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "No results found"}}')driver.quit()
require'rubygems'require'appium_lib'caps=Selenium::WebDriver::Remote::Capabilities.newcaps["os_version"]="9.0"caps["device"]="Google Pixel 3"caps["app"]="bs://5a96eae8cd61b93bb4bcdb287bbe3e9ba4f11a32"appium_driver=Appium::Driver.new({'caps'=>caps,'appium_lib'=>{:server_url=>"https://#YOUR_USERNAME:#YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub"}},true)driver=appium_driver.start_driverwait=Selenium::WebDriver::Wait.new(:timeout=>30)wait.until{driver.find_element(:accessibility_id,"Search Wikipedia").displayed?}element=driver.find_element(:accessibility_id,"Search Wikipedia")element.clickwait.until{driver.find_element(:id,"org.wikipedia.alpha:id/search_src_text").displayed?}search_box=driver.find_element(:id,"org.wikipedia.alpha:id/search_src_text")search_box.send_keys("BrowserStack")sleep5# Setting the status of test as 'passed' or 'failed' based on the condition if results are found for the searchresults=driver.find_elements(:class,"android.widget.TextView")ifresults.count>0driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Results found"}}')elsedriver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "No results found"}}')enddriver.quit
Mark test status after test has completed using REST API
You can also mark a test session’s status as passed or failed using our REST API. This is particularly useful when a test successfully completed on BrowserStack (i.e. Completed status) but there was a failed assertion in the test case.
The following arguments need to be passed in the REST API call for setting the status, and its corresponding reason:
status accepts either passed or failed as the value
reason accepts a value in string datatype. This can be useful to record a reason for test failure.
Here is a REST API request to update the status and reason for an example test session :
curl -u"YOUR_USERNAME:YOUR_ACCESS_KEY"\-X PUT "https://api-cloud.browserstack.com/app-automate/sessions/f97f02bf39d592f5fc349cee419294fdfb7593a2.json"\-H"Content-Type: application/json"\-d'{"status":"failed", "reason":"Element not found on the login page"}'
Character Limits
A maximum of 256 characters can be passed as input value for reason argument.
Escape sequences such as \n, \t, \b, \", \r are not accepted as valid input for reason argument.