我正在尝试使用log4j.properites运行我的硒测试程序。我想知道如何将我的testng类与log4j.properties集成。
我已经使用testNG创建了一个示例google logintest
我已经在src / main / resources下创建了log4j.properties
我为代码编写了记录器
记录器日志= Logger.getLogger(loginTest.class.getName());
我尝试运行测试,但看不到任何日志生成。
现在,我不确定接下来要采取的措施-有人可以指导我。
公共类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
最佳答案
我相信您的构建路径中有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
中查看详细信息。希望这可以帮助。
关于java - 如何将Log4j与TestNG集成?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55329209/