如何在 Selenium 中使用属性文件在框架中声明全局变量?
我们可以借助使用 .properties 文件的 Properties 类在 Selenium 中声明全局变量。在 .properties 文件中,数据存储在键值对中。我们可以读取和写入 .properties 文件中的值。
示例
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Propert {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Propert t = new Propert();
t.login();
}
public void login() throws IOException {
Properties prop = new Properties();
FileInputStream ips = new FileInputStream(
"C:\Users\ghs6kor\eclipse- workspace\Inheritance\config.properties");
prop.load(ips);
// read from properties file with getProperty() method
if (prop.getProperty("browser").equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(prop.getProperty("url"));
FileOutputStream ops = new FileOutputStream(
"C:\Users\ghs6kor\eclipse- workspace\Inheritance\config.properties");
String urlnm = driver.getTitle();
// writing in the properties file with setProperty() method
prop.setProperty("title", urlnm);
prop.store(ops, null);
}
}
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP