我正在开发一个 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");
}
}
页面类:
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?