1.切换浏览器类:其中包含了切换浏览器的方法,以及关闭浏览器,设置等待时间,以及重写的断言方法

package com.rrx.framework;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.Parameters;

public class BorwserEngin {
public String browserName;
public String URL;
public WebDriver driver;

public void initConfigDate() throws IOException {
browserName = PropertiesEngine.getProperties("browserName");
URL = PropertiesEngine.getProperties("URL");
//System.out.println(browserName);
}
@Parameters("Browser")
public WebDriver getDriver() throws IOException {
initConfigDate();
Logger.getLogger().info("浏览器名称"+browserName);
Logger.getLogger().info("URL:"+URL);
System.out.println(browserName);
if (browserName.equalsIgnoreCase("Firefox")) {
System.setProperty("webdriver.firefox.bin", "");
driver = new FirefoxDriver();
} else if (browserName.equalsIgnoreCase("Chrome")) {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\Administrator\\workspace\\SeleniumKuangJia\\driver\\chromedriver.exe");
driver = new ChromeDriver();
} else if (browserName.equalsIgnoreCase("IE")) {
System.setProperty("webdriver.ie.driver", "");
driver = new InternetExplorerDriver();
}
driver.get(URL);
return driver;
}

/**
* //关闭浏览器并且推出
*/
public void tearDown() {
driver.quit();
}
/**
* 隐式时间等待方法
* imlicitlyWait是隐式等待,一般在查找元素的时候使用。例如,我设置一个查找元素最大时间为10秒,使用了
imlicitlyWait后,如果第一次没有找到元素,会在10秒之内不断循环去找元素,知道超过10秒,报超时错误。
*/
public void callTime(int time){

driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
}

/**
* 断言方法
*/
public static void assertEqual(Object object,Object object2){
try {
Assert.assertEquals(object, object2,"不相同");
} catch (Error e) {

Reporter.log(""+e);//断言错误是把日志打印在测试报告中
Logger.getLogger().info(e);//打印在logger日志中

}

}
}

2.日志的输出采用long4j和Reporter两种方法。Reporter会将日志打印到测试报告中。如果不知道具体方法的可以查看我之前的博客。

 

posted on 2017-12-25 15:55  小李卫  阅读(483)  评论(0编辑  收藏  举报