Skip to content

Commit

Permalink
✅ : add e2e test scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Dec 12, 2019
1 parent a7a8f25 commit ada0909
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 23 deletions.
28 changes: 28 additions & 0 deletions src/test/java/io/codeka/gaia/e2e/DashboardPage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.codeka.gaia.e2e

import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.FindBy

class DashboardPage (val webDriver: WebDriver){

init {
if(! webDriver.title.equals("Gaia - A terraform UI")){
throw IllegalStateException("This is not the dashboard page. Current page is ${webDriver.title}")
}
}

@FindBy(className= "modules_count")
private lateinit var modulesCountElement: WebElement

@FindBy(className="stacks_count")
private lateinit var stacksCountElement: WebElement

@FindBy(className = "stacks_toUpdate_count")
private lateinit var stacksToUpdateCountElement: WebElement

fun modulesCount() = this.modulesCountElement.text.toInt()
fun stacksCount() = this.stacksCountElement.text.toInt()
fun stacksToUpdateCount() = this.stacksToUpdateCountElement.text.toInt()

}
10 changes: 7 additions & 3 deletions src/test/java/io/codeka/gaia/e2e/LoginPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.codeka.gaia.e2e
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.FindBy
import org.openqa.selenium.support.PageFactory

class LoginPage(val webDriver: WebDriver) {

Expand All @@ -22,9 +23,12 @@ class LoginPage(val webDriver: WebDriver) {
lateinit var submitButton: WebElement

fun login(login: String, password: String){
loginInput.sendKeys(login)
passwordInput.sendKeys(password)
submitButton.click()
if(webDriver.currentUrl.contains("login")){
loginInput.sendKeys(login)
passwordInput.sendKeys(password)
submitButton.click()
}
return PageFactory.initElements(this.webDriver, DashboardPage::class)
}

}
32 changes: 32 additions & 0 deletions src/test/java/io/codeka/gaia/e2e/ModulePage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.codeka.gaia.e2e

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.FindBy
import org.openqa.selenium.support.PageFactory

class ModulePage(webDriver: WebDriver) {

init {
if(! webDriver.currentUrl.contains("/modules/")){
throw IllegalStateException("This is not the module page. Current page is ${webDriver.currentUrl}")
}
}

@FindBy(id="module.name")
private lateinit var nameInput: WebElement

@FindBy(id="module.cliVersion")
private lateinit var cliVersionInput: WebElement

@FindBy(id="module.description")
private lateinit var descriptionInput: WebElement

fun moduleName() : String = nameInput.getAttribute("value")

fun moduleDescription() : String = descriptionInput.getAttribute("value")

fun cliVersion() : String = cliVersionInput.getAttribute("value")

}
23 changes: 23 additions & 0 deletions src/test/java/io/codeka/gaia/e2e/ModulesPage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.codeka.gaia.e2e

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.FindAll
import org.openqa.selenium.support.FindBy
import org.openqa.selenium.support.FindBys

class ModulesPage (webDriver: WebDriver){

private var moduleCards: List<WebElement>

init {
if(! webDriver.title.equals("Gaia - Modules")){
throw IllegalStateException("This is not the modules page. Current page is ${webDriver.title}")
}
moduleCards = webDriver.findElements(By.className("card"));
}

fun modulesCount() = this.moduleCards.size

}
79 changes: 59 additions & 20 deletions src/test/java/io/codeka/gaia/e2e/SeleniumIT.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package io.codeka.gaia.e2e;

import io.codeka.gaia.test.MongoContainer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.*;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
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.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.annotation.DirtiesContext;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
@Testcontainers
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class SeleniumIT {

@LocalServerPort
Expand All @@ -34,21 +41,17 @@ public class SeleniumIT {

private static WebDriver driver;

// private static Percy percy;

@BeforeAll
public static void openServerAndBrowser() throws IOException {
FirefoxOptions options = new FirefoxOptions();
driver = new FirefoxDriver(options);

// ChromeOptions options = new ChromeOptions();
// options.addArguments(
// "--headless",
// "--disable-web-security",
// "--allow-running-insecure-content",
// "--ignore-certificate-errors");
// driver = new ChromeDriver(options);
// percy = new Percy(driver);
ChromeOptions options = new ChromeOptions();
options.addArguments(
"--headless",
"--disable-web-security",
"--allow-running-insecure-content",
"--ignore-certificate-errors");
driver = new ChromeDriver(options);

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

@AfterAll
Expand All @@ -62,14 +65,50 @@ private String testUrl(){
}

@Test
void loginPage_opens() throws Exception {
@Order(1) // this test runs first as it logs the user in !
void loginPage() throws IOException {
driver.get(testUrl());
driver.manage().window().setSize(new Dimension(1280,800));

LoginPage page = PageFactory.initElements(driver, LoginPage.class);

page.login("admin", "admin123");
}

@Test
void dashboardShows(){
void dashboardPage_showsModuleCount() throws IOException {
driver.get(testUrl()+"/");

var page = PageFactory.initElements(driver, DashboardPage.class);

assertEquals(3, page.modulesCount());
assertEquals(0, page.stacksCount());
assertEquals(0, page.stacksToUpdateCount());
}

@Test
void modulesPage_showsModules() throws IOException {
driver.get(testUrl()+"/modules");

var page = PageFactory.initElements(driver, ModulesPage.class);
assertEquals(3, page.modulesCount());
}

@Test
void modulePage_showsModuleDetails() throws IOException {
driver.get(testUrl()+"/modules/e01f9925-a559-45a2-8a55-f93dc434c676");

var page = new ModulePage(driver);
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 10), page);

assertThat(page.moduleName()).isEqualTo("terraform-docker-mongo");
assertThat(page.moduleDescription()).contains("A sample terraform");
assertThat(page.cliVersion()).isEqualTo("0.11.14");
}

void takeScreenshot() throws IOException {
var file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
System.out.println(file.getAbsolutePath());
FileUtils.copyFileToDirectory(file, new File("./target"));
}
}

0 comments on commit ada0909

Please sign in to comment.