0

I am putting console errors/warnings from chrome browser page to java console, but what I need is to check for specific text that contains in java console information that I get, for example:

[2016-07-28T16:22:41+0400] [SEVERE] Uncaught ReferenceError: ***** is not defined
[2016-07-28T16:22:42+0400] [WARNING] ***************

Need to check something like this:

String logg = entry.toString(); Assert.assertTrue(logg.contains("[SEVERE]")); 

Would appreciate help

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.Assert;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;

 public class ALog {
private WebDriver driver;


@BeforeMethod
public void setUp() {
    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");        
    DesiredCapabilities caps = DesiredCapabilities.chrome();
    LoggingPreferences logPrefs = new LoggingPreferences();
    logPrefs.enable(LogType.BROWSER, Level.ALL);
    caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
    driver = new ChromeDriver(caps);
}

@AfterMethod
public void tearDown() {
    driver.quit();
}

public void analyzeLog() {


   LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);


     for (LogEntry entry : logEntries) {

         System.out.println(entry.toString());
         String logg = entry.toString(); // not working, just example of what I need
         Assert.assertTrue(logg.contains("[SEVERE]")); // not proper, just example of what I need



     }


     }



@Test
public void testMethod() {
    driver.get("type url to check with chrome console errors ");
           analyzeLog();

}
}
  • 2
    Possible duplicate of [Capturing browser logs with Selenium](http://stackoverflow.com/questions/25431380/capturing-browser-logs-with-selenium) – Jokab Jul 28 '16 at 13:52
  • No, its not duplicate, question is how to work with what I get from java console, for example how to check java console for specific text - its all about java, not selenium – Victor Wizz Jul 28 '16 at 14:03
  • Then why did you tag it with Selenium? You are working with a lot of Selenium functionality here. – Jokab Jul 28 '16 at 14:05
  • Shouldnt put selenium tag here, the question is about working with java console, selenium code is just for example, it doesnt matter – Victor Wizz Jul 28 '16 at 14:08

1 Answers1

0

Use the method LogEntry#getMessage() to read the output.

Jokab
  • 2,939
  • 1
  • 15
  • 26