Java, Selenium - Properties Reader Class - How to pass strings to another class

nosequeweaponer :

I have a doubt with the next case:

I have a config.properties file for reading and getting the information of the .property files. This is:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class FileReader {

    public Properties configProp;
    public Properties firstProps;

    public String propertiesFilePath1 = "src/main/resources/data/config.properties";
    public String propertiesFilePath2 = "src/main/resources/data/first.properties";

    public void ConfigProperties() throws IOException {


        // loadPropertyFile is a user defined method to locate multiple properties file
        Properties configProp = loadPropertyFile(propertiesFilePath1);
        Properties firstProps = loadPropertyFile(propertiesFilePath2);

        String browserName = configProp.getProperty("browser");
        String urlName = configProp.getProperty("url");

        // Access and Store values from Properties File to local variables (first.properties)
        String search = firstProps.getProperty("search");


        /*String searchValues = firstProps.getProperty("multipleSearchValues");
        // Convert input string in an array, based on any unique character
        String[] testArray = searchValues.split(",");*/
    }

    public static Properties loadPropertyFile(String filePath) throws IOException {
        // Loading Properties file
        File file=new File(filePath);
        FileInputStream fis=new FileInputStream(file);
        Properties prop=new Properties();
        prop.load(fis);

        return(prop);
    }
}

This is my config.properties:

browser = firefox
url = https://www.google.cl

I want to take the values of the class above for passing them to the next class:

    public class Browser extends FileReader {

    // Take the instance of WebDriver
    public WebDriver driver;

    public WebDriver initializeBrowser(){

        if(browser.equals("chrome")) {
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();

        } else if(browser.equals("firefox")) {
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();

        } else if(browser.equals("ie")) {
            WebDriverManager.iedriver().setup();
            driver = new InternetExplorerDriver();

        } else if(browser.equals("edge")) {
            WebDriverManager.edgedriver().setup();
            driver = new EdgeDriver();

        } else {
            System.setProperty("webdriver.safari.driver","/usr/bin/safaridriver");
            driver = new SafariDriver();
        }
        System.out.println("URL in use: "+ url);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        return driver;
    }

    public void getConfigFiles() throws IOException {
        driver.get(configProp.getProperty(browserName));
    }
}

In browserName it displays the next: Cannot resolve symbol 'browserName'

I don't know how to take them (browser and url) for passing them to Browser class.

Can anybody help me, please?

Villa_7 :

Firstly, you need to declare a Properties configProp variable global. Just put it right after class opening bracket before any method declaration like that:

public class FileReader {
public Properties configProp;
public String propertiesFilePath = "your path to this file";

   public void methodABC() {
      properties = new Properties();
      properties.load(new FileInputStream(propertiesFilePath));
      String browserName = properties.getProperty("browser");
     }
}

and in another class just like that:

public class ABC {

  void method123() {
     driver.get(properties.getProperty("url"));
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=171123&siteId=1