You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TwinColSelectElement.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. @Override
  32. protected void init() {
  33. super.init();
  34. List<WebElement> selectElements = findElements(bySelect);
  35. options = new Select(selectElements.get(0));
  36. selectedOptions = new Select(selectElements.get(1));
  37. List<WebElement> buttons = findElements(byButton);
  38. selButton = buttons.get(0);
  39. deselButton = buttons.get(1);
  40. }
  41. private void deselectAll() {
  42. if (selectedOptions.isMultiple()) {
  43. if (selectedOptions.getAllSelectedOptions()
  44. .size() != selectedOptions.getOptions().size()) {
  45. for (int i = 0, l = selectedOptions.getOptions()
  46. .size(); i < l; ++i) {
  47. selectedOptions.selectByIndex(i);
  48. }
  49. }
  50. deselButton.click();
  51. }
  52. while (selectedOptions.getOptions().size() > 0) {
  53. selectedOptions.selectByIndex(0);
  54. deselButton.click();
  55. }
  56. }
  57. public void deselectByText(String text) {
  58. selectedOptions.deselectAll();
  59. selectedOptions.selectByVisibleText(text);
  60. deselButton.click();
  61. }
  62. /**
  63. * Functionality to find option texts of all currently selected options.
  64. *
  65. * @return List of visible text for all selected options
  66. */
  67. public List<String> getValues() {
  68. return getOptionsFromSelect(selectedOptions);
  69. }
  70. /**
  71. * Functionality to find all option texts.
  72. *
  73. * @return List of visible text for all options
  74. */
  75. public List<String> getOptions() {
  76. List<String> optionTexts = getOptionsFromSelect(options);
  77. optionTexts.addAll(getValues());
  78. return optionTexts;
  79. }
  80. /**
  81. * Gets the available option texts, i.e. all values which have not been
  82. * selected.
  83. *
  84. * @return List of visible text for available options
  85. */
  86. public List<String> getAvailableOptions() {
  87. return getOptionsFromSelect(options);
  88. }
  89. public void selectByText(String text) {
  90. options.deselectAll();
  91. options.selectByVisibleText(text);
  92. selButton.click();
  93. }
  94. private List<String> getOptionsFromSelect(Select select) {
  95. List<String> optionTexts = new ArrayList<String>();
  96. for (WebElement option : select.getOptions()) {
  97. optionTexts.add(option.getText());
  98. }
  99. return optionTexts;
  100. }
  101. /**
  102. * Return first selected item (item in the right part of component)
  103. */
  104. public String getValue() {
  105. String value = "";
  106. WebElement selectedElement = findElement(
  107. By.className("v-select-twincol-selections"));
  108. List<WebElement> optionElements = selectedElement
  109. .findElements(By.tagName("option"));
  110. if (!optionElements.isEmpty()) {
  111. value = optionElements.get(0).getText();
  112. }
  113. return value;
  114. }
  115. @Override
  116. public void clear() {
  117. deselectAll();
  118. }
  119. }