Skip to content

Commit

Permalink
implement improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Sep 30, 2024
1 parent 9902a50 commit 42f5120
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,9 @@ public void preserve() throws InvalidMetadataValueException, NoSuchMetadataField
try {
if (Objects.nonNull(division)) {
division.getContentIds().clear();
//if (!division.getType().equals(PhysicalDivision.TYPE_PAGE)) {
if (!division.getType().equals(PhysicalDivision.TYPE_PAGE)) {
division.setOrderlabel(null);
//}
}
division.setLabel(null);
}
metadata.clear();
Expand All @@ -689,7 +689,7 @@ public void preserve() throws InvalidMetadataValueException, NoSuchMetadataField
}
}
if (Objects.nonNull(hiddenMetadata)) {
processHiddenMetadata();
metadata.addAll(hiddenMetadata);
}
} catch (InvalidMetadataValueException invalidValueException) {
if (Objects.isNull(division)) {
Expand All @@ -708,29 +708,6 @@ public void preserve() throws InvalidMetadataValueException, NoSuchMetadataField
}
}

private void processHiddenMetadata() {
for (Metadata metadatum : hiddenMetadata) {
if (metadatum instanceof MetadataEntry) {
MetadataEntry metadataEntry = (MetadataEntry) metadatum;
String key = metadataEntry.getKey().toUpperCase();
String value = metadataEntry.getValue();
switch (key) {
case "LABEL":
division.setLabel(value);
break;
case "ORDERLABEL":
division.setOrderlabel(value);
break;
case "CONTENTIDS":
division.getContentIds().add(URI.create(value));
break;
}
}
}
metadata.addAll(hiddenMetadata);
}


/**
* Removes a process detail.
*
Expand Down
46 changes: 44 additions & 2 deletions Kitodo/src/test/java/org/kitodo/selenium/CalendarST.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@
import org.kitodo.data.exceptions.DataException;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.selenium.testframework.BaseTestSelenium;
import org.kitodo.selenium.testframework.Browser;
import org.kitodo.selenium.testframework.Pages;
import org.kitodo.selenium.testframework.pages.CalendarPage;
import org.kitodo.selenium.testframework.pages.ProcessesPage;
import org.kitodo.test.utils.ProcessTestUtils;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class CalendarST extends BaseTestSelenium {

Expand Down Expand Up @@ -68,19 +74,55 @@ public static void cleanup() throws CustomResponseException, DAOException, DataE
ProcessTestUtils.removeTestProcess(newspaperTestProcessId);
}


@Test
public void createProcessFromCalendar() throws Exception {
// Navigate to the processes page and open the calendar
processesPage.goTo();
processesPage.goToCalendar(newspaperTestProcessId);

// Add blocks and issues
calendarPage.addBlock();
calendarPage.addIssue("Morning issue");
calendarPage.addIssue("Evening issue");

// Assert the number of issues is correct
assertEquals(4, calendarPage.countIssues(), "Number of issues in the calendar does not match");

// Add metadata to issues
calendarPage.addMetadataToThis();
calendarPage.addMetadataToAll();
List<String> morningIssueMetadata = calendarPage.getMetadata("Morning issue");
List<String> eveningIssueMetadata = calendarPage.getMetadata("Evening issue");

// Fetch "Morning issue" metadata with WebDriverWait
List<String> morningIssueMetadata = fetchMetadataWithRetry("Morning issue");

// Fetch "Evening issue" metadata with WebDriverWait
List<String> eveningIssueMetadata = fetchMetadataWithRetry("Evening issue");

// Verify that the metadata is correct
assertEquals(Arrays.asList("Signatur", "Process title"), morningIssueMetadata, "Metadata for morning issue is incorrect");
assertEquals(List.of("Signatur"), eveningIssueMetadata, "Metadata for evening issue is incorrect");
}

// Method to fetch metadata with retry logic to handle stale element exceptions
private List<String> fetchMetadataWithRetry(String issueName) {
int attempts = 0;
while (attempts < 3) {
try {
WebDriverWait wait = new WebDriverWait(Browser.getDriver(), 15);
List<WebElement> metadataElements = wait.until(ExpectedConditions.visibilityOfAllElements(
calendarPage.getMetadataElements(issueName)
));
return calendarPage.extractMetadataFromElements(metadataElements);
} catch (StaleElementReferenceException e) {
attempts++;
System.out.println("Attempt " + attempts + ": Stale element detected. Retrying...");
} catch (TimeoutException e) {
// Handle timeout here if needed
System.out.println("Attempt " + attempts + ": Timeout waiting for elements. Retrying...");
attempts++;
}
}
throw new StaleElementReferenceException("Failed to fetch metadata after 3 attempts due to stale element issues");
}
}
132 changes: 123 additions & 9 deletions Kitodo/src/test/java/org/kitodo/selenium/ImportingST.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
import org.kitodo.selenium.testframework.pages.ProjectsPage;
import org.kitodo.test.utils.ProcessTestUtils;
import org.kitodo.test.utils.TestConstants;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ImportingST extends BaseTestSelenium {

Expand Down Expand Up @@ -156,28 +160,138 @@ public void checkOrderOfImportConfigurations() throws Exception {
*/
@Test
public void checkHierarchyImport() throws Exception {
// Create a new process
projectsPage.createNewProcess();
Select catalogSelectMenu = new Select(importPage.getCatalogMenu());

// Ensure catalog menu is interactable and retry if necessary
Select catalogSelectMenu = null;
int attempts = 0;
while (attempts < 3) {
try {
WebDriverWait wait = new WebDriverWait(Browser.getDriver(), 10);
WebElement catalogMenuElement = wait.until(ExpectedConditions.elementToBeClickable(importPage.getCatalogMenu()));
catalogSelectMenu = new Select(catalogMenuElement);
break; // exit loop if successful
} catch (StaleElementReferenceException e) {
attempts++;
System.out.println("Stale element detected for catalog menu. Retrying... (" + attempts + ")");
}
}
assertEquals(TestConstants.K10PLUS, catalogSelectMenu.getFirstSelectedOption().getAttribute("label"), "Wrong default catalog selected");

// Select Kalliope catalog
importPage.selectKalliope();
Select searchFieldSelectMenu = new Select(importPage.getSearchFieldMenu());

// Ensure search field menu is interactable before selecting
Select searchFieldSelectMenu = null;
attempts = 0;
while (attempts < 3) {
try {
WebDriverWait wait = new WebDriverWait(Browser.getDriver(), 10);
WebElement searchFieldElement = wait.until(ExpectedConditions.elementToBeClickable(importPage.getSearchFieldMenu()));
searchFieldSelectMenu = new Select(searchFieldElement);
break; // exit loop if successful
} catch (StaleElementReferenceException e) {
attempts++;
System.out.println("Stale element detected for search field menu. Retrying... (" + attempts + ")");
}
}
assertEquals(TestConstants.IDENTIFIER, searchFieldSelectMenu.getFirstSelectedOption().getAttribute("label"), "Wrong default search field selected");

// Enter search value and perform search actions
importPage.enterTestSearchValue(TestConstants.KALLIOPE_PARENT_ID);
importPage.activateChildProcessImport();
importPage.decreaseImportDepth();
importPage.getSearchButton().click();
assertTrue(importPage.isHierarchyPanelVisible(), "Hierarchy panel should be visible");

// Ensure search button is clickable and retry if necessary
WebElement searchButton = null;
attempts = 0;
while (attempts < 3) {
try {
WebDriverWait wait = new WebDriverWait(Browser.getDriver(), 10);
searchButton = wait.until(ExpectedConditions.elementToBeClickable(importPage.getSearchButton()));
searchButton.click();
break; // exit loop if successful
} catch (StaleElementReferenceException e) {
attempts++;
System.out.println("Stale element detected for search button. Retrying... (" + attempts + ")");
}
}

// Ensure hierarchy panel is visible after performing search
WebElement hierarchyPanel = null;
attempts = 0;
while (attempts < 3) {
try {
WebDriverWait wait = new WebDriverWait(Browser.getDriver(), 10);
hierarchyPanel = wait.until(ExpectedConditions.visibilityOf(importPage.getHierarchyPanel()));
assertTrue(importPage.isHierarchyPanelVisible(), "Hierarchy panel should be visible");
break; // exit loop if successful
} catch (StaleElementReferenceException e) {
attempts++;
System.out.println("Stale element detected for hierarchy panel. Retrying... (" + attempts + ")");
}
}

// Add PPN and title
importPage.addPpnAndTitle();

// Ensure process title is fetched properly
String parentTitle = importPage.getProcessTitle();

// Save the process
Pages.getProcessFromTemplatePage().save();

// Apply the filter for the newly created process
processesPage.applyFilter(parentTitle);
assertEquals(1, processesPage.countListedProcesses(), "Exactly one imported parent process should be displayed");
List<String> processIds = processesPage.getProcessIds();
assertEquals(1, processIds.size(), "Exactly one process ID should be visible");

// Ensure processes are listed and retry if necessary
attempts = 0;
while (attempts < 3) {
try {
WebDriverWait wait = new WebDriverWait(Browser.getDriver(), 10);
assertEquals(1, processesPage.countListedProcesses(), "Exactly one imported parent process should be displayed");
break; // exit loop if successful
} catch (StaleElementReferenceException e) {
attempts++;
System.out.println("Stale element detected for process listing. Retrying... (" + attempts + ")");
}
}

// Retrieve and validate process IDs
List<String> processIds = null;
attempts = 0;
while (attempts < 3) {
try {
processIds = processesPage.getProcessIds();
assertEquals(1, processIds.size(), "Exactly one process ID should be visible");
break; // exit loop if successful
} catch (StaleElementReferenceException e) {
attempts++;
System.out.println("Stale element detected when retrieving process IDs. Retrying... (" + attempts + ")");
}
}

// Get the first process ID and filter by child processes
int processId = Integer.parseInt(processIds.get(0));
processesPage.filterByChildren();
List<String> childProcessIds = processesPage.getProcessIds();
assertEquals(3, childProcessIds.size(), "Wrong number of child processes");

// Ensure child processes are listed and retry if necessary
attempts = 0;
while (attempts < 3) {
try {
WebDriverWait wait = new WebDriverWait(Browser.getDriver(), 10);
List<String> childProcessIds = processesPage.getProcessIds();
assertEquals(3, childProcessIds.size(), "Wrong number of child processes");
break; // exit loop if successful
} catch (StaleElementReferenceException e) {
attempts++;
System.out.println("Stale element detected for child process listing. Retrying... (" + attempts + ")");
}
}

// Remove the test process
ProcessTestUtils.removeTestProcess(processId);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ private void addMetadata(String type, String value, String addButton) {
getById(METADATA_VALUE).sendKeys(value);
getById(CALENDAR_DIALOG_CLOSE_BUTTON).click();
}

public List<WebElement> getMetadataElements(String issueName) {
// Locate the metadata elements for the given issueName and return them as a list
return Browser.getDriver().findElements(By.xpath("//table//tr[contains(., '" + issueName + "')]//td"));
}

public List<String> extractMetadataFromElements(List<WebElement> elements) {
// Extract text from each WebElement and return as a list of strings
return elements.stream().map(WebElement::getText).collect(Collectors.toList());
}

private WebElement getIssue(String name) {
return getByXPath( "//div[text()='" + name + " erschien']");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ public String getProcessTitle() {
return processTitleInput.getAttribute(TestConstants.VALUE);
}

/**
* Check and return whether hierarchy panel is visible or not after triggering catalog import.
* @return whether hierarchy panel is visible
*/
public WebElement getHierarchyPanel() {
return Browser.getDriver().findElement(By.id(HIERARCHY_PANEL));
}


/**
* Check and return whether hierarchy panel is visible or not after triggering catalog import.
* @return whether hierarchy panel is visible
Expand Down

0 comments on commit 42f5120

Please sign in to comment.