Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

TwinColSelectElement.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.testbench.elements;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.openqa.selenium.WebElement;
  20. import org.openqa.selenium.support.ui.Select;
  21. import com.vaadin.testbench.By;
  22. import com.vaadin.testbench.elementsbase.ServerClass;
  23. @ServerClass("com.vaadin.ui.TwinColSelect")
  24. public class TwinColSelectElement extends AbstractSelectElement {
  25. private Select options;
  26. private Select selectedOptions;
  27. private WebElement deselButton;
  28. private WebElement selButton;
  29. private static org.openqa.selenium.By bySelect = By.tagName("select");
  30. private static org.openqa.selenium.By byButton = By.className("v-button");
  31. private WebElement optionsElement;
  32. private WebElement selectionsElement;
  33. @Override
  34. protected void init() {
  35. super.init();
  36. List<WebElement> selectElements = findElements(bySelect);
  37. optionsElement = selectElements.get(0);
  38. selectionsElement = selectElements.get(1);
  39. options = new Select(optionsElement);
  40. selectedOptions = new Select(selectionsElement);
  41. List<WebElement> buttons = findElements(byButton);
  42. selButton = buttons.get(0);
  43. deselButton = buttons.get(1);
  44. }
  45. private void deselectAll() {
  46. if (selectedOptions.isMultiple()) {
  47. if (selectedOptions.getAllSelectedOptions()
  48. .size() != selectedOptions.getOptions().size()) {
  49. for (int i = 0, l = selectedOptions.getOptions()
  50. .size(); i < l; ++i) {
  51. selectedOptions.selectByIndex(i);
  52. }
  53. }
  54. deselButton.click();
  55. }
  56. while (!selectedOptions.getOptions().isEmpty()) {
  57. selectedOptions.selectByIndex(0);
  58. deselButton.click();
  59. }
  60. }
  61. /**
  62. * Deselects the option with the given option text, i.e. removes it from the
  63. * right side column.
  64. *
  65. * @param text
  66. * the text of the option to deselect
  67. */
  68. public void deselectByText(String text) {
  69. if (isReadOnly()) {
  70. throw new ReadOnlyException();
  71. }
  72. selectedOptions.deselectAll();
  73. selectedOptions.selectByVisibleText(text);
  74. deselButton.click();
  75. }
  76. /**
  77. * Functionality to find option texts of all currently selected options.
  78. *
  79. * @return List of visible text for all selected options
  80. */
  81. public List<String> getValues() {
  82. return getOptionsFromSelect(selectedOptions);
  83. }
  84. /**
  85. * Functionality to find all option texts.
  86. *
  87. * @return List of visible text for all options
  88. */
  89. public List<String> getOptions() {
  90. List<String> optionTexts = getOptionsFromSelect(options);
  91. optionTexts.addAll(getValues());
  92. return optionTexts;
  93. }
  94. /**
  95. * Gets the available option texts, i.e. all values which have not been
  96. * selected.
  97. *
  98. * @return List of visible text for available options
  99. */
  100. public List<String> getAvailableOptions() {
  101. return getOptionsFromSelect(options);
  102. }
  103. /**
  104. * Selects the option with the given option text, i.e. adds it to the right
  105. * side column.
  106. *
  107. * @param text
  108. * the text of the option to select
  109. */
  110. public void selectByText(String text) {
  111. if (isReadOnly()) {
  112. throw new ReadOnlyException();
  113. }
  114. options.deselectAll();
  115. options.selectByVisibleText(text);
  116. selButton.click();
  117. }
  118. private List<String> getOptionsFromSelect(Select select) {
  119. List<String> optionTexts = new ArrayList<String>();
  120. for (WebElement option : select.getOptions()) {
  121. optionTexts.add(option.getText());
  122. }
  123. return optionTexts;
  124. }
  125. /**
  126. * Return first selected item (item in the right part of component).
  127. *
  128. * @return the option text for the item
  129. */
  130. public String getValue() {
  131. String value = "";
  132. WebElement selectedElement = findElement(
  133. By.className("v-select-twincol-selections"));
  134. List<WebElement> optionElements = selectedElement
  135. .findElements(By.tagName("option"));
  136. if (!optionElements.isEmpty()) {
  137. value = optionElements.get(0).getText();
  138. }
  139. return value;
  140. }
  141. @Override
  142. public void clear() {
  143. deselectAll();
  144. }
  145. /**
  146. * Gets the left {@code <select>} element inside the component, containing
  147. * the available options.
  148. *
  149. * @return the select element containing options inside the component
  150. * @since 8.1.1
  151. */
  152. public WebElement getOptionsElement() {
  153. return optionsElement;
  154. }
  155. /**
  156. * Gets the right {@code <select>} element inside the component, containing
  157. * the selected options.
  158. *
  159. * @return the select element containing selection inside the component
  160. * @since 8.1.1
  161. */
  162. public WebElement getSelectionsElement() {
  163. return selectionsElement;
  164. }
  165. }