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

ListSelectElement.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.By;
  20. import org.openqa.selenium.JavascriptExecutor;
  21. import org.openqa.selenium.WebElement;
  22. import org.openqa.selenium.support.ui.Select;
  23. import com.vaadin.testbench.elementsbase.ServerClass;
  24. @ServerClass("com.vaadin.ui.ListSelect")
  25. public class ListSelectElement extends AbstractSelectElement {
  26. private Select select;
  27. private static By bySelect = By.tagName("select");
  28. private WebElement selectElement;
  29. @Override
  30. protected void init() {
  31. super.init();
  32. selectElement = findElement(bySelect);
  33. select = new Select(selectElement);
  34. }
  35. /**
  36. * Selects the option(s) with the given text.
  37. * <p>
  38. * For a ListSelect in multi select mode, adds the given option(s) to the
  39. * current selection.
  40. *
  41. * @param text
  42. * the text of the option
  43. */
  44. public void selectByText(String text) {
  45. select.selectByVisibleText(text);
  46. if (isPhantomJS() && select.isMultiple()) {
  47. // Phantom JS does not fire a change event when
  48. // selecting/deselecting items in a multi select
  49. fireChangeEvent(selectElement);
  50. }
  51. }
  52. /**
  53. * Deselects the option(s) with the given text.
  54. *
  55. * @param text
  56. * the text of the option
  57. */
  58. public void deselectByText(String text) {
  59. select.deselectByVisibleText(text);
  60. if (isPhantomJS() && select.isMultiple()) {
  61. // Phantom JS does not fire a change event when
  62. // selecting/deselecting items in a multi select
  63. fireChangeEvent(selectElement);
  64. }
  65. }
  66. /**
  67. * Gets a list of the texts shown for all options.
  68. *
  69. * @return a list of option texts
  70. */
  71. public List<String> getOptions() {
  72. List<String> options = new ArrayList<String>();
  73. for (WebElement webElement : select.getOptions()) {
  74. options.add(webElement.getText());
  75. }
  76. return options;
  77. }
  78. /**
  79. * Clear operation is not supported for List Select. This operation has no
  80. * effect on List Select element.
  81. */
  82. @Override
  83. public void clear() {
  84. super.clear();
  85. }
  86. /**
  87. * Return value of the list select element
  88. *
  89. * @return value of the list select element
  90. */
  91. public String getValue() {
  92. return select.getFirstSelectedOption().getText();
  93. }
  94. private void fireChangeEvent(WebElement target) {
  95. if (!(getDriver() instanceof JavascriptExecutor)) {
  96. return;
  97. }
  98. ((JavascriptExecutor) getDriver()).executeScript(
  99. "var ev = document.createEvent('HTMLEvents');" //
  100. + "ev.initEvent('change', false, true);" //
  101. + "arguments[0].dispatchEvent(ev);", //
  102. target);
  103. }
  104. }