如何检查java控制台的特定文本存在,例如

时间:2016-07-28 13:47:40

标签: java console

我将Chrome浏览器页面中的控制台错误/警告放到java控制台,但我需要检查我得到的java控制台信息中包含的特定文本,例如:

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

需要检查以下内容:

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

非常感谢帮助

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();

}
}

1 个答案:

答案 0 :(得分:0)

使用方法LogEntry#getMessage()读取输出。