Skip to content

Instantly share code, notes, and snippets.

@sauceaaron
Last active December 2, 2019 19:59
Show Gist options
  • Save sauceaaron/9e8572a22262b2fd54efd952d2d8d817 to your computer and use it in GitHub Desktop.
Save sauceaaron/9e8572a22262b2fd54efd952d2d8d817 to your computer and use it in GitHub Desktop.
Getting browser log messages and javascript console log using Selenium Webdriver
import com.saucelabs.saucerest.SauceREST;
import org.junit.Test;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
public class BrowserLogTest
{
String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");
String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
@Test
public void hello() throws MalformedURLException
{
URL url = new URL("https://ondemand.saucelabs.com/wd/hub");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("username", SAUCE_USERNAME);
capabilities.setCapability("accessKey", SAUCE_ACCESS_KEY);
capabilities.setCapability("platform", "Windows 10");
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("version", "latest");
capabilities.setCapability("name", "Logging Test");
capabilities.setCapability("build", "troubleshooting");
capabilities.setCapability("extendedDebugging", true);
capabilities.setCapability("capturePerformance", true);
RemoteWebDriver driver = new RemoteWebDriver(url, capabilities);
String sessionId = driver.getSessionId().toString();
driver.get("https://saucelabs.com");
driver.getTitle();
// add some console logging output
driver.executeScript("console.log('hello javascript console log');");
driver.executeScript("console.log('another log entry');");
// get browser logs
LogEntries browserLogs = driver.manage().logs().get(LogType.BROWSER);
browserLogs.getAll().forEach(entry -> {
System.out.println("log: " + entry.toJson());
});
driver.quit();
SauceREST api = new SauceREST(SAUCE_USERNAME, SAUCE_ACCESS_KEY);
api.jobPassed(sessionId);
}
}
# set your relevant SAUCE_USERNAME, SAUCE_ACCESS_KEY, and SAUCE_SESSION_ID
curl https://$SAUCE_USERNAME:[email protected]/$SAUCE_SESSION_ID/console.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment