diff --git a/src/test/java/de/deadlocker8/budgetmaster/integration/ImportTest.java b/src/test/java/de/deadlocker8/budgetmaster/integration/ImportTest.java index 560f5ea0ecb752e0bfb43e02d334e04ec77c2e1b..d3fbdb7741b6b354c12550b80eb0e3626659df8f 100644 --- a/src/test/java/de/deadlocker8/budgetmaster/integration/ImportTest.java +++ b/src/test/java/de/deadlocker8/budgetmaster/integration/ImportTest.java @@ -1,17 +1,9 @@ package de.deadlocker8.budgetmaster.integration; 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.runner.RunWith; -import org.openqa.selenium.By; -import org.openqa.selenium.NoSuchElementException; 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.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; @@ -19,11 +11,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Main.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SeleniumTest @@ -33,91 +24,21 @@ public class ImportTest @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @Autowired private WebDriver driver; - private final static String BASE_URL = "https://localhost:"; - private String url; @LocalServerPort int port; - @Before - public void before() - { - url = BASE_URL + port; - } - @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) - { - } - - driver.get(url + "/settings/database/requestImport"); - - // upload database - WebElement input = driver.findElement(By.id("inputDatabaseImport")); - assertNotNull(input); + IntegrationTestHelper helper = new IntegrationTestHelper(driver, port); + helper.start(); + helper.login(); + helper.hideBackupReminder(); String path = getClass().getClassLoader().getResource("SearchDatabase.json").getFile().replace("/", File.separator); - if(path.startsWith("\\")) - { - path = path.substring(1); - } - 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")))); + List<String> sourceAccounts = Arrays.asList("DefaultAccount0815", "sfsdf"); + List<String> destinationAccounts = Arrays.asList("DefaultAccount0815", "Account2"); + helper.uploadDatabase(path, sourceAccounts, destinationAccounts); } } \ No newline at end of file diff --git a/src/test/java/de/deadlocker8/budgetmaster/integration/IntegrationTestHelper.java b/src/test/java/de/deadlocker8/budgetmaster/integration/IntegrationTestHelper.java index 61b74c81ec16f3a147d59806bf0e39aea72517a6..3170b91d9d3b38127026fadcc777b35a9e742209 100644 --- a/src/test/java/de/deadlocker8/budgetmaster/integration/IntegrationTestHelper.java +++ b/src/test/java/de/deadlocker8/budgetmaster/integration/IntegrationTestHelper.java @@ -1,13 +1,21 @@ 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.NoSuchElementException; +import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import java.io.File; 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(); List<WebElement> children = e.findElements(By.xpath("./*")); @@ -17,4 +25,100 @@ public class IntegrationTestHelper } 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(); + } + } }