我正在研究selenium webdriver的自学项目,并使用log4j进行日志记录。 有一个测试类 - 包含所有测试用例作为方法 有一个页面类 - 包含测试类
可以使用的所有Web元素和方法我应该如何使用log4j? 测试类:
public class ConfiguringPropertiesFile {
private WebDriver driver;
private String baseUrl;
static Logger log = Logger.getLogger(ConfiguringPropertiesFile.class);
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "https://www.some-website.com/";
// Maximize the browser's window
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void test() {
PropertyConfigurator.configure("log4j.properties");
driver.get(baseUrl);
SearchPage.navigateToFlightsTab(driver);
log.info("Navigate to flights tab");
SearchPage.fillOriginTextBox(driver, "New York");
SearchPage.destinationTextBox(driver).sendKeys("Chicago");
log.info("Enter destination city");
SearchPage.departureDateTextBox(driver).sendKeys("12/25/2014");
log.info("Enter departure date");
SearchPage.returnDateTextBox(driver).sendKeys("12/31/2014");
log.info("Enter return date");
}
}
Page Class:
public class SearchPage {
public static WebElement element = null;
static Logger log1 = Logger.getLogger(SearchPage.class);
/**
* Returns the flight origin text box element
* @param driver
* @return
*/
public static WebElement originTextBox(WebDriver driver) {
element = driver.findElement(By.id("flight-origin"));
return element;
}
public static void fillOriginTextBox(WebDriver driver, String origin) {
PropertyConfigurator.configure("log4j.properties");
element = originTextBox(driver);
element.sendKeys(origin);
log1.info("Entering the source city as " + origin);
}
/**
* Returns the flight destination text box element
* @param driver
* @return
*/
public static WebElement destinationTextBox(WebDriver driver) {
element = driver.findElement(By.id("flight-destination"));
return element;
}
}
在这种情况下,我正在两个类中初始化log4j,然后最大的问题是我必须在每个方法中调用PropertyConfigurator。
如何以更好的方式初始化它,而不必每次都调用PropertyConfigurator?
答案 0 :(得分:2)
您可能希望拥有一个类,您可以在其中初始化log4j并从该类调用方法以获取每个其他类中的log4j实例。在下面的示例中,您可以调用createLogger()方法从任何其他类获取Log4j的实例,例如使用:private Logger log = Logg.createLogger();
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Logg {
private static Logger _logger;
private static final String fileName = "defaultlog";
private static final String dateAndTimeFormat = "MM-dd-yyyy_hh.mm.ss";
private static final String logProperttFilePath = "./src/main/resources/com/framework/properties/log4j.properties";
static {
/**
* This is the static block which appends the log file name with the
* timestamp to make it unique
*/
try {
String dateTime = DateAndTime
.getFormattedCurrentDateAndTime(dateAndTimeFormat);
String FileName = fileName + "-" + dateTime + ".log";
File file = new File("logs/" + FileName);
if (file.createNewFile()) {
Properties props = new Properties();
props.load(new FileInputStream(logProperttFilePath));
props.setProperty("log4j.appender.File.File", "logs/"
+ FileName);
LogManager.resetConfiguration();
PropertyConfigurator.configure(props);
System.out.println("Property log4j.appender.File.File = logs/"
+ FileName);
}
} catch (IOException ex) {
ex.printStackTrace();
System.out.print("IO Exception in static method of Logger Class. "
+ ex.getMessage());
System.exit(-1);
}
}
/**
* This method creates instance of the Logger class coming from log4j jar by
* implementing a singelton
*
* @return _logger - new instance if no instance exist else an existing
* instance if the method is invoked previously
*/
public static Logger createLogger() {
if (_logger == null) {
_logger = LogManager.getLogger(Logg.class);
return _logger;
} else
return _logger;
}
}