Skip to content

Commit 38ff94c

Browse files
committed
test: add view for integration tests
1 parent 74e13fd commit 38ff94c

File tree

4 files changed

+129
-7
lines changed

4 files changed

+129
-7
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.flowingcode.vaadin.addons.chipfield.integration;
2+
3+
import java.util.concurrent.TimeoutException;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
6+
7+
import org.junit.Before;
8+
import org.openqa.selenium.By;
9+
import org.openqa.selenium.JavascriptExecutor;
10+
import org.openqa.selenium.WebElement;
11+
import org.openqa.selenium.support.ui.ExpectedConditions;
12+
import org.openqa.selenium.support.ui.WebDriverWait;
13+
14+
import com.vaadin.flow.component.ClientCallable;
15+
16+
public class AbstractChipfieldTest extends AbstractViewTest {
17+
18+
protected ChipFieldElement chipfield;
19+
20+
protected AbstractChipfieldTest() {
21+
super("it");
22+
}
23+
24+
@Before
25+
public void before() {
26+
chipfield = $(ChipFieldElement.class).first();
27+
}
28+
/**
29+
* Call a {@link ClientCallable} defined on the integration view.
30+
*
31+
* @param callable the client callable name
32+
* @param arguments arguments to be passed to the callable
33+
* @throws TimeoutException if the callable doesn't complete in 2 seconds.
34+
* @throws RuntimeException if the callable fails.
35+
*/
36+
protected final void call(String callable, Object... arguments) {
37+
WebElement view = getDriver().findElement(By.id("view"));
38+
String result = "data-callable-result";
39+
40+
StringBuilder script = new StringBuilder();
41+
script.append("var view = arguments[0];");
42+
script.append("var callable = arguments[1];");
43+
script.append("var result = arguments[2];");
44+
script.append("view.removeAttribute(result);");
45+
script.append("view.$server[callable](...arguments[3])");
46+
script.append(" .then(()=>view.setAttribute(result,true))");
47+
script.append(" .catch(()=>view.setAttribute(result,false));");
48+
49+
((JavascriptExecutor) getDriver()).executeScript(script.toString(), view, callable, result, arguments);
50+
51+
new WebDriverWait(getDriver(), 2, 100).until(ExpectedConditions.attributeToBeNotEmpty(view, result));
52+
if (!Boolean.parseBoolean(view.getAttribute(result))) {
53+
throw new RuntimeException(
54+
String.format("server call failed: %s(%s)", callable, Stream.of(arguments).map(Object::toString).collect(Collectors.joining(","))));
55+
}
56+
}
57+
58+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
package com.flowingcode.vaadin.addons.chipfield.integration;
22

3+
import java.util.List;
4+
5+
import org.openqa.selenium.Keys;
6+
37
import com.vaadin.testbench.TestBenchElement;
48
import com.vaadin.testbench.elementsbase.Element;
59

610
@Element("paper-chip-input-autocomplete")
711
public class ChipFieldElement extends TestBenchElement {
812

13+
@SuppressWarnings("unchecked")
14+
List<String> getValue() {
15+
return (List<String>) getProperty("items");
16+
}
17+
18+
@Override
19+
public void sendKeys(CharSequence... keysToSend) {
20+
$("paper-input").first().$("input").first().sendKeys(keysToSend);
21+
}
22+
23+
public void selectByText(String label) {
24+
sendKeys(label, Keys.ARROW_DOWN, Keys.ENTER);
25+
}
26+
927
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.flowingcode.vaadin.addons.chipfield.integration;
2+
3+
import java.util.Arrays;
4+
5+
import com.flowingcode.vaadin.addons.chipfield.ChipField;
6+
import com.vaadin.flow.component.ClientCallable;
7+
import com.vaadin.flow.component.html.Div;
8+
import com.vaadin.flow.router.Route;
9+
10+
@Route("it")
11+
public class IntegrationView extends Div {
12+
13+
private ChipField<String> field;
14+
15+
public IntegrationView() {
16+
setId("view");
17+
add(field = new ChipField<>("Field"));
18+
field.setItems("Lorem", "Ipsum");
19+
}
20+
21+
@ClientCallable
22+
private void testCallable(boolean arg) {
23+
if (!arg) {
24+
throw new IllegalArgumentException();
25+
}
26+
}
27+
28+
}
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
package com.flowingcode.vaadin.addons.chipfield.integration;
22

33
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.junit.Assert.assertThrows;
45

5-
import org.junit.Test;
6+
import java.util.Arrays;
67

7-
public class ViewIT extends AbstractViewTest {
8+
import org.hamcrest.Matchers;
9+
import org.junit.Test;
810

9-
public ViewIT() {
10-
super("it");
11-
}
11+
public class ViewIT extends AbstractChipfieldTest {
1212

1313
@Test
14-
public void upgradedToCustomElement() {
14+
public void testUpgradedToCustomElement() {
1515
ChipFieldElement chipfield = $(ChipFieldElement.class).first();
1616
assertThat(chipfield, hasBeenUpgradedToCustomElement);
17-
}
17+
}
18+
19+
@Test
20+
public void testCallableSuccess() {
21+
// test that the callable mechanism worls
22+
call("testCallable", true);
23+
}
24+
25+
@Test
26+
public void testCallableFailure() {
27+
// test that the callable mechanism detect failures
28+
assertThrows(RuntimeException.class, () -> call("testCallable", false));
29+
}
30+
31+
@Test
32+
public void testCallableFailure2() {
33+
// test that the callable mechanism detect failures
34+
assertThrows(RuntimeException.class, () -> call("testCallable"));
35+
}
1836

1937
}

0 commit comments

Comments
 (0)