add browser platform validation for safari and edge

This commit is contained in:
Greg Gauthier 2020-10-12 09:58:47 +01:00
parent a6d643bb29
commit 62959be1c3
2 changed files with 20 additions and 14 deletions

View File

@ -9,9 +9,12 @@ import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.UnreachableBrowserException;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions;
import java.util.Arrays;
import java.util.List;
public class BrowserDriver extends RemoteWebDriver {
@ -41,6 +44,7 @@ public class BrowserDriver extends RemoteWebDriver {
}
private RemoteWebDriver getChrome(final Boolean headless){
validateBrowser();
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
@ -49,6 +53,7 @@ public class BrowserDriver extends RemoteWebDriver {
}
private RemoteWebDriver getFirefox(final Boolean headless){
validateBrowser();
WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
@ -57,6 +62,7 @@ public class BrowserDriver extends RemoteWebDriver {
}
private RemoteWebDriver getEdge(final Boolean headless){
validateBrowser();
WebDriverManager.edgedriver().setup();
EdgeOptions options = new EdgeOptions();
//if (headless.equals(true)){ // necessary for Selenium 3
@ -72,6 +78,7 @@ public class BrowserDriver extends RemoteWebDriver {
}
private SafariDriver getSafari(final Boolean headless){
validateBrowser();
if (headless.equals(true)){
throw new UnsupportedOperationException("Safari does not support headless execution yet");
}
@ -79,4 +86,15 @@ public class BrowserDriver extends RemoteWebDriver {
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
return new SafariDriver(options);
}
private void validateBrowser() {
String osName = System.getProperty("os.name");
if (browser.contentEquals("safari") && !osName.contentEquals("Mac OS X")){
throw new UnreachableBrowserException("Safari browser not available on this platform");
}
List<String> osNames = Arrays.asList("Windows 10", "Mac OS X");
if (browser.contentEquals("edge") && !osNames.contains(osName)){
throw new UnreachableBrowserException("Edge browser not available on this platform");
}
}
}

View File

@ -7,10 +7,8 @@ import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.UnreachableBrowserException;
import tools.BrowserDriver;
import static org.junit.jupiter.api.Assertions.fail;
public class Stepdefs {
@ -31,14 +29,8 @@ public class Stepdefs {
@Given("I have a driver for {string}")
public void i_have_a_driver_for(String browser) {
Boolean headless = true;
if (browser.contains("safari")){
System.out.println(System.getProperty("os.name"));
if (!System.getProperty("os.name").contains("Mac OS X")){
throw new UnreachableBrowserException("Safari browser not available on this platform");
}
headless = false;
}
boolean headless = true;
if (browser.contains("safari")) headless = false;
driver = new BrowserDriver(browser, headless).getDriver();
}
@ -55,9 +47,5 @@ public class Stepdefs {
Assertions.assertEquals("QA Testing as a Service | test IO", driver.getTitle());
}
public String getOperatingSystem() {
String os = System.getProperty("os.name");
return os;
}
}