Selenium webdriver 如何在SeleniumWebDriver自动化中使用属性文件作为对象存储库?
如何在SeleniumWebDriver自动化中使用属性文件作为对象存储库Selenium webdriver 如何在SeleniumWebDriver自动化中使用属性文件作为对象存储库?,selenium-webdriver,Selenium Webdriver,如何在SeleniumWebDriver自动化中使用属性文件作为对象存储库 我正在寻求有关设置的说明以及实现此目的需要执行的步骤。创建一个framework.properties文件,并以这种方式存储变量(下面是两个带有示例值的定位器) 创建用于加载属性文件的类。您可以使用以下代码段: 注意:Path/src/main/resources/com/framework/properties/是一个示例路径,可能会根据您的框架进行更改 public class PropertyManager {
我正在寻求有关设置的说明以及实现此目的需要执行的步骤。创建一个framework.properties文件,并以这种方式存储变量(下面是两个带有示例值的定位器) 创建用于加载属性文件的类。您可以使用以下代码段: 注意:Path/src/main/resources/com/framework/properties/是一个示例路径,可能会根据您的框架进行更改
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扩展名保存 例如,通过右键单击project>new>file在eclipse中添加新文件 在config.properties文件中添加以下数据并保存 请编写以下代码以访问此文件
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
也使用like-
driver.findelement(By.id("user")).sendKeys(pro.getProperty("Username"));
driver.findelement(By.id("pass")).sendKeys(pro.getProperty("Password"));
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"));