標籤:部落格 sele blog throw testng tor als use body
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會將日誌列印到測試報告中。如果不知道具體方法的可以查看我之前的部落格。
java+Selenium+TestNg搭建自動化測試架構(2)實現跨瀏覽器功能