如何在 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