Skip to content
Snippets Groups Projects
Commit aad5600d authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#431 - externalized helpers

parent bc9b711d
No related branches found
No related tags found
No related merge requests found
package de.deadlocker8.budgetmaster.integration; package de.deadlocker8.budgetmaster.integration;
import de.deadlocker8.budgetmaster.Main; import de.deadlocker8.budgetmaster.Main;
import de.deadlocker8.budgetmaster.authentication.UserService;
import de.thecodelabs.utils.util.Localization;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
...@@ -19,11 +11,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; ...@@ -19,11 +11,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@SeleniumTest @SeleniumTest
...@@ -33,91 +24,21 @@ public class ImportTest ...@@ -33,91 +24,21 @@ public class ImportTest
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Autowired @Autowired
private WebDriver driver; private WebDriver driver;
private final static String BASE_URL = "https://localhost:";
private String url;
@LocalServerPort @LocalServerPort
int port; int port;
@Before
public void before()
{
url = BASE_URL + port;
}
@Test @Test
public void requestImport() throws InterruptedException public void requestImport()
{
driver.get(url);
// login
WebElement inputPassword = driver.findElement(By.id("login-password"));
inputPassword.sendKeys(UserService.DEFAULT_PASSWORD);
WebElement buttonLogin = driver.findElement(By.tagName("button"));
buttonLogin.click();
// hide backup reminder if present
try
{
WebElement buttonCloseReminder = driver.findElement(By.cssSelector("#modalBackupReminder #buttonCloseReminder"));
buttonCloseReminder.click();
}
catch(NoSuchElementException ignored)
{ {
} IntegrationTestHelper helper = new IntegrationTestHelper(driver, port);
helper.start();
driver.get(url + "/settings/database/requestImport"); helper.login();
helper.hideBackupReminder();
// upload database
WebElement input = driver.findElement(By.id("inputDatabaseImport"));
assertNotNull(input);
String path = getClass().getClassLoader().getResource("SearchDatabase.json").getFile().replace("/", File.separator); String path = getClass().getClassLoader().getResource("SearchDatabase.json").getFile().replace("/", File.separator);
if(path.startsWith("\\")) List<String> sourceAccounts = Arrays.asList("DefaultAccount0815", "sfsdf");
{ List<String> destinationAccounts = Arrays.asList("DefaultAccount0815", "Account2");
path = path.substring(1); helper.uploadDatabase(path, sourceAccounts, destinationAccounts);
}
input.sendKeys(path);
WebElement buttonUpload = driver.findElement(By.id("button-confirm-database-import"));
buttonUpload.click();
// create new account
driver.findElement(By.className("button-new-account")).click();
WebElement inputAccountName = driver.findElement(By.id("account-name"));
inputAccountName.sendKeys("DefaultAccount0815");
driver.findElement(By.tagName("button")).click();
// create second account
driver.findElement(By.className("button-new-account")).click();
inputAccountName = driver.findElement(By.id("account-name"));
inputAccountName.sendKeys("Account2");
driver.findElement(By.tagName("button")).click();
// account matching
WebElement headlineImport = driver.findElement(By.className("headline"));
assertEquals(Localization.getString("info.title.database.import.dialog"), IntegrationTestHelper.getTextNode(headlineImport));
List<WebElement> tableRows = driver.findElements(By.cssSelector(".container form table tr"));
assertEquals(2, tableRows.size());
WebElement row1 = tableRows.get(0);
WebElement sourceAccount1 = row1.findElement(By.className("account-source"));
assertEquals("DefaultAccount0815", IntegrationTestHelper.getTextNode(sourceAccount1));
// match first account
row1.findElement(By.className("select-dropdown")).click();
WebElement accountToSelect = row1.findElement(By.xpath("//form/table/tbody/tr[1]/td[5]/div/div/ul/li/span[text()='DefaultAccount0815']"));
accountToSelect.click();
// match second account
tableRows.get(1).findElement(By.className("select-dropdown")).click();
accountToSelect = row1.findElement(By.xpath("//form/table/tbody/tr[2]/td[5]/div/div/ul/li/span[text()='Account2']"));
accountToSelect.click();
// confirm import
driver.findElement(By.id("buttonImport")).click();
assertEquals(Localization.getString("menu.settings"), IntegrationTestHelper.getTextNode(driver.findElement(By.className("headline"))));
} }
} }
\ No newline at end of file
package de.deadlocker8.budgetmaster.integration; package de.deadlocker8.budgetmaster.integration;
import de.deadlocker8.budgetmaster.authentication.UserService;
import de.thecodelabs.utils.util.Localization;
import org.openqa.selenium.By; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import java.io.File;
import java.util.List; import java.util.List;
public class IntegrationTestHelper import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
class IntegrationTestHelper
{ {
public static String getTextNode(WebElement e) static String getTextNode(WebElement e)
{ {
String text = e.getText().trim(); String text = e.getText().trim();
List<WebElement> children = e.findElements(By.xpath("./*")); List<WebElement> children = e.findElements(By.xpath("./*"));
...@@ -17,4 +25,100 @@ public class IntegrationTestHelper ...@@ -17,4 +25,100 @@ public class IntegrationTestHelper
} }
return text; return text;
} }
private final static String BASE_URL = "https://localhost:";
private WebDriver driver;
private String url;
IntegrationTestHelper(WebDriver driver, int port)
{
this.driver = driver;
this.url = BASE_URL + port;
}
void start()
{
driver.get(url);
}
void login()
{
WebElement inputPassword = driver.findElement(By.id("login-password"));
inputPassword.sendKeys(UserService.DEFAULT_PASSWORD);
WebElement buttonLogin = driver.findElement(By.tagName("button"));
buttonLogin.click();
}
void hideBackupReminder()
{
try
{
WebElement buttonCloseReminder = driver.findElement(By.cssSelector("#modalBackupReminder #buttonCloseReminder"));
buttonCloseReminder.click();
}
catch(NoSuchElementException ignored)
{
}
}
void uploadDatabase(String path, List<String> sourceAccounts, List<String> destinationAccounts)
{
if(path.startsWith("\\"))
{
path = path.substring(1);
}
driver.get(url + "/settings/database/requestImport");
// upload database
WebElement input = driver.findElement(By.id("inputDatabaseImport"));
input.sendKeys(path);
// confirm upload
driver.findElement(By.id("button-confirm-database-import")).click();
// create new accounts
for(String account : destinationAccounts)
{
createAccountOnImport(account);
}
// account matching
matchAccounts(sourceAccounts, destinationAccounts);
// confirm import
driver.findElement(By.id("buttonImport")).click();
assertEquals(Localization.getString("menu.settings"), IntegrationTestHelper.getTextNode(driver.findElement(By.className("headline"))));
}
private void createAccountOnImport(String accountName)
{
driver.findElement(By.className("button-new-account")).click();
WebElement inputAccountName = driver.findElement(By.id("account-name"));
inputAccountName.sendKeys(accountName);
driver.findElement(By.tagName("button")).click();
}
private void matchAccounts(List<String> sourceAccounts, List<String> destinationAccounts)
{
WebElement headlineImport = driver.findElement(By.className("headline"));
assertEquals(Localization.getString("info.title.database.import.dialog"), IntegrationTestHelper.getTextNode(headlineImport));
List<WebElement> tableRows = driver.findElements(By.cssSelector(".container form table tr"));
assertEquals(destinationAccounts.size(), tableRows.size());
for(int i = 0; i < destinationAccounts.size(); i++)
{
String account = destinationAccounts.get(i);
WebElement row = tableRows.get(i);
WebElement sourceAccount = row.findElement(By.className("account-source"));
assertEquals(sourceAccounts.get(i), IntegrationTestHelper.getTextNode(sourceAccount));
row.findElement(By.className("select-dropdown")).click();
WebElement accountToSelect = row.findElement(By.xpath("//form/table/tbody/tr[" + (i+1) + "]/td[5]/div/div/ul/li/span[text()='" + account + "']"));
accountToSelect.click();
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment