This commit is contained in:
Greg Gauthier 2020-10-13 13:42:09 +01:00
parent 3d9325b655
commit 2c031e4d1f

View File

@ -13,16 +13,17 @@ import org.openqa.selenium.remote.UnreachableBrowserException;
import org.openqa.selenium.safari.SafariDriver; import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions; import org.openqa.selenium.safari.SafariOptions;
import java.util.Arrays; import java.io.IOException;
import java.util.List; import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public class BrowserDriver extends RemoteWebDriver { public class BrowserDriver extends RemoteWebDriver {
private final RemoteWebDriver driver; private final RemoteWebDriver driver;
private final String browser; private final String osName = System.getProperty("os.name");
public BrowserDriver(final String browser, final Boolean headless){ public BrowserDriver(final String browser, final Boolean headless) {
this.browser = browser;
switch (browser) { switch (browser) {
case "chrome": this.driver = getChrome(headless); case "chrome": this.driver = getChrome(headless);
@ -42,7 +43,6 @@ public class BrowserDriver extends RemoteWebDriver {
} }
private RemoteWebDriver getChrome(final Boolean headless){ private RemoteWebDriver getChrome(final Boolean headless){
validateBrowser();
WebDriverManager.chromedriver().setup(); WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions(); ChromeOptions options = new ChromeOptions();
options.setHeadless(headless); options.setHeadless(headless);
@ -51,7 +51,6 @@ public class BrowserDriver extends RemoteWebDriver {
} }
private RemoteWebDriver getFirefox(final Boolean headless){ private RemoteWebDriver getFirefox(final Boolean headless){
validateBrowser();
WebDriverManager.firefoxdriver().setup(); WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions(); FirefoxOptions options = new FirefoxOptions();
options.setHeadless(headless); options.setHeadless(headless);
@ -60,7 +59,10 @@ public class BrowserDriver extends RemoteWebDriver {
} }
private RemoteWebDriver getEdge(final Boolean headless){ private RemoteWebDriver getEdge(final Boolean headless){
validateBrowser(); List<String> osNames = Arrays.asList("Windows 10", "Mac OS X");
if (!osNames.contains(osName)){
throw new UnreachableBrowserException("Edge browser not available on this platform");
}
WebDriverManager.edgedriver().setup(); WebDriverManager.edgedriver().setup();
EdgeOptions options = new EdgeOptions(); EdgeOptions options = new EdgeOptions();
//if (headless.equals(true)){ // necessary for Selenium 3 //if (headless.equals(true)){ // necessary for Selenium 3
@ -76,7 +78,10 @@ public class BrowserDriver extends RemoteWebDriver {
} }
private SafariDriver getSafari(final Boolean headless){ private SafariDriver getSafari(final Boolean headless){
validateBrowser(); String osName = System.getProperty("os.name");
if (!osName.contentEquals("Mac OS X")){
throw new UnreachableBrowserException("Safari browser not available on this platform");
}
if (headless.equals(true)){ if (headless.equals(true)){
throw new UnsupportedOperationException("Safari does not support headless execution yet"); throw new UnsupportedOperationException("Safari does not support headless execution yet");
} }
@ -85,14 +90,14 @@ public class BrowserDriver extends RemoteWebDriver {
return new SafariDriver(options); return new SafariDriver(options);
} }
private void validateBrowser() { private Path findFile(Path targetDir, String fileName) throws IOException {
String osName = System.getProperty("os.name"); return Files.list(targetDir).filter( (p) -> {
if (browser.contentEquals("safari") && !osName.contentEquals("Mac OS X")){ if (Files.isRegularFile(p)) {
throw new UnreachableBrowserException("Safari browser not available on this platform"); return p.getFileName().toString().equals(fileName);
} } else {
List<String> osNames = Arrays.asList("Windows 10", "Mac OS X"); return false;
if (browser.contentEquals("edge") && !osNames.contains(osName)){
throw new UnreachableBrowserException("Edge browser not available on this platform");
} }
}).findFirst().orElse(null);
} }
} }