如何使用java在Selenium webdriver中创建日志文件历史记录

svgewumm  于 2022-11-27  发布在  Java
关注(0)|答案(2)|浏览(184)

目前正在使用Selenium webdriver。我已经创建了一个日志文件,日志信息存储在logfile.log中。当我开始运行测试用例时,会捕获日志信息,但整个历史记录是可见的。我希望清除日志文件,或者需要在开始执行测试用例之前清除日志文件。
示例日志信息:

2013-10-01 09:18:06,655 INFO  [TEST] Initializing Selenium... 
2013-10-01 09:18:06,686 INFO  [TEST] Selenium instance started 
2013-10-01 09:18:11,905 INFO  [TEST] ______________________________________________________________ 
2013-10-01 09:18:11,905 INFO  [TEST] Initializing Selenium... 
2013-10-01 09:18:11,921 INFO  [TEST] Selenium instance started 
2013-10-01 09:18:11,921 INFO  [TEST] Accessing Stored uid,pwd from the stored text file 
2013-10-01 09:18:11,921 INFO  [TEST] Retrieved uid pwd from the text file 
2013-10-01 09:18:12,858 INFO  [TEST] Sign in to the OneReports website 
2013-10-01 09:18:12,874 INFO  [TEST] Enter Username 
2013-10-01 09:18:13,077 INFO  [TEST] Enter Password 
2013-10-01 09:18:14,155 INFO  [TEST] Submitting login details 
2013-10-01 09:18:33,139 INFO  [TEST] Stopping Selenium... 
2013-10-01 09:18:33,139 INFO  [TEST]  ______________________________________________________________
gudnpqoy

gudnpqoy1#

我希望这能对你有所帮助:
Automation Log for Selenium
步骤1-下载log4j jar文件
点击链接下载http://mirrors.ibiblio.org/pub/mirrors/maven/log4j/jars/log4j-1.2.15.jar
步骤2-将log4j添加到当前项目
选择您的项目〉右键单击〉单击构建路径〉单击配置构建路径〉转到库部分〉添加外部jar文件〉选择log4j〉单击保存
步骤3-打开记事本,复制下面的代码,并将文件保存为log4j.properties。该文件应该放在src目录下,这应该创建一个“默认包”。

// Here we have defined root logger
log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC

// Here we define the appender
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
log4j.appender.HTML=org.apache.log4j.FileAppender

// Here we define log file location
log4j.appender.R.File=./log/testlog.log
log4j.appender.TTCC.File=./log/testlog1.log
log4j.appender.HTML.File=./log/application.html

// Here we define the layout and pattern
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n
log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
log4j.appender.TTCC.layout.DateFormat=ISO8601
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Application log
log4j.appender.HTML.layout.LocationInfo=true

第4步-编写测试用例

import java.util.concurrent.TimeUnit;
import orgenter code here.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Google {
   public static void main(String[] args) {

    // Here we need to create logger instance so we need to pass Class name for 
    //which  we want to create log file in my case Google is classname
         Logger logger=Logger.getLogger("Google");

      // configure log4j properties file
       PropertyConfigurator.configure("Log4j.properties");
         // Open browser
        WebDriver driver = new FirefoxDriver();
        logger.info("Browser Opened");

        // Set implicit wait
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        logger.info("Implicit wait given");

        // Load application
     driver.get("https://www.google.co.in/");
     logger.info("Url opened");

       // type Selenium
       driver.findElement(By.name("q")).sendKeys("Selenium");
        logger.info("keyword type");           
    }
}

第5步-执行测试用例并验证输出和日志文件。这些日志将在项目文件夹中创建,并带有“log”标签

xzlaal3s

xzlaal3s2#

您可以只清除代码开头的日志文件:

PrintWriter writer = new PrintWriter(logfile);
writer.print("");
writer.close();

相关问题