我使用硒webdriver進行測試,並希望有機會選擇運行每一組測試的瀏覽器。我用一個工廠來實現這一點,但出於某種原因,我似乎無法讓它工作。以下是我有java工廠模式問題
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;
}
}
而在測試類我:
String type = null;
WebDriverFactory.getWebDriver(type);
當我輸入一個字符串掃描儀似乎並沒有做任何事情,並且瀏覽器無法打開?任何人都可以幫我嗎?
我現在有這個測試類:
System.out.println("choose a browser:");
Scanner scan = new Scanner(System.in);
String type = scan.next();
WebDriverFactory.getWebDriver(type);
調用getWebDriver(null)應該讓你NPE?比較 – Jayan
可以把恆定的字符串先放入,但是在我用我的字符串選擇進入後,似乎還沒有做任何事情。 – user2177781