Skip to content

Commit

Permalink
Add Selenium and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IkramMaalej committed Feb 20, 2023
1 parent ef9c929 commit 1f770b4
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,17 @@ public void setAllSelected(boolean allSelected) {
@SuppressWarnings("unchecked")
public List<Process> getSelectedProcesses() {
List<Process> selectedProcesses = new ArrayList<>();
if (selectedProcessesOrProcessDTOs.size() > 0) {
ProcessService processService = ServiceManager.getProcessService();
if (allSelected) {
try {
this.selectedProcessesOrProcessDTOs = processService.findByQuery(processService.getQueryForFilter(
this.isShowClosedProcesses(), isShowInactiveProjects(), getFilter())
.mustNot(processService.createSetQueryForIds(new ArrayList<>(excludedProcessIds))), false);
} catch (DataException e) {
logger.error(e.getMessage());
}
ProcessService processService = ServiceManager.getProcessService();
if (allSelected) {
try {
this.selectedProcessesOrProcessDTOs = processService.findByQuery(processService.getQueryForFilter(
this.isShowClosedProcesses(), isShowInactiveProjects(), getFilter())
.mustNot(processService.createSetQueryForIds(new ArrayList<>(excludedProcessIds))), false);
} catch (DataException e) {
logger.error(e.getMessage());
}
}
if (selectedProcessesOrProcessDTOs.size() > 0) {
if (selectedProcessesOrProcessDTOs.get(0) instanceof ProcessDTO) {
// list contains ProcessDTO instances
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<p:ajax event="toggleSelect"
listener="#{ProcessForm.selectAll}"/>

<p:column styleClass="checkboxListColumn" selectionMode="multiple" resizable="false"/>
<p:column id="selectionColumn" styleClass="checkboxListColumn" selectionMode="multiple" resizable="false"/>

<p:column styleClass="hierarchy-info">
<p:rowToggler rendered="#{process.getParentID() ne 0 or process.hasChildren() or ProcessForm.getCurrentTasksForUser(process).size() gt 0}"/>
Expand Down Expand Up @@ -253,10 +253,10 @@

</p:dataTable>

<p:overlayPanel widgetVar="allSelectableOverlayPanel" dismissable="true" styleClass="allSelectableOverlayPanel" >
<p:overlayPanel id="allSelectableOverlayPanel" widgetVar="allSelectableOverlayPanel" dismissable="true" styleClass="allSelectableOverlayPanel" >
<ul>
<li><p:commandLink value="#{msgs.selectAllRowsOnPage}" onclick="PF('processesTable').unselectAllRows();PF('processesTable').selectAllRowsOnPage();PF('allSelectableOverlayPanel').hide();"/></li>
<li><p:commandLink value="#{msgs.selectAllRows}" actionListener="#{ProcessForm.setAllSelected(true)}" onclick="PF('processesTable').selectAllRows();PF('allSelectableOverlayPanel').hide();"/></li>
<li><p:commandLink id="selectAllRowsOnPage" value="#{msgs.selectAllRowsOnPage}" onclick="PF('processesTable').unselectAllRows();PF('processesTable').selectAllRowsOnPage();PF('allSelectableOverlayPanel').hide();"/></li>
<li><p:commandLink id="selectAllRows" value="#{msgs.selectAllRows}" actionListener="#{ProcessForm.setAllSelected(true)}" onclick="PF('processesTable').selectAllRows();PF('allSelectableOverlayPanel').hide();"/></li>
</ul>
</p:overlayPanel>

Expand Down
112 changes: 112 additions & 0 deletions Kitodo/src/test/java/org/kitodo/production/forms/ProcessFormIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo.production.forms;

import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kitodo.MockDatabase;
import org.kitodo.SecurityTestUtils;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.database.beans.Project;
import org.kitodo.data.database.beans.Template;
import org.kitodo.production.services.ServiceManager;

public class ProcessFormIT {

private ProcessForm processForm = new ProcessForm();

/**
* Setup Database and start elasticsearch.
*
* @throws Exception If databaseConnection failed.
*/
@BeforeClass
public static void prepareDatabase() throws Exception {
MockDatabase.startNode();
MockDatabase.insertProcessesFull();
addProcesses();
SecurityTestUtils.addUserDataToSecurityContext(ServiceManager.getUserService().getById(1), 1);
}

private static void addProcesses() throws Exception {
Project projectOne = ServiceManager.getProjectService().getById(1);
Template template = ServiceManager.getTemplateService().getById(1);

Process forthProcess = new Process();
forthProcess.setTitle("Forth process");
LocalDate localDate = LocalDate.of(2020, 3, 20);
forthProcess.setCreationDate(Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
forthProcess.setWikiField("SelectionTest");
forthProcess.setDocket(ServiceManager.getDocketService().getById(1));
forthProcess.setProject(projectOne);
forthProcess.setRuleset(ServiceManager.getRulesetService().getById(1));
forthProcess.setTemplate(template);
ServiceManager.getProcessService().save(forthProcess);

Process fifthProcess = new Process();
fifthProcess.setTitle("Fifth process");
localDate = LocalDate.of(2020, 4, 20);
fifthProcess.setCreationDate(Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
fifthProcess.setWikiField("SelectionTest");
fifthProcess.setDocket(ServiceManager.getDocketService().getById(1));
fifthProcess.setProject(projectOne);
fifthProcess.setRuleset(ServiceManager.getRulesetService().getById(1));
fifthProcess.setTemplate(template);
ServiceManager.getProcessService().save(fifthProcess);
}

/**
* Cleanup the database and stop elasticsearch.
*
* @throws Exception if elasticsearch could not been stopped.
*/
@AfterClass
public static void cleanDatabase() throws Exception {
MockDatabase.stopNode();
MockDatabase.cleanDatabase();
}

/**
* Tests the selection in the process data table.
*/
@Test
public void testProcessesSelection() throws Exception {
List<Integer> selectedIds;
processForm.setAllSelected(true);

selectedIds = processForm.getSelectedProcesses()
.stream().map(Process::getId).sorted().collect(Collectors.toList());
Assert.assertEquals(new ArrayList<>(Arrays.asList(1, 2, 4, 5)), selectedIds);

processForm.getExcludedProcessIds().add(4);
selectedIds = processForm.getSelectedProcesses()
.stream().map(Process::getId).sorted().collect(Collectors.toList());

Assert.assertEquals(new ArrayList<>(Arrays.asList(1, 2, 5)), selectedIds);

processForm.setAllSelected(false);
processForm.selectedProcessesOrProcessDTOs = ServiceManager.getProcessService().findByAnything("SelectionTest");
selectedIds = processForm.getSelectedProcesses()
.stream().map(Process::getId).sorted().collect(Collectors.toList());
Assert.assertEquals(new ArrayList<>(Arrays.asList(4, 5)), selectedIds);
}
}
117 changes: 117 additions & 0 deletions Kitodo/src/test/java/org/kitodo/selenium/ProcessesSelectingST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo.selenium;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.kitodo.data.database.beans.Process;
import org.kitodo.data.database.beans.Project;
import org.kitodo.data.database.beans.Template;
import org.kitodo.data.database.beans.User;
import org.kitodo.production.services.ServiceManager;
import org.kitodo.selenium.testframework.BaseTestSelenium;
import org.kitodo.selenium.testframework.Pages;
import org.kitodo.selenium.testframework.pages.ProcessesPage;

public class ProcessesSelectingST extends BaseTestSelenium {

private static ProcessesPage processesPage;

/**
* Set up process selecting tests.
* @throws Exception as exception
*/
@BeforeClass
public static void setup() throws Exception {

User user = ServiceManager.getUserService().getById(1);
user.setTableSize(2);
ServiceManager.getUserService().saveToDatabase(user);
addProcesses();
processesPage = Pages.getProcessesPage();
}

private static void addProcesses() throws Exception {
Project projectOne = ServiceManager.getProjectService().getById(1);
Template template = ServiceManager.getTemplateService().getById(1);

Process forthProcess = new Process();
forthProcess.setTitle("Forth process");
LocalDate localDate = LocalDate.of(2020, 3, 20);
forthProcess.setCreationDate(Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
forthProcess.setWikiField("SelectionTest");
forthProcess.setDocket(ServiceManager.getDocketService().getById(1));
forthProcess.setProject(projectOne);
forthProcess.setRuleset(ServiceManager.getRulesetService().getById(1));
forthProcess.setTemplate(template);
ServiceManager.getProcessService().save(forthProcess);

Process fifthProcess = new Process();
fifthProcess.setTitle("Fifth process");
localDate = LocalDate.of(2020, 4, 20);
fifthProcess.setCreationDate(Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
fifthProcess.setWikiField("SelectionTest");
fifthProcess.setDocket(ServiceManager.getDocketService().getById(1));
fifthProcess.setProject(projectOne);
fifthProcess.setRuleset(ServiceManager.getRulesetService().getById(1));
fifthProcess.setTemplate(template);
ServiceManager.getProcessService().save(fifthProcess);
}

@Before
public void login() throws Exception {
Pages.getLoginPage().goTo().performLoginAsAdmin();
}

@After
public void logout() throws Exception {
Pages.getTopNavigation().logout();
}

/**
* Tests selection of all processes on one page in the processes data table.
* @throws Exception as exception
*/
@Test
public void selectAllProcessesOnPageTest() throws Exception {
processesPage.goTo();

processesPage.selectAllRowsOnPage();
assertEquals(processesPage.countListedSelectedProcesses(), 2);

processesPage.goToNextPage();
assertEquals(processesPage.countListedSelectedProcesses(), 0);
}

/**
*Tests selection of all processes in the processes data table.
* @throws Exception as exception
*/
@Test
public void selectAllProcessesTest() throws Exception {
processesPage.goTo();

processesPage.selectAllRows();
assertEquals(processesPage.countListedSelectedProcesses(), 2);

processesPage.goToNextPage();
assertEquals(processesPage.countListedSelectedProcesses(), 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ public static List<WebElement> getRowsOfTable(WebElement table) {
return table.findElements(By.tagName("tr"));
}

public static long getSelectedRowsOfTable(WebElement table) {
return getRowsOfTable(table)
.stream()
.filter(element -> element.getAttribute("aria-selected").equals("true"))
.count();
}

public static List<WebElement> getCellsOfRow(WebElement row) {
return row.findElements(By.tagName("td"));
}
Expand All @@ -234,7 +241,7 @@ public static List<String> getTableDataByColumn(WebElement table, int columnInde

public static String getCellDataByRow(WebElement row, int columnIndex) {
List<WebElement> cells = getCellsOfRow(row);
if(cells.size()<=columnIndex){
if (cells.size() <= columnIndex) {
return "";
}
return cells.get(columnIndex).getText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import static org.awaitility.Awaitility.await;
import static org.kitodo.selenium.testframework.Browser.getRowsOfTable;
import static org.kitodo.selenium.testframework.Browser.getSelectedRowsOfTable;
import static org.kitodo.selenium.testframework.Browser.getTableDataByColumn;

import java.io.File;
Expand Down Expand Up @@ -46,6 +47,8 @@ public class ProcessesPage extends Page<ProcessesPage> {
private static final String WAIT_FOR_ACTIONS_MENU = "Wait for actions menu to open";
private static final String WAIT_FOR_COLUMN_SORT = "Wait for column sorting";

private static final String WAIT_FOR_SELECTION_MENU = "Wait for process selection menu to open";

@SuppressWarnings("unused")
@FindBy(id = PROCESSES_TAB_VIEW)
private WebElement processesTabView;
Expand Down Expand Up @@ -148,6 +151,18 @@ public class ProcessesPage extends Page<ProcessesPage> {
@FindBy(id = FILTER_INPUT)
private WebElement filterInput;

@FindBy(css = ".ui-chkbox-all .ui-chkbox-box")
private WebElement selectAllCheckBox;

@FindBy(id = PROCESSES_FORM + ":selectAllRowsOnPage")
private WebElement selectAllRowsOnPageLink;

@FindBy(id = PROCESSES_FORM + ":selectAllRows")
private WebElement selectAllRowsLink;

@FindBy(className = "ui-paginator-next")
private WebElement nextPage;

public ProcessesPage() {
super("pages/processes.jsf");
}
Expand Down Expand Up @@ -192,6 +207,13 @@ public int countListedBatches() throws Exception {
return batchSelect.getOptions().size();
}

public long countListedSelectedProcesses() throws Exception {
if (!isAt()) {
goTo();
}
return getSelectedRowsOfTable(processesTable);
}

/**
* Returns a list of all processes titles which were displayed on process page.
*
Expand Down Expand Up @@ -437,4 +459,30 @@ public void clickProcessesTableHeaderForSorting(int column) {
.ignoreExceptions()
.until(() -> !columnHeader.getAttribute("aria-sort").equals(previousAriaSort));
}

public void selectAllRowsOnPage() {
selectAllCheckBox.click();
await(WAIT_FOR_SELECTION_MENU).pollDelay(700, TimeUnit.MILLISECONDS)
.atMost(30, TimeUnit.SECONDS).until(() -> selectAllRowsOnPageLink.isDisplayed());
selectAllRowsOnPageLink.click();
await("Wait for visible processes table").pollDelay(700, TimeUnit.MILLISECONDS)
.atMost(30, TimeUnit.SECONDS).until(() -> processesTable.isDisplayed());
}

public void selectAllRows() {
selectAllCheckBox.click();
await(WAIT_FOR_SELECTION_MENU).pollDelay(700, TimeUnit.MILLISECONDS)
.atMost(30, TimeUnit.SECONDS).until(() -> selectAllRowsLink.isDisplayed());
selectAllRowsLink.click();
await("Wait for visible processes table").pollDelay(700, TimeUnit.MILLISECONDS)
.atMost(30, TimeUnit.SECONDS).until(() -> processesTable.isDisplayed());
}

public void goToNextPage() {
if (nextPage.isEnabled()) {
nextPage.click();
await("Wait for visible processes table").pollDelay(700, TimeUnit.MILLISECONDS)
.atMost(30, TimeUnit.SECONDS).until(() -> processesTable.isDisplayed());
}
}
}

0 comments on commit 1f770b4

Please sign in to comment.