How to capture JavaScript errors with Selenium WebDriver using Java

荒凉一梦 提交于 2020-08-02 13:15:51

问题


I was wondering if there is a way to capture JavaScript errors on a page while running automated Selenium tests.


回答1:


There is logs Beta version in WebDriver

driver.manage().logs().get(LogType.BROWSER);

Will give you the console content.

Then you can filter it using Level

LogEntries entries = driver.manage().logs().get(LogType.BROWSER);
entries.filter(Level.SEVERE);



回答2:


And there is one that worked for me. Here it is.

    public boolean isThereJSErrorOnThePage() {
    Set<String> errorStrings = new HashSet<>();
    errorStrings.add("SyntaxError");
    errorStrings.add("EvalError");
    errorStrings.add("ReferenceError");
    errorStrings.add("RangeError");
    errorStrings.add("TypeError");
    errorStrings.add("URIError");
    LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
    for (LogEntry logEntry : logEntries) {
        for (String errorString : errorStrings) {
            if (logEntry.getMessage().contains(errorString)) {
                LOGGER.error("Java Script error has been detected:");
                LOGGER.error(new Date(logEntry.getTimestamp()) + " " + logEntry.getLevel() + " " + logEntry.getMessage());
                return true;
            }
        }
    }
    return false;
}

If it does not work out of a box, try to add capabilities:

DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
driver = new ChromeDriver(desiredCapabilities);



回答3:


you can try https://github.com/AutomatedOwl/chromedriver-js-errors-collector it captures all the js errors and attaches them to allure report.




回答4:


Try the below code for capturing the javascript error of webPage and let me know if it has helped you

import java.util.List;
import java.util.concurrent.TimeUnit;
import net.jsourcerer.webdriver.jserrorcollector.JavaScriptError;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class jsError {
 WebDriver driver;

 @BeforeTest
 public void setup() throws Exception {
  FirefoxProfile profile = new FirefoxProfile();
  JavaScriptError.addExtension(profile);
  driver = new FirefoxDriver(profile);
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2015/01/table-with-checkbox.html");
 }

 @Test
 public void printPageErrors() throws Exception {
  //Capture all errors and store them In array.
  List<JavaScriptError> Errors = JavaScriptError.readErrors(driver);
  System.out.println("Total No Of JavaScript Errors : " + Errors.size());
  //Print Javascript Errors one by one from array.
  for (int i = 0; i < Errors.size(); i++) {
   System.out.println("Error Message : "
     + Errors.get(i).getErrorMessage());
   System.out.println("Error Line No : "
     + Errors.get(i).getLineNumber());
   System.out.println(Errors.get(i).getSourceName());
   System.out.println();
  }
 }
}


来源:https://stackoverflow.com/questions/49506768/how-to-capture-javascript-errors-with-selenium-webdriver-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!