Reputation: 143
Environment: Java, Selenium webdriver, Maven, testNG, Log4J, Eclipse
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="TestAll" parallel = "tests" thread-count = "2"> <test name="postivelogintest_IE"> <parameter name="browser" value="ie"/> <classes> <class name="com.dice.LoginTest"> <methods> <include name="DataDrivenpositiveLoginTest"/> </methods> </class> </classes> </test> <test name="postivelogintest_CH"> <parameter name="browser" value="ch"/> <classes> <class name="com.dice.LoginTest"> <methods> <include name="DataDrivenpositiveLoginTest"/> </methods> </class> </classes> </test> </suite>
package com.diceBase; import org.apache.log4j.Logger; import org.openqa.selenium.WebDriver; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Parameters; public class BaseTest { protected WebDriver driver; protected Logger log; @Parameters ({"browser"}) @BeforeMethod protected void MethodSetup(String browser){ log.info("method set up"); // line 16 driver = BrowserFactory.getDriver(browser); } @AfterMethod protected void TearDown(){ log.info("method tear down"); try { Thread.sleep(5000); driver.quit(); } catch (Exception e) { } } }
I added log4j.properties under src/main/resources.
In the BaseTest.java, I added two lines after importing log4j.
log.info("method set up");
log.info("method tear down");
iMy goal is to be able to use log.info entire project. Before that, I would like to test it by only importing log4j logger in basetest class to see if it works. If it works then I can import log4j in entire project.
I get error if I keep both log messages. But, If I remove both log messages, script passes. How can I print the logs using log4j?
Upvotes: 2
Views: 1988
Reputation: 9
The log variable in your BaseTest.java has not assigned with object of logger class (https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Logger.html) . Since No object is assigned to the variable log, it throws NULL pointer exception. So please create object of logger class to varaiable log
Upvotes: 1