如何在Selenium WebDriver Automation中使用屬性文件作爲對象庫?如何在Selenium WebDriver Automation中使用屬性文件作爲對象庫?
我正在尋求有關設置和需要完成這些步驟的說明。
如何在Selenium WebDriver Automation中使用屬性文件作爲對象庫?如何在Selenium WebDriver Automation中使用屬性文件作爲對象庫?
我正在尋求有關設置和需要完成這些步驟的說明。
創建framework.properties文件和變量存儲以這種方式(下面是兩個定位器與樣本值)
locator1=username
locator2=password
用於裝載屬性文件創建一個類。您可以使用下面的代碼片段: 注:路徑/ src目錄/主/資源/ COM /架構/屬性/是一個樣本路徑,並且可以按照你自己的框架
public class PropertyManager {
private static final Properties PROPERTY = new Properties();
private static final String FRAMEWORKPROPERTIESPATH = "/src/main/resources/com/framework/properties/";
private static final Logger LOGGER = Logg.createLogger();
public static Properties loadPropertyFile(String propertyToLoad) {
try {
PROPERTY.load(new FileInputStream(System.getProperty("user.dir")
+ FRAMEWORKPROPERTIESPATH + propertyToLoad));
} catch (IOException io) {
LOGGER.info(
"IOException in the loadFrameworkPropertyFile() method of the PropertyManager class",
io);
Runtime.getRuntime().halt(0);
}
return PROPERTY;
}
}
改變。當你要訪問的變量屬性類,使用下面的代碼片段:
private static final Properties LOCATORPROPERTIES = PropertyManager
.loadPropertyFile("framework.properties");
public void click() {
driver.findElement(By.id(LOCATORPROPERTIES.getProperty("locator1")));
}
創建任何文件&保存它.properties擴展 例如 - 在Eclipse中添加新的文件,通過對項目點擊右鍵>新建>文件
添加以下數據config.properties文件並保存
Username = Jhon
Password = Qwerty123
下面寫代碼來訪問此文件
String filepath = "./config.properties" ; // Path of .properties file
File f = new File(filepath);
FileInputStream fs = new FileInputStream(f);
Properties pro = new Properties();
Pro.Load(fs);
pro.getProperty("Username"); // return value "Jhon" return type string
pro.getProperty("Password"); // retun value "Qwerty123" return type string
而且使用像 -
driver.findelement(By.id("user")).sendKeys(pro.getProperty("Username"));
driver.findelement(By.id("pass")).sendKeys(pro.getProperty("Password"));