first refactor. Separate driver generator class. Sensitivity to browser and selenium version

This commit is contained in:
Greg Gauthier 2020-10-12 08:39:29 +01:00
parent 5f9018ae5e
commit a6d643bb29
4 changed files with 114 additions and 26 deletions

View File

@ -18,7 +18,8 @@ test {
dependencies {
implementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.2'
implementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.6.2'
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0-alpha-6'
//implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
implementation group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '4.2.2'
testImplementation group: 'org.hamcrest', name: 'hamcrest', version: '2.2'
testImplementation 'io.cucumber:cucumber-java:6.7.0'

View File

@ -0,0 +1,82 @@
package tools;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
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.safari.SafariDriver;
import org.openqa.selenium.safari.SafariOptions;
public class BrowserDriver extends RemoteWebDriver {
private final RemoteWebDriver driver;
private final String browser;
private final Boolean headless;
public BrowserDriver(final String browser, final Boolean headless){
this.browser = browser;
this.headless = headless;
switch (browser) {
case "chrome": this.driver = getChrome(headless);
break;
case "firefox": this.driver = getFirefox(headless);
break;
case "edge": this.driver = getEdge(headless); // headless unsupported until selenium 4
break;
case "safari": this.driver = getSafari(headless); // headless unsupported indefinitely
break;
default: throw new IllegalArgumentException("Invalid browser specified");
}
}
public RemoteWebDriver getDriver(){
return this.driver;
}
private RemoteWebDriver getChrome(final Boolean headless){
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
options.addArguments("--ignore-certificate-errors"); // only for limited test envs
return new ChromeDriver(options);
}
private RemoteWebDriver getFirefox(final Boolean headless){
WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
options.addArguments("--ignore-certificate-errors");
return new FirefoxDriver(options);
}
private RemoteWebDriver getEdge(final Boolean headless){
WebDriverManager.edgedriver().setup();
EdgeOptions options = new EdgeOptions();
//if (headless.equals(true)){ // necessary for Selenium 3
// throw new UnsupportedOperationException("Edge does not support headless execution yet");
//}
//options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
//** the following will only work once Selenium 4 is ready.
options.setAcceptInsecureCerts(true);
if (headless){
options.addArguments("headless");
}
return new EdgeDriver(options);
}
private SafariDriver getSafari(final Boolean headless){
if (headless.equals(true)){
throw new UnsupportedOperationException("Safari does not support headless execution yet");
}
SafariOptions options = new SafariOptions();
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
return new SafariDriver(options);
}
}

View File

@ -5,22 +5,21 @@ import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
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 {
private RemoteWebDriver driver;
private String url;
protected RemoteWebDriver driver;
@Before
public void setUp() {
url = "https://test.io/";
}
@After
@ -32,13 +31,21 @@ public class Stepdefs {
@Given("I have a driver for {string}")
public void i_have_a_driver_for(String browser) {
driver = generateDriver(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;
}
driver = new BrowserDriver(browser, headless).getDriver();
}
@When("I navigate to test.io")
public void i_navigate_to_test_io() {
driver.get("https://test.io/");
if (!driver.getCurrentUrl().equals("https://test.io/")){
driver.get(url);
if (!driver.getCurrentUrl().equals(url)){
fail("Browser navigation failed");
}
}
@ -48,21 +55,9 @@ public class Stepdefs {
Assertions.assertEquals("QA Testing as a Service | test IO", driver.getTitle());
}
//helpers
public RemoteWebDriver generateDriver(final String browser) {
if (browser.equals("chrome")){
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
options.addArguments("--ignore-certificate-errors"); // only for limited test envs
return new ChromeDriver(options);
} else if (browser.equals("firefox")){
WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
options.addArguments("--ignore-certificate-errors");
return new FirefoxDriver(options);
}
return null;
public String getOperatingSystem() {
String os = System.getProperty("os.name");
return os;
}
}

View File

@ -7,4 +7,14 @@ Feature: Browser Demo
Scenario: Load test.io web page with firefox
Given I have a driver for "firefox"
When I navigate to test.io
Then The page is displayed
Scenario: Load test.io web page with MS Edge
Given I have a driver for "edge"
When I navigate to test.io
Then The page is displayed
Scenario: Load test.io web page with Safari
Given I have a driver for "safari"
When I navigate to test.io
Then The page is displayed