如何将Log4j与TestNG集成?

时间:2019-03-24 22:29:54

标签: java selenium log4j testng

我正在尝试使用log4j.properites运行我的硒测试程序。我想知道如何将我的testng类与log4j.properties集成。

  1. 我已经使用testNG创建了一个示例Google登录测试
  2. 我已经在src / main / resources下创建了log4j.properties
  3. 我已经为代码编写了记录器 记录器日志= Logger.getLogger(loginTest.class.getName());
  4. 我尝试运行测试,但看不到任何日志生成。 现在,我不确定下一步要怎么做-有人可以指导我吗?

    公共类loginTest {

    WebDriver driver;
    Logger log = Logger.getLogger(loginTest.class.getName());
    
    
    @BeforeMethod
    public void setUp() {
        System.setProperty("webdriver.chrome.driver" , "C:/Users/User/Desktop/Selenium Drivers/chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }
    
    
    @Test
    public void titleTest() {
        String title = driver.getTitle();
        System.out.println(title);
        Assert.assertEquals(title, "Google");
    }
    
    @Test
    public void gmailLogoTest() {
        boolean b = driver.findElement(By.linkText("Gmail")).isDisplayed();
        Assert.assertTrue(true);
    }
    
    @AfterMethod
    public void tearDown() {
        driver.quit();
    
    
    }
    

    }

    设置级别

    log4j.rootCategory =调试,控制台,文件

    写入控制台的Appender

    log4j.appender.console = org.apache.log4j.ConsoleAppender
    log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern =%d {MM-dd-yyyy HH:mm:ss}%F%-5p [%t]%c {2}%L-%m%n

    写入文件的Appender

    log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File = application.log

    定义日志文件的最大大小

    log4j.appender.file.MaxFileSize = 10mb log4j.appender.file.MaxBackupIndex = 10 log4j.appender.file.layout = org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern =%d {ISO8601}%5p [%t]%c {1}:%L-%m%n log4j.appender.file.Append = true

1 个答案:

答案 0 :(得分:3)

我相信您的构建路径中有log4j.jar

您的log4j.properties文件夹中应该有src文件。请检查配置。

# Root logger option
log4j.rootLogger=DEBUG, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\Testlog.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

这是您的考试:

package testpkg;

import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;

public class loginTest {
WebDriver driver;
Logger log = Logger.getLogger(loginTest.class.getName());

  @BeforeMethod
  public void setUp() {
        //System.setProperty("webdriver.chrome.driver" , "C:/Users/User/Desktop/Selenium Drivers/chromedriver.exe");
      System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + File.separator + "\\Executables\\Chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        log.debug("opening webiste");
    }

  @Test
  public void titleTest() {
        String title = driver.getTitle();
        System.out.println(title);
        Assert.assertEquals(title, "Google");
        log.debug("Title match");
    }

  @Test
  public void gmailLogoTest() {
        boolean b = driver.findElement(By.linkText("Gmail")).isDisplayed();
        Assert.assertTrue(true);
        log.debug("Linked found");

    }

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


    }

}

输出:

您可以在控制台中查看详细信息,以及D:\\Testlog.log文件中提到的log4j.properties

希望这会有所帮助。