Wednesday, August 29, 2018

Selenium : How To Capture Js Errors Inwards Google Chrome

| Wednesday, August 29, 2018
Today nigh of the applications are using Java Scripts for customer side processing similar customer side validations, etc. So It Is rattling Important to that If whatever mistake related to Javascript Is non acquaint whatever page of application which tin disturb application functionality or user sense In whatever browser.
Doing this draw of piece of job volition taking lots of efforts equally y'all needs to become on every page 1 past times 1 too banking concern jibe If in that place Is whatever JS error. So nosotros demand only about automated solution which tin assistance us to relaxation our task.

If y'all remember, Earlier nosotros learnt how to capture page JavaScript error using JSErrorCollector In Firefox browser. Now let's attempt to produce same draw of piece of job but piffling dissimilar agency too using google chrome browser.

We volition utilisation logger Interface to collect mistake from page equally described In bellow given example. It volition collect Java Script Errors from spider web page too impress errors In console. Execute bellow given event In eclipse too banking concern jibe result.

package Testing_Pack;  import java.util.logging.Level;  import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; 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.BeforeMethod; import org.testng.annotations.Test;  world cast LogErrorInChrome {   WebDriver driver;   @BeforeMethod  world void setUp() {   System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");   DesiredCapabilities cap = DesiredCapabilities.chrome();    // Set logging preference In Google Chrome browser capability to log   // browser errors.   LoggingPreferences pref = novel LoggingPreferences();   pref.enable(LogType.BROWSER, Level.ALL);   cap.setCapability(CapabilityType.LOGGING_PREFS, pref);   driver = novel ChromeDriver(cap);  }   // Function to capture JSError log.  world void GetJSErrosLog() {   // Capture all JSerrors too impress In console.   LogEntries jserrors = driver.manage().logs().get(LogType.BROWSER);   for (LogEntry mistake : jserrors) {    System.out.println(error.getMessage());   }  }   @Test  world void testMethod() {   driver.get("aa/search?q=capturing-page-javascript-errors-using");   // Call GetJSErrosLog() to log too impress JSErrors In console.   GetJSErrosLog();  } }

Console outcome of inwards a higher house event volition looks similar bellow.



<< PREVIOUS || NEXT >>

Related Posts