Skip to content

Commit

Permalink
✅ : make selenium tests work again
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit authored and cdubuisson committed Apr 9, 2020
1 parent 81f7411 commit 0cc5419
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/main/client/app/pages/modules/modules.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
v-for="module in modules"
:key="module.id"
:title="module.name"
class="moduleCard"
>
<b-card-text>
<app-cli-badge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<b-nav-item-dropdown>
<template v-slot:button-content>
<font-awesome-icon :icon="['far', 'user']" />
<span>{{ user.username }}</span>
<span id="user.name">{{ user.username }}</span>
</template>
<b-dropdown-item @click="doLogout">
<span>Log Out</span>
Expand Down
25 changes: 20 additions & 5 deletions src/test/java/io/codeka/gaia/e2e/DashboardPage.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
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.ui.ExpectedCondition
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait

class DashboardPage (val webDriver: WebDriver){

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

@FindBy(className= "modules_count")
fun waitForPageLoaded() {
val wait = WebDriverWait(webDriver, 10)
wait.until(ExpectedConditions.titleIs("Gaia - Dashboard"))
wait.until(ExpectedConditions.numberOfElementsToBe(By.className("b-overlay"), 0))

// getting widget values
wait.until(ExpectedConditions.numberOfElementsToBe(By.className("widget-value"), 3))

val countElements = webDriver.findElements(By.className("widget-value"))
modulesCountElement = countElements[0]
stacksCountElement = countElements[1]
stacksToUpdateCountElement = countElements[2]
}

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()

}
}
2 changes: 1 addition & 1 deletion src/test/java/io/codeka/gaia/e2e/JobPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class JobPage(webDriver: WebDriver) {

fun jobDetailTitle() : String = jobDetailTitle.text

}
}
28 changes: 18 additions & 10 deletions src/test/java/io/codeka/gaia/e2e/LoginPage.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package io.codeka.gaia.e2e

import org.junit.jupiter.api.Assertions.assertEquals
import org.openqa.selenium.By
import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.FindBy
import org.openqa.selenium.support.PageFactory
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.ExpectedConditions.*
import org.openqa.selenium.support.ui.WebDriverWait

class LoginPage(val webDriver: WebDriver) {

init {
if(! webDriver.currentUrl.contains("login")){
throw IllegalStateException("This is not the login page. Current page is ${webDriver.currentUrl}")
}
val wait = WebDriverWait(webDriver, 10)
wait.until { titleIs("Gaia - A terraform UI - Login") }
}

@FindBy(name="username")
Expand All @@ -23,12 +28,15 @@ class LoginPage(val webDriver: WebDriver) {
lateinit var submitButton: WebElement

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

// wait for successful login
val wait = WebDriverWait(webDriver, 10)
wait.until { titleIs("Gaia - A terraform UI").andThen { elementToBeClickable(By.id("user.name")) } }

assertEquals("admin", webDriver.findElement(By.id("user.name")).text);
}

}
}
20 changes: 14 additions & 6 deletions src/test/java/io/codeka/gaia/e2e/ModulesPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ 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.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait

class ModulesPage (webDriver: WebDriver){
class ModulesPage (val webDriver: WebDriver){

private var moduleCards: List<WebElement>
private lateinit var moduleCards: List<WebElement>

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

fun modulesCount() = this.moduleCards.size

}
fun waitForPageLoaded() {
val wait = WebDriverWait(webDriver, 10)
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("moduleCard")))

moduleCards = webDriver.findElements(By.className("moduleCard"));
}

}
26 changes: 17 additions & 9 deletions src/test/java/io/codeka/gaia/e2e/SeleniumIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
@Testcontainers

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Tag("e2e")
@Disabled("Disabled until complete migration of client")
public class SeleniumIT {

@LocalServerPort
Expand Down Expand Up @@ -84,7 +82,8 @@ void loginPage() {
driver.get(testUrl());
driver.manage().window().setSize(new Dimension(1280,800));

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

percy.snapshot("Login Page");

Expand All @@ -93,9 +92,11 @@ void loginPage() {

@Test
void dashboardPage_showsModuleCount() {
driver.get(testUrl()+"/");
driver.get(testUrl()+"/dashboard");

var page = PageFactory.initElements(driver, DashboardPage.class);
var page = new DashboardPage(driver);
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 10), page);
page.waitForPageLoaded();

assertEquals(3, page.modulesCount());
assertEquals(1, page.stacksCount());
Expand All @@ -108,7 +109,10 @@ void dashboardPage_showsModuleCount() {
void modulesPage_showsModules() {
driver.get(testUrl()+"/modules");

var page = PageFactory.initElements(driver, ModulesPage.class);
var page = new ModulesPage(driver);
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 10), page);
page.waitForPageLoaded();

assertEquals(3, page.modulesCount());

percy.snapshot("Modules");
Expand All @@ -130,7 +134,7 @@ void modulePage_showsModuleDetails() {

@Test
void stackPage_showsStackDetails() {
driver.get(testUrl()+"/stacks/de28a01f-257a-448d-8e1b-00e4e3a41db2");
driver.get(testUrl()+"/stacks/de28a01f-257a-448d-8e1b-00e4e3a41db2/edit");

var page = new StackPage(driver);
PageFactory.initElements(new AjaxElementLocatorFactory(driver, 10), page);
Expand All @@ -152,9 +156,13 @@ void jobPage_showsJobDetails() {
percy.snapshot("Job Details");
}

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

0 comments on commit 0cc5419

Please sign in to comment.