[英]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. 我正在使用Selenium Webdriver进行测试,并希望有机会选择在其中运行每个测试集的浏览器。我曾经使用工厂来实现这一点,但是由于某种原因,我似乎无法使其正常运行。 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. 如Jayan所述,您应该有一个NPE,因为您
getWebDriver
null传递给getWebDriver
方法。 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
. 但是,除此之外,在代码中,您正在使用
scan.next()
语句读取字符串,但尚未将读取值分配给变量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. 除此之外,我看不到将
type
变量传递给getWebDriver
方法的任何用法,无论如何,还是将用户的type
作为getWebDriver
方法的type
。
You should first correct the method getWebDriver(type)
so it doesn't return null always! 您应该首先更正方法
getWebDriver(type)
以便它不会始终返回null! 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. 正如Mubin所建议的,如果要在方法内部读取输入,则可以放弃输入参数
type
。 However, Based on the discussion in comments to this answer here's how the method getWebDriver(type)
should look like: 但是,基于对该答案的注释中的讨论,此处是
getWebDriver(type)
方法的外观:
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
. OP在测试实现中将null传递给此方法,并且未分配
Scanner
读取的值。 It isn't clear what the OP was expecting. 目前尚不清楚OP在期待什么。 As Jayan mentioned, we should get an
NullPointerException
. 如Jayan所述,我们应该获得
NullPointerException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.