From 12793c9667cb2708ddfbcfe0d94364dd0dc607e3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pekka=20Hyv=C3=B6nen?= Date: Tue, 13 Dec 2016 16:13:05 +0200 Subject: [PATCH] Fix broken test for RadioButtonGroup and CheckBoxGroup (#7970) Selection by clicking options was broken on Chrome 40 on Windows. Switched the custom element to extend corresponding elements from TestBench API. --- .../customelements/CheckBoxGroupElement.java | 36 +-------------- .../RadioButtonGroupElement.java | 44 +++---------------- .../CheckBoxGroupFocusBlurTest.java | 6 +-- .../RadioButtonGroupFocusBlurTest.java | 13 +++--- 4 files changed, 18 insertions(+), 81 deletions(-) diff --git a/uitest-common/src/main/java/com/vaadin/testbench/customelements/CheckBoxGroupElement.java b/uitest-common/src/main/java/com/vaadin/testbench/customelements/CheckBoxGroupElement.java index 9b234fa1cd..e8a05cb194 100644 --- a/uitest-common/src/main/java/com/vaadin/testbench/customelements/CheckBoxGroupElement.java +++ b/uitest-common/src/main/java/com/vaadin/testbench/customelements/CheckBoxGroupElement.java @@ -21,31 +21,19 @@ import java.util.List; import org.openqa.selenium.WebElement; import com.vaadin.testbench.By; -import com.vaadin.testbench.elements.AbstractSelectElement; -import com.vaadin.testbench.elementsbase.ServerClass; /** * TestBench element supporting CheckBoxGroup * * @author Vaadin Ltd */ - -@ServerClass("com.vaadin.ui.CheckBoxGroup") -public class CheckBoxGroupElement extends AbstractSelectElement { +public class CheckBoxGroupElement + extends com.vaadin.testbench.elements.CheckBoxGroupElement { private static org.openqa.selenium.By byButtonSpan = By .className("v-select-option"); private static org.openqa.selenium.By byLabel = By.tagName("label"); private static org.openqa.selenium.By byInput = By.tagName("input"); - public List getOptions() { - List optionTexts = new ArrayList<>(); - List options = findElements(byButtonSpan); - for (WebElement option : options) { - optionTexts.add(option.findElement(byLabel).getText()); - } - return optionTexts; - } - public List getOptionsCssClasses() { List optionTexts = new ArrayList<>(); List options = findElements(byButtonSpan); @@ -70,26 +58,6 @@ public class CheckBoxGroupElement extends AbstractSelectElement { return icons; } - public void selectByText(String text) throws ReadOnlyException { - if (isReadOnly()) { - throw new ReadOnlyException(); - } - List options = findElements(byButtonSpan); - for (int i = 0; i < options.size(); i++) { - WebElement option = options.get(i); - if (text.equals(option.findElement(byLabel).getText())) { - option.findElement(byInput).click(); - - // Seems like this is needed because of #19753 - waitForVaadin(); - - // Toggling selection causes the DOM to be rebuilt, so fetch new - // items and continue iterating from the same index - options = findElements(byButtonSpan); - } - } - } - /** * Return list of the selected options in the checkbox group * diff --git a/uitest-common/src/main/java/com/vaadin/testbench/customelements/RadioButtonGroupElement.java b/uitest-common/src/main/java/com/vaadin/testbench/customelements/RadioButtonGroupElement.java index f6bb4d93f7..ce62ddd4be 100644 --- a/uitest-common/src/main/java/com/vaadin/testbench/customelements/RadioButtonGroupElement.java +++ b/uitest-common/src/main/java/com/vaadin/testbench/customelements/RadioButtonGroupElement.java @@ -15,55 +15,23 @@ */ package com.vaadin.testbench.customelements; -import com.vaadin.testbench.By; -import com.vaadin.testbench.elements.AbstractSelectElement; -import com.vaadin.testbench.elementsbase.ServerClass; -import org.openqa.selenium.WebElement; - import java.util.ArrayList; import java.util.List; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.By; + /** * TestBench element supporting RadioButtonGroup * * @author Vaadin Ltd */ -@ServerClass("com.vaadin.ui.RadioButtonGroup") -public class RadioButtonGroupElement extends AbstractSelectElement { +public class RadioButtonGroupElement + extends com.vaadin.testbench.elements.RadioButtonGroupElement { private static org.openqa.selenium.By byButtonSpan = By .className("v-select-option"); - private static org.openqa.selenium.By byLabel = By.tagName("label"); - private static org.openqa.selenium.By byInput = By.tagName("input"); - - public List getOptions() { - List optionTexts = new ArrayList<>(); - List options = findElements(byButtonSpan); - for (WebElement option : options) { - optionTexts.add(option.findElement(byLabel).getText()); - } - return optionTexts; - } - - public void selectByText(String text) throws ReadOnlyException { - if (isReadOnly()) { - throw new ReadOnlyException(); - } - List options = findElements(byButtonSpan); - for (int i = 0; i < options.size(); i++) { - WebElement option = options.get(i); - if (text.equals(option.findElement(byLabel).getText())) { - option.findElement(byInput).click(); - - // Seems like this is needed because of #19753 - waitForVaadin(); - - // Toggling selection causes the DOM to be rebuilt, so fetch new - // items and continue iterating from the same index - options = findElements(byButtonSpan); - } - } - } /** * Return list of the selected options in the radiobutton group diff --git a/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupFocusBlurTest.java b/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupFocusBlurTest.java index e0929eb478..734a80eb2a 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupFocusBlurTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupFocusBlurTest.java @@ -40,12 +40,12 @@ public class CheckBoxGroupFocusBlurTest extends MultiBrowserTest { List checkBoxes = $(CheckBoxGroupElement.class).first() .findElements(By.tagName("input")); - checkBoxes.get(0).click(); + $(CheckBoxGroupElement.class).first().selectByText("1"); // Focus event is fired Assert.assertTrue(logContainsText("1. Focus Event")); - checkBoxes.get(1).click(); + $(CheckBoxGroupElement.class).first().selectByText("2"); // click on the second checkbox doesn't fire anything Assert.assertFalse(logContainsText("2.")); @@ -71,7 +71,7 @@ public class CheckBoxGroupFocusBlurTest extends MultiBrowserTest { // blur event is fired Assert.assertTrue(logContainsText("2. Blur Event")); - checkBoxes.get(3).click(); + $(CheckBoxGroupElement.class).first().selectByText("4"); // Focus event is fired Assert.assertTrue(logContainsText("3. Focus Event")); diff --git a/uitest/src/test/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupFocusBlurTest.java b/uitest/src/test/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupFocusBlurTest.java index 30ebf6abd7..16889cfbf0 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupFocusBlurTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/radiobuttongroup/RadioButtonGroupFocusBlurTest.java @@ -38,14 +38,16 @@ public class RadioButtonGroupFocusBlurTest extends MultiBrowserTest { public void focusBlurEvents() { openTestURL(); - List radioButtons = $(RadioButtonGroupElement.class).first() + RadioButtonGroupElement radioButtonGroup = $( + RadioButtonGroupElement.class).first(); + List radioButtons = radioButtonGroup .findElements(By.tagName("input")); - radioButtons.get(0).click(); + radioButtonGroup.selectByText("1"); // Focus event is fired Assert.assertTrue(logContainsText("1. Focus Event")); - radioButtons.get(1).click(); + radioButtonGroup.selectByText("2"); // click on the second radio button doesn't fire anything Assert.assertFalse(logContainsText("2.")); @@ -60,8 +62,7 @@ public class RadioButtonGroupFocusBlurTest extends MultiBrowserTest { Assert.assertFalse(logContainsText("2.")); // click to label of a radio button - $(RadioButtonGroupElement.class).first() - .findElements(By.tagName("label")).get(2).click(); + radioButtonGroup.findElements(By.tagName("label")).get(2).click(); // no new events Assert.assertFalse(logContainsText("2.")); @@ -70,7 +71,7 @@ public class RadioButtonGroupFocusBlurTest extends MultiBrowserTest { // blur event is fired Assert.assertTrue(logContainsText("2. Blur Event")); - radioButtons.get(3).click(); + radioButtonGroup.selectByText("4"); // Focus event is fired Assert.assertTrue(logContainsText("3. Focus Event")); -- 2.39.5