使用loggingSelenium示例:
packagecom.selenium.demo;
importcom.thoughtworks.selenium.*;
importcom.unitedinternet.portal.selenium.utils.logging.*;
importjava.io.*;
importjava.io.BufferedWriter;
importorg.apache.commons.lang.*;
importjava.util.Date;
importjava.text.SimpleDateFormat;
publicclassLogTestextendsSeleneseTestCase{
LoggingSeleniumselenium;
BufferedWriterloggingWriter;
Stringurl="http://mail.zzy.cn";
// Date now=new Date();
// SimpleDateFormat fnow=new SimpleDateFormat("yyyy-MM-dd HH_mm_ss");
// final String resultPath = "E:\\selenium\\SRC\\testDemo";
finalStringerrorLog="E:\\selenium\\SRC\\testDemo\\errorLog";
finalStringresultHtmlFileName=errorLog+ File.separator+ LoggingUtils.timeStampForFileName()+"result.html";
finalStringresultEncoding="UTF-8";
//覆盖setUp方法
publicvoidsetUp()throwsException {
if(!newFile(errorLog).exists())
{
newFile(errorLog).mkdirs();
}
loggingWriter= LoggingUtils.createWriter(resultHtmlFileName,resultEncoding);
LoggingResultsFormatter htmlFormatter =
newHtmlResultFormatter(loggingWriter,resultEncoding);
htmlFormatter.setScreenShotBaseUri("/");// this is for linking to the screenshots
htmlFormatter.setAutomaticScreenshotPath(errorLog);
// wrap HttpCommandProcessor from remote-control
LoggingCommandProcessor myProcessor =
newLoggingCommandProcessor(newHttpCommandProcessor("localhost", 4444,"*iexplore",url), htmlFormatter);
selenium=newLoggingDefaultSelenium(myProcessor);
selenium.start();
}
//覆盖teardown方法(在这里没有特别的设置)
publicvoidtearDown()throwsException {
selenium.stop();
try{
if(null!=loggingWriter) {
loggingWriter.close();
}
}catch(IOException e) {
// do nothing
}
}
//截屏
selenium.captureScreenshot(errorLog+ File.separator+"ErrorScreenshot_"+ LoggingUtils.timeStampForFileName()+".png");
注意:
使用loggingSelenium时,报错信息为:
java.lang.NoClassDefFoundError: org/apache/commons/lang/ArrayUtils
解决方案:从地址:http://commons.apache.org/lang/下载commons-lang-2.4-bin.zip
解压后,把commons-lang-2.4.jar导入项目中