Find JavaScript Errors Through Automation in Selenium

Posted By : Sakshi Dewan Gandhi | 24-Apr-2018

    First of All Let us Understand What are Javascript Errors:

Any Kind of Javascript Syntactical error is referred to as Javascipt Error. Nowadays, GUI Developers are using lot of heavy Javascript components in React JS, JQuery, Angular, Bootstrap, AJAX etc.In order to send the request from one browser to another these all types of components are used. So, there are high chances that there will be Javascript Errors which we can not see on Frontend, as there might be no visibility on the GUI, but they persist in the background.
Following are the type of JavaScript Errors:
1. Uncaught Type Error: Cannot read property.
2. Type Error: Undefined is not an Object.
3. Type Error: Null is not an object.
4. (Unknown): Script Error.
5.Type Error: Object doesn't support property.
6. Type Error: Undefined is not a function.
7.Uncaught Range Error: Max call stack.
8.Type Error: Cannot Read Property Length.
9. Uncaught Type Error: Cannot set Property.
10.ReferenceError : Event is not defined.
Following Line of code will be required to implement in order to fetch the above console errors via selenium:
Pre-requisites: Make a new Maven Project in Selenium and get all the Selenium Jars in POM.xml file.
package co.dashboard.login.basic;

import java.util.logging.Level;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestConsoleErrors {

public WebDriver driver;

@BeforeMethod
public void setup()
{
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
LoggingPreferences logginPreferences = new LoggingPreferences();
LoggingPreferences loggingpreferences;
loggingpreferences.enable(LogType.BROWSER, Level.ALL);
capabilities.setCapability(CapabilityType.LOGGING_PREFS, logginPreferences);
System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver");
driver = new chromeDriver(capabilities);

}

@AfterMethod
public void tearDown(){
driver.quit();
}
public void extractJSLogsInfo(){
LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
for(LogEntry entry : logEntries){
System.out.println(new Date(entry.getTimestamp() + "" + entry.getLevel() + "" + entry.getMessage()));
}
}

@Test
public void testMethod(){
driver.get("http://180.151.230.14/dashboard#/user");
extractJSLogsInfo(); 
}
}

Related Tags

About Author

Author Image
Sakshi Dewan Gandhi

Sakshi has experience into Testing, which comprises of Functional, Performance and End-to end-System Testing. She has worked in various domains like: Telecom, E-Commerce, E-Learning and Language Translation.

Request for Proposal

Name is required

Comment is required

Sending message..