java+Selenium+TestNg搭建自動化測試架構(2)實現跨瀏覽器功能

來源:互聯網
上載者:User

標籤:部落格   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)實現跨瀏覽器功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.