為了方便管理測(cè)試數(shù)據(jù)确徙,需要將數(shù)據(jù)和腳本分離,獨(dú)立來管理數(shù)據(jù)烦却。其中一個(gè)方法我們可以通過讀取properties文件或是excel文件來讀取測(cè)試數(shù)據(jù)
1.properties文件內(nèi)容都是以鍵值對(duì)形式存在的宠叼,在工程文件下建立一個(gè)properties文件
2.創(chuàng)建讀取properties文件的java類
public class PropertyReader {
// 導(dǎo)入文件
private static Properties props = new Properties();
public static void init(String file) throws AssertionError{
try {
props.load(new FileInputStream(file));
} catch (Exception e) {
try {
props.load(props.getClass().getResourceAsStream(file));
} catch (FileNotFoundException e1) {
throw new AssertionError("File with locator's information not found: " + e.toString());
} catch (IOException e1) {
throw new AssertionError("IO error while trying to reach locator's information file: " + e.toString());
} catch (Exception ex) {
try {
props.load(Reflection.getCallerClass(3).getResourceAsStream(file));
} catch (Exception e1) {
throw new AssertionError("Unknown exception when calling throw Reflection: " + e1.toString());
}
}
}
}
// 獲取屬性值
public static String getProperty(String key) {
return props.getProperty(key);
}
public static Properties getProps() {
return props;
}
}
3.創(chuàng)建一個(gè)測(cè)試類讀取properties文件,利用參數(shù)傳值,運(yùn)行
public class test {
@Test
public void test(){
String path = System.getProperty("user.dir");
String file = path + "/src/test/java/Data/data.properties";
PropertyReader.init(file);
driver = SeleniumDriver.openBrowser("firefox", "http://www.epwk.us/");
Action.click(LoginPage.loginButton);
// 輸入賬號(hào)密碼登陸
Action.sendkeys(LoginPage.account, PropertyReader.getProperty("account"));
Action.sendkeys(LoginPage.password, PropertyReader.getProperty("password"));
Action.click(LoginPage.submintButton);
}
}
利用這個(gè)有個(gè)弊端就是每次傳入的值都要寫對(duì)應(yīng)的key冒冬,很繁瑣