环境: 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) { } } }
答案 0 :(得分:1)
BaseTest.java中的日志变量未分配logger类(https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Logger.html)的对象。由于没有将对象分配给变量日志,因此会抛出NULL指针异常。所以请将logger类的对象创建为可变日志