Capturing JavaScript error in Selenium.


We can capture Javascript error in Selenium. This type of error appears at the Console Tab on opening the Developer tools in the browser. This can occur due to some functional issue in the page or due to extra logs which may cause performance issues.

We can handle the Javascript errors with the driver object and manage method.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
import java.util.ArrayList;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import java.util.logging.Level;
import java.util.Set;
public class JavascrptLogErs{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String u = "https://the−internet.herokuapp.com/javascript_error";
      driver.get(u);
      // maximize browser
      driver.manage().window().maximize();
      // to obtain browser errors
      Set<String> logtyp = driver.manage().logs().getAvailableLogTypes();
      for (String s : logtyp) {
         System.out.println(logtyp);
      }
      LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
      List<LogEntry> lg = logEntries.filter(Level.ALL);
      for(LogEntry logEntry : lg) {
         System.out.println(logEntry);
      }
      driver.quit();
   }
}

Updated on: 30-Nov-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements