Home > Enterprise >  How prop.getProperty("browser") present in initialization() will work?
How prop.getProperty("browser") present in initialization() will work?

Time:07-07

static WebDriver driver;
static Properties prop;

public TestBase(){
try {
prop=new properties();
FileInputStream ip = new FileInputStream("/users/Christain/Documents/workspace" "/qa/config/config.properties");
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTruce();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void initialization(){
String browserName = prop.getProperty("browser");

if(browserName.equals("chrome")){
System.setProperty("webdriver.chrome.driver", "/User/Christain/Downloads/chromedriver");
driver=new ChromeDriver();
}
else if(browserName.equals("FF")){
System.setProperty("webdriver.gecko.driver", "/User/Christain/Dwonloads/geckodriver");
driver=new ForefoxDriver();
}

If prop.load(ip) present in TestBase(), then how prop.getProperty("browser") present in initialization() will work?

I mean prop.load(ip) know where config file is and it's range not outside TestBase(), then how prop.getProperty("browser") present in initialization() will work?

CodePudding user response:

This code can work but it's really bad and should be fixed. This code requires that you create an instance of TestBase, and that act will initialize the static variable prop. This is valid java but idiotic: That means invoking initialization(), a static method, will throw a NullPointerException unless you invoke the constructor once. If you invoke the constructor more than once, it keeps re-loading that properties file for no good reason.

The exception handling is also deplorable (the proper "I do not want to deal with it" catch block is throw new RuntimeException("uncaught", e), not e.printStackTrace(), and the catch of FileNotFoundException should be deleted, given that the IOException already deals with it; FNFEx is a subtype of IOEx).

In general static should be avoided, but if somehow its appropriate here (doubtful, but not enough context to be sure), such a setup should look like either:

class TestBase {
  private static final TestBase INSTANCE = new TestBase();
  private final Properties prop;

  private TestBase() {
    prop = new Properties();
    try {
      try (var ip = new FileInputStream("/users/Christain/Documents/workspace/qa/config/config.properties")) {
        prop.load(ip);
      }
    } catch (IOException e) {
      throw new RuntimeException("uncaught", e);
    }
  }

  public static TestBase get() {
    return INSTANCE;
  }

  public void init() {
    String browserName = ....;
    .. code as written here ..
  }
}

and where you currently have TestBase.initialization(), you should have TestBase.get().initialization() instead.

Alternatively:

public class TestBase {
  private static Properties properties;

  private static void ensureProperties() {
    if (properties != null) return;
    Properties p = new Properties();
    try {
      try (var ip = new FileInputStream("/users/Christain/Documents/workspace/qa/config/config.properties")) {
        prop.load(ip);
      }
    } catch (IOException e) {
      throw new RuntimeException("uncaught", e);
    }
    properties = p;
  }

  public static void initialization() {
    ensureProperties();
    String browserName = ....;
    ... code as written here ....
  }
}
  • Related