From c7c76b863e35ed1af3e5a69b39946ac929bb849f Mon Sep 17 00:00:00 2001 From: helkv Date: Tue, 21 Jan 2020 12:53:24 +0100 Subject: [PATCH] #2 Improve some Assertions -> Use contains-Assertions to check whether a certain name of an element (file, library, ...) is present in a list of elements! (Instead of having a method on page-object level which only returns a boolean) -> Add a assertion-description to assertions which have to check a boolean value - Add methods: readLibraryNames(), readFileNames() - Remove unused methods --- src/main/java/ui/pages/HomePage.java | 10 ++++-- src/main/java/ui/pages/LibraryPage.java | 31 ++++--------------- src/test/java/stepdefinitions/FileSteps.java | 2 +- .../java/stepdefinitions/LibrariesSteps.java | 27 ++++++++-------- 4 files changed, 28 insertions(+), 42 deletions(-) diff --git a/src/main/java/ui/pages/HomePage.java b/src/main/java/ui/pages/HomePage.java index 33ffe0f..6312565 100644 --- a/src/main/java/ui/pages/HomePage.java +++ b/src/main/java/ui/pages/HomePage.java @@ -2,6 +2,7 @@ import static io.cucumber.spring.CucumberTestContext.SCOPE_CUCUMBER_GLUE; +import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -95,10 +96,13 @@ public HomePage createNewLibrary(String newLibraryName) { return homePage; } - public boolean containsLibrary(String libraryName) { - List libraryLinks = this.myLibrariesDiv.findElements(By.linkText(libraryName)); + public List readLibraryNames() { + List libraryLinks = this.myLibrariesDiv.findElements(By.xpath(".//*[@class='repo-name-span']/a")); - return !libraryLinks.isEmpty(); + List libraryNames = new ArrayList<>(); + libraryLinks.forEach(libraryLink -> libraryNames.add(libraryLink.getText())); + + return libraryNames; } public HomePage deleteLibrary(String libraryName) { diff --git a/src/main/java/ui/pages/LibraryPage.java b/src/main/java/ui/pages/LibraryPage.java index a33382d..dc01cea 100644 --- a/src/main/java/ui/pages/LibraryPage.java +++ b/src/main/java/ui/pages/LibraryPage.java @@ -4,8 +4,7 @@ import java.io.File; import java.net.URL; -import java.util.Arrays; -import java.util.Collection; +import java.util.ArrayList; import java.util.List; import org.openqa.selenium.By; @@ -44,31 +43,13 @@ public LibraryPage(WebDriver driver) { super(driver); } - public boolean containsElements(Collection elementNames) { - for (String elementName : elementNames) { - List elementLinks = this.directoryViewDiv.findElements(By.linkText(elementName)); - if (elementLinks.isEmpty()) { - return false; - } - } + public List readFileNames() { + List fileLinks = this.directoryViewDiv.findElements(By.xpath(".//*[@class='dirent-name']/a")); - return true; - } - - public boolean containsElements(String... elementNames) { - return this.containsElements(Arrays.asList(elementNames)); - } - - public boolean containsElementsContainingNameSubstring(String... elementNameSubstrings) { - for (String elementNameSubstring : elementNameSubstrings) { - List elementLinks = this.directoryViewDiv - .findElements(By.partialLinkText(elementNameSubstring)); - if (elementLinks.isEmpty()) { - return false; - } - } + List fileNames = new ArrayList<>(); + fileLinks.forEach(fileLink -> fileNames.add(fileLink.getText())); - return true; + return fileNames; } public void openMarkdownElement(String elementName) { diff --git a/src/test/java/stepdefinitions/FileSteps.java b/src/test/java/stepdefinitions/FileSteps.java index 0fc0b77..15bcb44 100644 --- a/src/test/java/stepdefinitions/FileSteps.java +++ b/src/test/java/stepdefinitions/FileSteps.java @@ -45,7 +45,7 @@ public void markdownFileTitlePresent(String fileName) { public void markdownViewerButtonsPresent() { boolean buttonsPresent = markdownViewer.buttonsPresent(); - assertThat(buttonsPresent).isTrue(); + assertThat(buttonsPresent).as("Check presence of the Markdown-Viewer buttons.").isTrue(); } @Then("Archive metadata content consists of:") diff --git a/src/test/java/stepdefinitions/LibrariesSteps.java b/src/test/java/stepdefinitions/LibrariesSteps.java index 7498a49..aa57d64 100644 --- a/src/test/java/stepdefinitions/LibrariesSteps.java +++ b/src/test/java/stepdefinitions/LibrariesSteps.java @@ -53,9 +53,9 @@ public void createNewLibrary(String libraryName) { @Then("My Libraries contains {string}") public void myLibrariesContains(String libraryName) { - boolean containsLibrary = homePage.containsLibrary(libraryName); + List containedLibraryNames = homePage.readLibraryNames(); - assertThat(containsLibrary).isTrue(); + assertThat(containedLibraryNames).contains(libraryName); } @Then("Library {string} contains:") @@ -68,9 +68,9 @@ public void libraryContains(String libraryName, List fileNames) { logger.error("Waiting for Library was interrupted.", e); } - boolean defaultElementsContained = homePage.openLibrary(libraryName).containsElements(fileNames); + List containedFileNames = homePage.openLibrary(libraryName).readFileNames(); - assertThat(defaultElementsContained).isTrue(); + assertThat(containedFileNames).containsAll(fileNames); } @Given("Open Library {string}") @@ -98,7 +98,9 @@ public void lockFile(String fileName) { @Then("Lock symbole displayed for {word}") public void lockSymboleDisplayedForFile(String fileName) { - assertThat(libraryPage.lockedIconVisible(fileName)).isTrue(); + boolean lockedIconVisible = libraryPage.lockedIconVisible(fileName); + + assertThat(lockedIconVisible).as("Check visibility of the Locked-Icon.").isTrue(); } @When("Unlock {word} file") @@ -108,7 +110,9 @@ public void unlockFile(String fileName) { @Then("Lock symbole not displayed for {word}") public void lockSymboleNotDisplayedForFile(String fileName) { - assertThat(libraryPage.lockedIconVisible(fileName)).isFalse(); + boolean lockedIconVisible = libraryPage.lockedIconVisible(fileName); + + assertThat(lockedIconVisible).as("Check non-visibility of the Locked-Icon.").isFalse(); } @When("Upload file {word} to Library") @@ -118,11 +122,9 @@ public void uploadFileToLibrary(String fileName) { @Then("Library contains file {word}") public void libraryContainsFile(String fileName) { - // TODO: Return all files of the library and assert whether fileName is - // contained. Rework this for all assertions which only check true/false! - boolean elementContained = libraryPage.containsElements(fileName); + List containedFileNames = libraryPage.readFileNames(); - assertThat(elementContained).isTrue(); + assertThat(containedFileNames).contains(fileName); } @Then("Library {string} contains certificate") @@ -131,10 +133,9 @@ public void libraryContainsCertificate(String libraryName) { homePage.navigateTo(); homePage.openLibrary(libraryName); - // TODO: How to handle "cared-data-certificate_" correctly - boolean elementContained = libraryPage.containsElementsContainingNameSubstring("cared-data-certificate_"); + List containedFileNames = libraryPage.readFileNames(); - assertThat(elementContained).isTrue(); + assertThat(containedFileNames).anyMatch(fileName -> fileName.startsWith("cared-data-certificate_")); } }