From 907c17905aacc3bddc06c1460109bece8945f7c8 Mon Sep 17 00:00:00 2001 From: Artur Date: Mon, 29 Jan 2018 11:09:50 +0200 Subject: [PATCH] Add hasAttribute helper (#980) --- .../java/com/vaadin/testUI/ElementQueryView.java | 7 ++++++- .../vaadin/tests/elements/BasicElementIT.java | 16 ++++++++++++++++ .../com/vaadin/testbench/TestBenchElement.java | 12 ++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/vaadin-testbench-integration-tests/src/main/java/com/vaadin/testUI/ElementQueryView.java b/vaadin-testbench-integration-tests/src/main/java/com/vaadin/testUI/ElementQueryView.java index c211e2752..658ead787 100644 --- a/vaadin-testbench-integration-tests/src/main/java/com/vaadin/testUI/ElementQueryView.java +++ b/vaadin-testbench-integration-tests/src/main/java/com/vaadin/testUI/ElementQueryView.java @@ -9,7 +9,12 @@ public class ElementQueryView extends Div { public ElementQueryView() { for (int i = 0; i < 10; i++) { - add(new Div(new NativeButton("Button " + i))); + NativeButton button = new NativeButton("Button " + i); + add(new Div(button)); + if (i == 5) { + button.getElement().setAttribute("boolean", true); + button.getElement().setAttribute("string", "value"); + } } } diff --git a/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/elements/BasicElementIT.java b/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/elements/BasicElementIT.java index a398b81c7..95ef572ce 100644 --- a/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/elements/BasicElementIT.java +++ b/vaadin-testbench-integration-tests/src/test/java/com/vaadin/tests/elements/BasicElementIT.java @@ -98,4 +98,20 @@ public void waitForNonExistant() { $(PolymerTemplateViewElement.class).waitForFirst(); Assert.fail("Should not have found an element which does not exist"); } + + @Test + public void hasAttribute() { + NativeButtonElement withAttributes = $(NativeButtonElement.class) + .get(5); + NativeButtonElement withoutAttributes = $(NativeButtonElement.class) + .get(6); + + Assert.assertTrue(withAttributes.hasAttribute("string")); + Assert.assertTrue(withAttributes.hasAttribute("boolean")); + Assert.assertFalse(withAttributes.hasAttribute("nonexistant")); + + Assert.assertFalse(withoutAttributes.hasAttribute("string")); + Assert.assertFalse(withoutAttributes.hasAttribute("boolean")); + Assert.assertFalse(withoutAttributes.hasAttribute("nonexistant")); + } } diff --git a/vaadin-testbench/src/main/java/com/vaadin/testbench/TestBenchElement.java b/vaadin-testbench/src/main/java/com/vaadin/testbench/TestBenchElement.java index 0c5f49700..9b0872508 100644 --- a/vaadin-testbench/src/main/java/com/vaadin/testbench/TestBenchElement.java +++ b/vaadin-testbench/src/main/java/com/vaadin/testbench/TestBenchElement.java @@ -225,6 +225,18 @@ public String getAttribute(String name) { return wrappedElement.getAttribute(name); } + /** + * Checks if the given attribute is present on the element. + * + * @param attribute + * the name of the attribute + * @return true if the attribute is present, false + * otherwise + */ + public boolean hasAttribute(String attribute) { + return (boolean) callFunction("hasAttribute", attribute); + } + @Override public boolean isSelected() { autoScrollIntoView();