简体   繁体   中英

java factory pattern issue

I am using selenium webdriver for testing and want to give the opportunity to select which browser to run each set of tests in. I have used a factory to achieve this but for some reason I can't seem to get it working. Here is what I have

class WebDriverFactory {

private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();

private WebDriverFactory() {
}

public static void setChromePath() {
// Set file path here 
}

public static void setIEPath() {
// Set file path here 
}

public static WebDriver getWebDriver(String type) {

System.out.println("choose a browser:");
Scanner scan = new Scanner(System.in);
scan.next();

if (type.equalsIgnoreCase("chrome")) {
  return createChrome();
} else if (type.equalsIgnoreCase("firefox")) {
  return createFirefox();
} else if (type.equalsIgnoreCase("IE")) {
  return createInternetExplorer();
} else {
  return null;
}


}

private static WebDriver createChrome() {

System.setProperty("webdriver.chrome.driver", "C:/Program Files          

(x86)/Google/Chrome/Application/chromedriver_win32_2.1/chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions chromeOptions = new ChromeOptions();
capabilities.setCapability("chrome.binary",    

"C:/AppData/Local/Google/Chrome/Application/chrome.exe");
 WebDriver driver = new ChromeDriver(capabilities);
 return driver;
}

private static WebDriver createFirefox() {

WebDriver driver = new FirefoxDriver();
return driver;
}

private static WebDriver createInternetExplorer() {

File file = new File("C:/Utils/IEDriverServer_Win32_2.33.0/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
return driver;
}
}

And in the test class I have:

String type = null;
WebDriverFactory.getWebDriver(type);

When I enter a string the scanner doesn't seem to do anything and the browser doesn't open? Could anyone help me out?

I now have this in the test class:

System.out.println("choose a browser:");
Scanner scan = new Scanner(System.in);
String type = scan.next();
WebDriverFactory.getWebDriver(type);

As Jayan mentioned, you should have got a NPE since you are passing null to the getWebDriver method. However, keeping that aside, in your code, you are reading a string using scan.next() statement, but you haven't assigned the read value to the variable type .

Saying that, your statement should be as below:

Scanner scan = new Scanner(System.in);
type = scan.next();

Apart from that, I don't see any use of passing type variable to the getWebDriver method, as you are anyhow, taking the input from user for type in getWebDriver method.

You should first correct the method getWebDriver(type) so it doesn't return null always! And then see where you go. As Mubin suggested, you could let go of the input parameter type if you are going to read the input inside the method. However, Based on the discussion in comments to this answer here's how the method getWebDriver(type) should look like:

public static WebDriver getWebDriver(String type) {

  if (type.equalsIgnoreCase("chrome")) {
    return createChrome();
  } else if (type.equalsIgnoreCase("firefox")) {
    return createFirefox();
  } else if (type.equalsIgnoreCase("IE")) {
    return createInternetExplorer();
  } else {
    return null;
  }

}

OP was passing null to this method in the test implementation, and also not assigning the value read by Scanner . It isn't clear what the OP was expecting. As Jayan mentioned, we should get an NullPointerException .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM