Skip to content

Commit

Permalink
test: add grid radio selection column tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flang authored and paodb committed Feb 21, 2024
1 parent 4182d10 commit f2b033d
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<drivers.dir>${project.basedir}/drivers</drivers.dir>
<jetty.version>9.4.36.v20210114</jetty.version>
<webdrivermanager.version>3.8.1</webdrivermanager.version>
<driver-binary-downloader-maven-plugin.version>1.0.17</driver-binary-downloader-maven-plugin.version>
</properties>

<organization>
Expand Down Expand Up @@ -158,7 +160,7 @@
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
<version>${webdrivermanager.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
Expand Down Expand Up @@ -330,7 +332,7 @@
<configuration>
<quiet>true</quiet>
<doclint>none</doclint>
<additionalparam>-Xdoclint:none</additionalparam>
<additionalparam>-Xdoclint:none</additionalparam>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -414,9 +416,8 @@

<plugin>
<groupId>com.lazerycode.selenium</groupId>
<artifactId>driver-binary-downloader-maven-plugin
</artifactId>
<version>1.0.17</version>
<artifactId>driver-binary-downloader-maven-plugin</artifactId>
<version>${driver-binary-downloader-maven-plugin.version}</version>
<configuration>
<onlyGetDriversForHostOperatingSystem>
true
Expand Down Expand Up @@ -563,7 +564,18 @@
<vaadin.version>24.2.6</vaadin.version>
<jetty.version>11.0.12</jetty.version>
<frontend.hotdeploy>true</frontend.hotdeploy>
<webdrivermanager.version>5.6.3</webdrivermanager.version>
<driver-binary-downloader-maven-plugin.version>1.0.18</driver-binary-downloader-maven-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.17.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>

</profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ public TestBenchElement getSelectionCheckbox(int rowIndex) {
return (TestBenchElement) elements.stream().findFirst().orElse(null);
}

public TestBenchElement getSelectionRadioButton(int rowIndex) {
// assumes that Grid is in single-selection mode
TestBenchElement cell = getSlottedCell(getRow(rowIndex));
List<WebElement> elements = cell.findElements(By.tagName("vaadin-radio-button"));
return (TestBenchElement) elements.stream().findFirst().orElse(null);
}


private TestBenchElement getSlottedCell(WebElement e) {
String slot = e.findElement(By.tagName("slot")).getAttribute("name");
return findElement(By.cssSelector(String.format("[slot='%s']", slot)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*-
* #%L
* Grid Helpers Add-on
* %%
* Copyright (C) 2022 - 2024 Flowing Code
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

package com.flowingcode.vaadin.addons.gridhelpers.it;

import static java.time.temporal.ChronoUnit.SECONDS;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import com.flowingcode.vaadin.testbench.rpc.HasRpcSupport;
import com.vaadin.flow.component.grid.testbench.GridElement;
import com.vaadin.testbench.TestBenchElement;
import java.time.Duration;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class GridRadioSelectionColumnIT extends AbstractViewTest implements HasRpcSupport {

IntegrationViewCallables $server = createCallableProxy(IntegrationViewCallables.class);
GridHelperElement grid;

public GridRadioSelectionColumnIT() {
super("it");
}

@Override
public void setup() throws Exception {
super.setup();
grid = new GridHelperElement($(GridElement.class).waitForFirst());
}

@Test
public void testRadioSelectionColumnShown() {
// assert that the first column is not the one with radio button
assertNull("the first column has radio buttons", grid.getSelectionRadioButton(0));

$server.showRadioSelectionColumn();

waitUntil(ExpectedConditions
.presenceOfElementLocated(By.tagName("grid-flow-radio-selection-column")));

// assert that the first column is the one with radio buttons
assertNotNull("the first column has not any radio buttons", grid.getSelectionRadioButton(0));
}

@Test
public void testShouldSelectRowOnClick() {
$server.showRadioSelectionColumn();

waitUntil(ExpectedConditions
.presenceOfElementLocated(By.tagName("grid-flow-radio-selection-column")));

assertThat($server.getSelectedRows(), empty());

grid.getCell(0, 1).click();
assertThat($server.getSelectedRows(), contains(0));

grid.getCell(4, 1).click();
assertThat($server.getSelectedRows(), contains(4));
}

@Test
public void testShouldSelectRowOnRadioButtonClick() {
$server.showRadioSelectionColumn();

waitUntil(ExpectedConditions
.presenceOfElementLocated(By.tagName("grid-flow-radio-selection-column")));


assertThat($server.getSelectedRows(), empty());

TestBenchElement radioButton = grid.getSelectionRadioButton(0);
radioButton.click();
assertThat($server.getSelectedRows(), contains(0));


radioButton = grid.getSelectionRadioButton(4);
radioButton.click();
assertThat($server.getSelectedRows(), contains(4));
}

@Test
public void testRadioSelectionColumnFrozen() {
$server.showRadioSelectionColumn();

waitUntil(ExpectedConditions
.presenceOfElementLocated(By.tagName("grid-flow-radio-selection-column")));

TestBenchElement selectionColElement = grid.$("grid-flow-radio-selection-column").first();

String frozen = "return arguments[0].frozen";


new WebDriverWait(getDriver(), Duration.of(2, SECONDS))
.until(ExpectedConditions.attributeContains(selectionColElement, "frozen", "false"));

// assert that the selection column is not frozen by default
assertFalse("Selection column is frozen", (Boolean) executeScript(frozen, selectionColElement));

// make selection column frozen
selectionColElement.setProperty("frozen", true);

new WebDriverWait(getDriver(), Duration.of(2, SECONDS))
.until(ExpectedConditions.attributeContains(selectionColElement, "frozen", "true"));

// assert that the selection column is now frozen
assertTrue("Selection column is not frozen",
(Boolean) executeScript(frozen, selectionColElement));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.flowingcode.vaadin.addons.gridhelpers.it;

import com.flowingcode.vaadin.addons.gridhelpers.GridHelper;
import com.flowingcode.vaadin.addons.gridhelpers.GridRadioSelectionColumn;
import com.flowingcode.vaadin.addons.gridhelpers.Person;
import com.flowingcode.vaadin.addons.gridhelpers.TestData;
import com.flowingcode.vaadin.testbench.rpc.JsonArrayList;
Expand Down Expand Up @@ -178,4 +179,9 @@ public void setHeaderVisible(boolean visible) {
public void setFooterVisible(boolean visible) {
grid.setFooterVisible(visible);
}

@Override
public void showRadioSelectionColumn() {
grid.showRadioSelectionColumn();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ public interface IntegrationViewCallables extends RmiCallable {

void setFooterVisible(boolean visible);

void showRadioSelectionColumn();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import com.flowingcode.vaadin.addons.gridhelpers.GridHelper;
Expand Down Expand Up @@ -190,4 +191,9 @@ public void testMenuToggleColumn() {
assertTrue(GridHelper.isMenuToggleColumn(toggleColumn));
}

@Test
public void testShowRadioSelectionColumn() {
assertNotNull(grid.showRadioSelectionColumn());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void testSerialization() throws ClassNotFoundException, IOException {
try {
Grid<?> grid = new Grid<>();
GridHelper.setColumnToggleVisible(grid, false);
GridHelper.showRadioSelectionColumn(grid);
testSerializationOf(grid);
} catch (Exception e) {
Assert.fail("Problem while testing serialization: " + e.getMessage());
Expand Down

0 comments on commit f2b033d

Please sign in to comment.