Skip to end of metadata
Go to start of metadata

The JavascriptExecutor for Selenium lets you use JavaScript commands in your test scripts to perform actions in the browser you're using for your test. Sauce Labs has developed a set of custom JavascriptExecutor methods that you can use to annotate your tests, set their pass/fail status, and add information to the log of your Selenium commands to use in debugging.

Basic Example

((JavascriptExecutor)driver).executeScript("sauce:job-name=My test"); 

Methods

MethodDescription
"sauce:job-result=passed"
Sets the pass/fail status of the job. Valid options are passed, failed, true, and false. True means passed and false means failed.
"sauce:job-name=My awesome job"
Sets the job name
"sauce:job-tags=tag1,tag2,tag3"
Sets the job tags in a comma-separated list.
"sauce:job-build=mybuild123"
Sets the job’s build name.
"sauce: stop network"
"sauce: start network"

Stops/re-starts the VM’s network connection. A space must be included between sauce: and stop or start.

For Mac OSX only.

"sauce: break"
Puts a Sauce breakpoint in the test. Test execution will pause at this point, waiting for manual control by clicking in the test’s live video.  A space must be included between sauce: and break.

"sauce:context=This line appears in the command list as 'info'"

Logs the given line in the job’s Selenium commands list. No spaces can be between sauce: and context.

"sauce: job-info={'build':'mybuild', 'name':'my test name', 'public':'team'}"

Sets one or more job information fields to the values sent in the JSON-formatted dictionary.

Setting Pass/Fail

Setting the pass/fail status of your tests is important for getting the most out of your analytics. Selenium has three built-in states it recognizes for tests: In ProgressErrorComplete. Whether or not the application you were testing passed or failed the test you ran against it has to be set after the test completes. You can set the pass/fail status by means of the REST API after the test has run, by using a test framework, or by means of the sauce:job-result method.

Code Example

This code is from a basic example of a Java script using the TestNG framework, with the full script available from our Test Framework repository in GitHub

/**
     * Method that gets invoked after test.
     * Dumps browser log and
     * Closes the browser
     */

    @AfterMethod

    public void tearDown(ITestResult result) throws Exception {

        //Gets browser logs if available.
        ((JavascriptExecutor) webDriver.get()).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
        webDriver.get().quit();
    }

Providing Context for Selenium Commands

One of the most difficult aspects of troubleshooting Selenium tests is being able to match Selenium commands to browser actions. The sauce:context method provides you with a way to inject text into the command log to associate with a specific command. For example, in the command log on the left, it's difficult to immediately identify which commands are responsible for following a link to the page, which ones submitted a comment, and which ones asserted that the comment was the required text. In the example on the right, each set of commands has been provided with a context so you can easily track the execution of your test without having to watch the video. 

Code Example

The code examples shown here are from the Java-TestNG-Selenium framework example available from our GitHub repository

In a TestBase.java script, create a method so that as the test script executes, the annotation associated with each step of the test is set using the sauce:context method. 

/**
     * Method that gets invoked after test.
     * Dumps browser log and
     * Closes the browser
     */

    @AfterMethod

    public void tearDown(ITestResult result) throws Exception {

        //Gets browser logs if available.
        ((JavascriptExecutor) webDriver.get()).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
        webDriver.get().quit();
    }

    protected void annotate(String text) {
        ((JavascriptExecutor) webDriver.get()).executeScript("sauce:context=" + text);
    }
}

In your test script, in this example TextInputTest.java, include the text annotation you want associated with each step of the test. 

public class TextInputTest extends TestBase {

    /**
     * Runs a simple test verifying if the comment input is functional.
     * @throws InvalidElementStateException
     */

    @org.testng.annotations.Test(dataProvider = "hardCodedBrowsers")
    public void verifyCommentInputTest(String browser, String version, String os, Method method)
            throws MalformedURLException, InvalidElementStateException, UnexpectedException {
        this.createDriver(browser, version, os, method.getName());
        WebDriver driver = this.getWebDriver();
 

        String commentInputText = UUID.randomUUID().toString();
        this.annotate("Visiting GuineaPig page...");
        GuineaPigPage page = GuineaPigPage.visitPage(driver);


        this.annotate(String.format("Submitting comment: \"%s\"", commentInputText));
        page.submitComment(commentInputText);

        this.annotate(String.format("Asserting submitted comment is: \"%s\"", commentInputText));
        Assert.assertTrue(page.getSubmittedCommentText().contains(commentInputText));

    }