1
votes

I am trying to run my selenium-testng program using log4j.properites. i would like to know how to integrate my testng class with log4j.properties.

  1. I have create a sample google logintest using testNG
  2. I have created log4j.properties under src/main/resources
  3. I have written logger piece for code Logger log = Logger.getLogger(loginTest.class.getName());
  4. I tried running the test, but i couldnt see any log getting generated. Now I am not sure about the next steps - could someone please guide me.

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

    }

    Set level

    log4j.rootCategory=debug, console, file

    Appender which writes to console

    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 which writes to a file

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

    Defining maximum size of a log file

    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

1 Answers

6
votes

I believed you have log4j.jar in your build path.

You should have log4j.properties file in your src folder.Please check the configuration.

# 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

Here is your test :

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


    }

}

Output:

You can see the details in the console as well as D:\\Testlog.log which is mentioned in the log4j.properties file.

Hope this helps.