Java, Selenium - Свойства Reader Class - Как передать строки в другой класс - PullRequest
0 голосов
/ 08 марта 2020

У меня есть сомнения по поводу следующего случая:

У меня есть файл config.properties для чтения и получения информации о файлах .property. Это:

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);
    }
}

Это мой config.properties:

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

Я хочу взять значения класса выше для передачи их следующему классу:

    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));
    }
}

В browserName отображается следующее: Не удается разрешить символ 'browserName'

Я не знаю, как получить их (browser и url) для передачи их в класс Browser .

Кто-нибудь может мне помочь, пожалуйста?

1 Ответ

1 голос
/ 08 марта 2020

Во-первых, вам нужно объявить глобальную переменную Properties configProp. Просто поместите его сразу после открывающей скобки класса перед любым объявлением метода, подобным этому:

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");
     }
}

, а в другом классе просто так:

public class ABC {

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