一、Properties
1.读取properties文件
Properties properties = new Properties(); properties.load(new FileInputStream(propertyFileName));
2.读取.properties文件数据数据
Configuration config = config = new PropertiesConfiguration(); String.valueOf(config.getProperty(key));
3.获取Name
Enumeration<?> en = properties.propertyNames while (en.hasMoreElements()) { String key = (String) en.nextElement(); if (key.toLowerCase().equals(RETRYCOUNT)) { sRetryCount = properties.getProperty(key); } if (key.toLowerCase().equals(SOURCEDIR)) { sourceCodeDir = properties.getProperty(key); } if (key.toLowerCase().equals(SOURCEENCODING)) { sourceCodeEncoding = properties.getProperty(key); }
二、Logger
1.获得Logger对象
static Logger logger = Logger.getLogger(BaseParpare.class); logger.error("浏览器不能正常工作,请检查是不是被手动关闭或者其他原因",e); logger.info("当前操作系统是:[" + currentPlatform + "]"); logger.warn("注意:页面没有完全加载出来,刷新重试!!");
三、ITestContext:当前测试的上下文信息
String[] groups = context.getIncludedGroups();
@Override public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) { List<ITestResult> list = new ArrayList<ITestResult>(); for (ISuite suite : suites) { Map<String, ISuiteResult> suiteResults = suite.getResults(); for (ISuiteResult suiteResult : suiteResults.values()) { ITestContext testContext = suiteResult.getTestContext(); IResultMap passedTests = testContext.getPassedTests(); IResultMap failedTests = testContext.getFailedTests(); IResultMap skippedTests = testContext.getSkippedTests(); IResultMap failedConfig = testContext.getFailedConfigurations(); list.addAll(this.listTestResult(passedTests)); list.addAll(this.listTestResult(failedTests)); list.addAll(this.listTestResult(skippedTests)); list.addAll(this.listTestResult(failedConfig)); } } this.sort(list); this.outputResult(list, outputDirectory+"/test.txt"); }
从.xml获取参数数据
//从testng.xml文件中获取浏览器的属性值 String browserName = context.getCurrentXmlTest().getParameter("browserName");
<!-- chrome,firefox,ghost和ie不区分大小写 --> <parameter name="browserName" value="chrome" /> <!-- 页面元素10秒不出现超时时间 -->
context.setAttribute("SELENIUM_DRIVER", seleniumUtil.driver);//添加参数
四、从.properties文件获取数据
public class getProperties { public static void main(String[] args) throws FileNotFoundException, IOException { Properties pps = new Properties(); pps.load(new FileInputStream("Test.properties")); Enumeration enum1 = pps.propertyNames();//得到配置文件的名字 while(enum1.hasMoreElements()) { String strKey = (String) enum1.nextElement(); String strValue = pps.getProperty(strKey); System.out.println(strKey + "=" + strValue); } } }
name=JJ
Weight=4444
Height=3333
五、获取系统信息
Properties props = System.getProperties();//获得系统属性集 String currentPlatform = props.getProperty("os.name");//获得操作系统名称
六、执行Javascript命令
JavascriptExecutor js = (JavascriptExecutor)driver;
String status =(String)js.executeScript("return document.readyState");
七、Assert 断言
Assert.fail("浏览器driver没有获得对象,退出操作失败");
八、用Log4j实现日志输出
Log4j是开源的jar包,通过http://maven.ibiblio.org/maven/log4j/jars/下载最新版本
1、创建log4j.properties文件,详细配置说明:http://www.cnblogs.com/chenxqNo01/p/6370340.html;
https://www.cnblogs.com/signheart/p/3a41989df3a6a20c6592d6d03d3a75d1.html
log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC // 定义一个appender log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.TTCC=org.apache.log4j.RollingFileAppender log4j.appender.HTML=org.apache.log4j.FileAppender // 定义log文件保存路径,三种日志文件格式 log4j.appender.R.File=./Log/testlog.log log4j.appender.TTCC.File=./Log/testlog1.log log4j.appender.HTML.File=./Log/application.html // 定义layout和pattern log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout log4j.appender.TTCC.layout.DateFormat=ISO8601 log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout log4j.appender.HTML.layout.Title=Application log log4j.appender.HTML.layout.LocationInfo=true
代码:
public class TestBaidu { public static void main(String[] args) { Logger logger=Logger.getLogger("baidu"); PropertyConfigurator.configure(".\Log4j.properties"); // 打开浏览器 System.setProperty("webdriver.chrome.driver", ".\Tools\chromedriver.exe"); WebDriver driver = new ChromeDriver(); logger.info("启动浏览器"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); logger.info("隐式等待10秒"); driver.get("https://www.baidu.com"); logger.info("打开百度首页"); driver.findElement(By.id("kw")).sendKeys("Selenium"); logger.info("在搜索输入框输入selenium"); } }
九、TestNG实现日志的输出
通过Report.log("log message")这样的方式,给每个步骤添加日志解释,这些日志解释会显示在TestNG生成的html报告中。
package lessons; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Reporter; import org.testng.annotations.Test; public class TestngReportDemo { @Test public void testReport(){ WebDriver driver=new ChromeDriver(); Reporter.log("launch chrome browser instance"); driver.manage().window().maximize(); Reporter.log("Max the browser"); driver.get("https://www.baidu.com"); Reporter.log("open baidu site"); driver.quit(); Reporter.log("退出浏览器"); } }