How to use the Properties file to declare global variables in a framework in Selenium?

Software TestingAutomation TestingSelenium Web Driver

We can declare global variables in Selenium with the help of the Properties class which works with .properties file. In the .properties file, the data is stored in key value pairs. We can read and write values in the .properties file.

Eaxmple

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);
      }
   }
}
raja
Published on 11-Jun-2020 15:29:39
Advertisements