add junit5 tests with parameterization

This commit is contained in:
Greg Gauthier 2020-10-13 18:17:45 +01:00
parent 2c031e4d1f
commit f4a4d051da
4 changed files with 100 additions and 45 deletions

View File

@ -16,8 +16,9 @@ test {
} }
dependencies { dependencies {
implementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.2' implementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.0'
implementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.6.2' implementation group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.7.0'
implementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.7.0'
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0-alpha-6' 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: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.141.59'
implementation group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '4.2.2' implementation group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '4.2.2'

View File

@ -8,76 +8,75 @@ import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.UnreachableBrowserException; 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.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
public class BrowserDriver extends RemoteWebDriver { public class BrowserDriver {
private final RemoteWebDriver driver; private static final String osName = System.getProperty("os.name");
private final String osName = System.getProperty("os.name");
public BrowserDriver(final String browser, final Boolean headless) {
public static RemoteWebDriver getDriver(final String browser, final Boolean headless) {
switch (browser) { switch (browser) {
case "chrome": this.driver = getChrome(headless); case "chrome":
break; return getChrome(headless);
case "firefox": this.driver = getFirefox(headless); case "firefox":
break; return getFirefox(headless);
case "edge": this.driver = getEdge(headless); // headless unsupported until selenium 4 case "edge":
break; return getEdge(headless); // headless unsupported until selenium 4
case "safari": this.driver = getSafari(headless); // headless unsupported indefinitely case "safari":
break; return getSafari(false); // headless unsupported indefinitely
default: throw new IllegalArgumentException("Invalid browser specified"); default: throw new IllegalArgumentException("Invalid browser specified");
} }
} }
public RemoteWebDriver getDriver(){ private static RemoteWebDriver getChrome(final Boolean headless){
return this.driver;
}
private RemoteWebDriver getChrome(final Boolean headless){
WebDriverManager.chromedriver().setup(); WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions(); ChromeOptions options = new ChromeOptions();
options.setHeadless(headless); options.setHeadless(headless);
options.addArguments("--ignore-certificate-errors"); // only for limited test envs options.setAcceptInsecureCerts(true);
return new ChromeDriver(options); RemoteWebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
return driver;
} }
private RemoteWebDriver getFirefox(final Boolean headless){ private static RemoteWebDriver getFirefox(final Boolean headless){
WebDriverManager.firefoxdriver().setup(); WebDriverManager.firefoxdriver().setup();
FirefoxOptions options = new FirefoxOptions(); FirefoxOptions options = new FirefoxOptions();
options.setHeadless(headless); options.setHeadless(headless);
options.addArguments("--ignore-certificate-errors"); options.setAcceptInsecureCerts(true);
return new FirefoxDriver(options); RemoteWebDriver driver = new FirefoxDriver(options);
driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
return driver;
} }
private RemoteWebDriver getEdge(final Boolean headless){ private static RemoteWebDriver getEdge(final Boolean headless){
List<String> osNames = Arrays.asList("Windows 10", "Mac OS X"); List<String> osNames = Arrays.asList("Windows 10", "Mac OS X");
if (!osNames.contains(osName)){ if (!osNames.contains(osName)){
throw new UnreachableBrowserException("Edge browser not available on this platform"); throw new UnreachableBrowserException("Edge browser not available on this platform");
} }
WebDriverManager.edgedriver().setup(); WebDriverManager.edgedriver().setup();
EdgeOptions options = new EdgeOptions(); EdgeOptions options = new EdgeOptions();
options.setAcceptInsecureCerts(true);
//if (headless.equals(true)){ // necessary for Selenium 3 //if (headless.equals(true)){ // necessary for Selenium 3
// throw new UnsupportedOperationException("Edge does not support headless execution yet"); // throw new UnsupportedOperationException("Edge does not support headless execution yet");
//} //}
//options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true); //options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
//** the following will only work once Selenium 4 is ready. //** the following will only work once Selenium 4 is ready.
options.setAcceptInsecureCerts(true);
if (headless){ if (headless){
options.addArguments("headless"); options.addArguments("headless");
} }
return new EdgeDriver(options); RemoteWebDriver driver = new EdgeDriver(options);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
return driver;
} }
private SafariDriver getSafari(final Boolean headless){ private static RemoteWebDriver getSafari(final Boolean headless){
String osName = System.getProperty("os.name"); String osName = System.getProperty("os.name");
if (!osName.contentEquals("Mac OS X")){ if (!osName.contentEquals("Mac OS X")){
throw new UnreachableBrowserException("Safari browser not available on this platform"); throw new UnreachableBrowserException("Safari browser not available on this platform");
@ -86,18 +85,12 @@ public class BrowserDriver extends RemoteWebDriver {
throw new UnsupportedOperationException("Safari does not support headless execution yet"); throw new UnsupportedOperationException("Safari does not support headless execution yet");
} }
SafariOptions options = new SafariOptions(); SafariOptions options = new SafariOptions();
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); //options.setAcceptInsecureCerts(true);
return new SafariDriver(options); DesiredCapabilities capabilities = new DesiredCapabilities();
} capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
RemoteWebDriver driver = new SafariDriver(options);
private Path findFile(Path targetDir, String fileName) throws IOException { driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return Files.list(targetDir).filter( (p) -> { return driver;
if (Files.isRegularFile(p)) {
return p.getFileName().toString().equals(fileName);
} else {
return false;
}
}).findFirst().orElse(null);
} }
} }

View File

@ -31,7 +31,7 @@ public class Stepdefs {
public void i_have_a_driver_for(String browser) { public void i_have_a_driver_for(String browser) {
boolean headless = true; boolean headless = true;
if (browser.contains("safari")) headless = false; if (browser.contains("safari")) headless = false;
driver = new BrowserDriver(browser, headless).getDriver(); driver = BrowserDriver.getDriver(browser, headless);
} }
@When("I navigate to test.io") @When("I navigate to test.io")

View File

@ -0,0 +1,61 @@
package junit;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import tools.BrowserDriver;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import static org.junit.jupiter.api.Assertions.fail;
public class BrowserTests {
private RemoteWebDriver driver;
private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
@BeforeEach
void setupThis(){
System.out.println(dtf.format(LocalDateTime.now()) + " starting");
}
@DisplayName("Browser Validation")
@ParameterizedTest(name = "Browser: {0}")
@ValueSource(strings = {"chrome", "firefox", "edge", "safari" })
void testBrowsers(String browser) {
driver = BrowserDriver.getDriver(browser, true);
System.out.println(dtf.format(LocalDateTime.now()) + " Navigating to url...");
String url = "https://duckduckgo.com/";
driver.get(url);
if (!driver.getCurrentUrl().equals(url)){
fail("Browser navigation failed");
}
System.out.println(dtf.format(LocalDateTime.now()) + " Entering search now...");
RemoteWebElement searchBox =
(RemoteWebElement)driver.findElementByXPath("//input[@id='search_form_input_homepage']");
searchBox.sendKeys("frankenberries");
searchBox.sendKeys(Keys.ENTER);
System.out.println(dtf.format(LocalDateTime.now())+ " Checking search results...");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10),Duration.ofMillis(100));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='links']")));
RemoteWebElement results =
(RemoteWebElement)driver.findElementByPartialLinkText("Monster cereal");
System.out.println(results.getText());
Assertions.assertNotNull(results);
}
@AfterEach
void tearThis(){
driver.quit();
System.out.println(dtf.format(LocalDateTime.now()) + " finished");
}
}