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

Log contextual information using console annotations

Use console annotators to customize logs that you want to send to BrowserStack from within the test script for debugging or tracking.

You are currently viewing the documentation for the old dashboard. We’ve revamped the dashboard experience. Explore the new dashboard now!

In this guide, you will learn:

What are console annotators

Your test script contains contextual information related to the test, such as description of the test, start and end of scenarios, or any other data that you may want to print on the console for debugging and tracking. Using console annotations, you can add configurations in your tests that will log this information on BrowserStack. These logs are available on the Automate Dashboard along with the Selenium raw logs. These logs provide a way for you to quickly search and go to a specific section of the test and troubleshoot any failed tests.

After you run a test that includes console annotations, you can view the logs with these annotations on the Automate Dashboard as seen in the following example:

Image showing console annotation on the dashboard

Send logs to BrowserStack using JavascriptExecutor

You can send annotations to BrowserStack from within the test script using the annotate action implemented through the JavascriptExecutor as shown in the following snippets:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("browserstack_executor: {\"action\": \"annotate\", \"arguments\": {\"data\": \"<any string>\", \"level\": \"<info/warn/debug/error>\"}}");

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

  • data accepts a value in string data-type.
  • level accepts the standard log severity levels, namely info , debug , warn, and error . This argument is optional with the default value of info.

Searching and Filtering

After your test script has sent console annotations to BrowserStack, you can find all the annotations pushed to the logs in the Text Logs tab on the Automate dashboard.

You can either search with the logged text in the searchbar or directly navigate to the section of test execution where you want to troubleshoot. This search feature is particularly helpful when your test sessions run for a long duration.

Console Annotation searching

You can also filter these annotated logs based on the severity levels and customize the selection based on your logging patterns.

Console Annotation filtering

Sample code

The following code snippets show the annotation that is sent to BrowserStack when a product is added to the cart.

// Sample test in Java to run Automate session.
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.JavascriptExecutor;
import java.net.URL;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class JavaSample {
    public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
    public static final String AUTOMATE_ACCESS_KEY = "YOUR_ACCESS_KEY";
    public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "hub.browserstack.com/wd/hub";
    public static void main(String[] args) throws Exception {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
        browserstackOptions.put("osVersion", "14");
        browserstackOptions.put("deviceName", "iPhone 12");
        browserstackOptions.put("realMobile", "true");
        browserstackOptions.put("projectName", "BStack Sample Test");
        browserstackOptions.put("buildName", "BStack Build Number 1");
        capabilities.setCapability("bstack:options", browserstackOptions);


        final WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
        try {
            // logging start of test session at BrowserStack
            annotate("Add to cart","info",driver);
            driver.get("https://bstackdemo.com/");
            final WebDriverWait wait = new WebDriverWait(driver, 10);
            wait.until(ExpectedConditions.titleIs("StackDemo"));
            // getting name of the product
            String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'1\']/p"))).getText();
            // waiting for the Add to cart button to be clickable
            WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\'1\']/div[4]")));
            // clicking the 'Add to cart' button
            cart_btn.click();
            // checking if the Cart pane is visible
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content")));
            // getting the product's name added in the cart
            final String product_in_cart = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'__next\']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).getText();
            // logging variable for debugging purpose
            annotate("product in cart = " + product_in_cart,"debug",driver);
            // verifying whether the product added to cart is available in the cart
            if (product_name.equals(product_in_cart)) {
                markTestStatus("passed", "Product has been successfully added to the cart!", driver);
            }else{
                markTestStatus("failed", "Product has not successfully added to the cart!", driver);
                annotate("Product not found in cart. Test Failed" ,"error",driver);
            }
        } catch (Exception e) {
            markTestStatus("failed", "Some elements failed to load", driver);
        }
        driver.quit();
    }
    // This method accepts the status, reason and WebDriver instance and marks the test on BrowserStack
    public static void markTestStatus(String status, String reason, WebDriver driver) {
        final JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \""+ status + "\", \"reason\": \"" + reason + "\"}}");
    }

    public static void annotate(String data, String level, WebDriver driver) {
        final JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("browserstack_executor: {\"action\": \"annotate\", \"arguments\": {\"data\": \""+ data + "\", \"level\": \"" + level + "\"}}");
    }
} 

Check our list of various JavaScript Executors that BrowserStack offers.

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