How To Catch Javascript Errors through Selenium - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Friday, 30 March 2018

How To Catch Javascript Errors through Selenium

 // Fetch console logs
        LogEntries logs = driver.manage().logs().get(LogType.BROWSER);
        for (LogEntry entry : logs) {
            if (entry.getLevel() == Level.SEVERE) {
                System.out.println("JS Error: " + entry.getMessage());
            }
        }

 We need to download JAR File from here

https://github.com/mguillem/JSErrorCollector/blob/master/dist/JSErrorCollector-0.6.jar



package mypackage;

import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import net.jsourcerer.webdriver.jserrorcollector.JavaScriptError;

public class JSError {

String baseUrl = "";
WebDriver driver;

@BeforeTest
public void setUp() throws Exception {
FirefoxProfile ffProfile = new FirefoxProfile();
JavaScriptError.addExtension(ffProfile);
driver = new FirefoxDriver(ffProfile);
baseUrl = "https://www.quikr.com/";
}

@Test
public void contentTitle() throws Exception {
driver.get(baseUrl + "/");
Thread.sleep(5000);
}

@AfterTest
public void tearDown() throws Exception {
List jsErrors = JavaScriptError.readErrors(driver);
System.out.println("——————START displaying JS errors——————");
for (int i = 0; i < jsErrors.size(); i++) {
System.out.println(((JavaScriptError) jsErrors.get(i)).getErrorMessage());
System.out.println("Errorine: "+ ((JavaScriptError) jsErrors.get(i)).getLineNumber());
System.out.println(((JavaScriptError) jsErrors.get(i)).getSourceName());
System.out.println("\n");
}
System.out.println("——————STOPlaying JS errors———————");
//driver.close();
//driver.quit();
}
}


Output

——————START displaying JS errors——————
mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
Errorine: 1
https://teja8.kuikr.com/public/mon/qap/4.8.0/image.template.js


TypeError: $(...).DataTable is not a function
Errorine: 2723
https://www.quikr.com//


mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
Errorine: 1
https://tpc.googlesyndication.com/pagead/js/r20171011/r20110914/activeview/osd_listener.js


SyntaxError: missing ; before statement
Errorine: 1
https://teja8.kuikr.com/js/im/chatpnp.1507869060.js


mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
Errorine: 1
https://tpc.googlesyndication.com/pagead/js/r20171011/r20110914/activeview/osd_listener.js


mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
Errorine: 1
https://tpc.googlesyndication.com/pagead/js/r20171011/r20110914/activeview/osd_listener.js


mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
Errorine: 1
https://pagead2.googlesyndication.com/pagead/show_ads.js


mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
Errorine: 15
https://googleads.g.doubleclick.net/pagead/html/r20171011/r20170110/zrt_lookup.html#


mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
Errorine: 1
https://pagead2.googlesyndication.com/pagead/js/r20171011/r20170110/show_ads_impl.js


mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
Errorine: 1
https://tpc.googlesyndication.com/pagead/js/r20171011/r20110914/abg.js


——————STOPlaying JS errors———————
PASSED: contentTitle

No comments:

Post a Comment