Selenium Grid Java DevTools Listen Javascript Exceptions Example

This selenium grid java DevTools example will listen to Javascript exceptions.

Example DevTools Listen Javascript Exceptions

public class test_dev_tools_listen_javascript_exceptions {

	private static WebDriver driver;


	//NOTE: find these credentials in your Gridlastic dashboard after launching your selenium grid (get a free account).
	public static String VIDEO_URL = System.getenv("VIDEO_URL"); //like https://s3-us-west-1.amazonaws.com/027a15f2-530d-31e5-f8cc-7ceaf6355377/239a51a9-c526-ceb8-9ffd-1759b782a464/play.html?
	public static String HUB_URL = System.getenv("HUB_URL"); // like "https://USERNAME:ACCESS_KEY@YOUR_SUBDOMAIN.gridlastic.com/wd/hub";


	public static void main(String[] args) throws Exception {


		// Example test environment. NOTE: Gridlastic auto scaling requires all
		// these 3 environment variables in each request.
		String platform_name = "win10"; // win10, linux
		String browser_version = "latest"; // like 99 or latest


		// chrome, MicrosoftEdge
		ChromeOptions options = new ChromeOptions();
		//EdgeOptions options = new EdgeOptions();			

		options.setCapability("browserVersion", browser_version); 
		options.setCapability("platformName", platform_name);

		// Enable video recording
		Map gridlasticOptions = new HashMap<>();
		gridlasticOptions.put("video", "true");
		options.setCapability("gridlastic:options", gridlasticOptions);

		// On Linux start-maximized does not expand browser window to max screen size. Always set a window size.
		if (platform_name.equalsIgnoreCase("linux")) {
			options.addArguments(Arrays.asList("--window-position=0,0"));
			options.addArguments(Arrays.asList("--window-size=1920,1080"));
		} else {
			options.addArguments(Arrays.asList("--start-maximized"));
		}


		ClientConfig config = ClientConfig.defaultConfig().readTimeout(Duration.ofMinutes(10));
		driver = RemoteWebDriver.builder().address(new URL(HUB_URL)).oneOf(options).config(config).build();


		// TEST test_dev_tools_listen_javaScript_exceptions
		SessionId sessionId = null;
		try {
			sessionId = ((RemoteWebDriver) driver).getSessionId();
			DevTools devTools = ((HasDevTools) driver).getDevTools();			
			devTools.createSession();
			List jsExceptionsList = new ArrayList<>();
			Consumer addEntry = jsExceptionsList::add;
			devTools.getDomains().events().addJavascriptExceptionListener(addEntry);
			driver.get("https://www.gridlastic.com/?demo");
			WebElement link2click = driver.findElement(By.linkText("HOW IT WORKS"));
			((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);",
					link2click, "onclick", "throw new Error('Hello, world!')");
			link2click.click();
			for (JavascriptException jsException : jsExceptionsList) {
				System.out.println("JS exception message: " + jsException.getMessage());
				System.out.println("JS exception system information: " + jsException.getSystemInformation());
				jsException.printStackTrace();
			}
			devTools.close();
			System.out.println("Test Video: " + VIDEO_URL + sessionId.toString());
			driver.quit();
		} catch (Exception e) {
			e.printStackTrace();
			driver.quit();
			System.out.println("Test Video: " + VIDEO_URL + sessionId.toString());
		}

		
	}
}