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 {
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.junit.jupiter', name: 'junit-jupiter-api', version: '5.7.0'
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: '3.141.59'
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.FirefoxOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
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.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class BrowserDriver extends RemoteWebDriver {
private final RemoteWebDriver driver;
private final String osName = System.getProperty("os.name");
public BrowserDriver(final String browser, final Boolean headless) {
public class BrowserDriver {
private static final String osName = System.getProperty("os.name");
public static RemoteWebDriver getDriver(final String browser, final Boolean 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;
case "chrome":
return getChrome(headless);
case "firefox":
return getFirefox(headless);
case "edge":
return getEdge(headless); // headless unsupported until selenium 4
case "safari":
return getSafari(false); // headless unsupported indefinitely
default: throw new IllegalArgumentException("Invalid browser specified");
}
}
public RemoteWebDriver getDriver(){
return this.driver;
}
private RemoteWebDriver getChrome(final Boolean headless){
private static RemoteWebDriver getChrome(final Boolean headless){
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setHeadless(headless);
options.addArguments("--ignore-certificate-errors"); // only for limited test envs
return new ChromeDriver(options);
options.setAcceptInsecureCerts(true);
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();
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(headless);
options.addArguments("--ignore-certificate-errors");
return new FirefoxDriver(options);
options.setAcceptInsecureCerts(true);
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");
if (!osNames.contains(osName)){
throw new UnreachableBrowserException("Edge browser not available on this platform");
}
WebDriverManager.edgedriver().setup();
EdgeOptions options = new EdgeOptions();
options.setAcceptInsecureCerts(true);
//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);
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");
if (!osName.contentEquals("Mac OS X")){
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");
}
SafariOptions options = new SafariOptions();
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
return new SafariDriver(options);
}
private Path findFile(Path targetDir, String fileName) throws IOException {
return Files.list(targetDir).filter( (p) -> {
if (Files.isRegularFile(p)) {
return p.getFileName().toString().equals(fileName);
} else {
return false;
}
}).findFirst().orElse(null);
//options.setAcceptInsecureCerts(true);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
RemoteWebDriver driver = new SafariDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}

View File

@ -31,7 +31,7 @@ public class Stepdefs {
public void i_have_a_driver_for(String browser) {
boolean headless = true;
if (browser.contains("safari")) headless = false;
driver = new BrowserDriver(browser, headless).getDriver();
driver = BrowserDriver.getDriver(browser, headless);
}
@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");
}
}