123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- /*
- * Copyright 2000-2016 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.vaadin.testbench.elements;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.support.ui.Select;
-
- import com.vaadin.testbench.By;
- import com.vaadin.testbench.elementsbase.ServerClass;
-
- @ServerClass("com.vaadin.ui.TwinColSelect")
- public class TwinColSelectElement extends AbstractSelectElement {
-
- private Select options;
- private Select selectedOptions;
- private WebElement deselButton;
- private WebElement selButton;
- private static org.openqa.selenium.By bySelect = By.tagName("select");
- private static org.openqa.selenium.By byButton = By.className("v-button");
-
- @Override
- protected void init() {
- super.init();
- List<WebElement> selectElements = findElements(bySelect);
- options = new Select(selectElements.get(0));
- selectedOptions = new Select(selectElements.get(1));
- List<WebElement> buttons = findElements(byButton);
- selButton = buttons.get(0);
- deselButton = buttons.get(1);
- }
-
- private void deselectAll() {
- if (selectedOptions.isMultiple()) {
- if (selectedOptions.getAllSelectedOptions()
- .size() != selectedOptions.getOptions().size()) {
- for (int i = 0, l = selectedOptions.getOptions()
- .size(); i < l; ++i) {
- selectedOptions.selectByIndex(i);
- }
- }
- deselButton.click();
- }
- while (selectedOptions.getOptions().size() > 0) {
- selectedOptions.selectByIndex(0);
- deselButton.click();
- }
- }
-
- public void deselectByText(String text) {
- selectedOptions.deselectAll();
- selectedOptions.selectByVisibleText(text);
- deselButton.click();
- }
-
- /**
- * Functionality to find option texts of all currently selected options.
- *
- * @return List of visible text for all selected options
- */
- public List<String> getValues() {
- return getOptionsFromSelect(selectedOptions);
- }
-
- /**
- * Functionality to find all option texts.
- *
- * @return List of visible text for all options
- */
- public List<String> getOptions() {
- List<String> optionTexts = getOptionsFromSelect(options);
- optionTexts.addAll(getValues());
- return optionTexts;
- }
-
- /**
- * Gets the available option texts, i.e. all values which have not been
- * selected.
- *
- * @return List of visible text for available options
- */
- public List<String> getAvailableOptions() {
- return getOptionsFromSelect(options);
- }
-
- public void selectByText(String text) {
- options.deselectAll();
- options.selectByVisibleText(text);
- selButton.click();
- }
-
- private List<String> getOptionsFromSelect(Select select) {
- List<String> optionTexts = new ArrayList<String>();
- for (WebElement option : select.getOptions()) {
- optionTexts.add(option.getText());
- }
- return optionTexts;
- }
-
- /**
- * Return first selected item (item in the right part of component)
- */
- public String getValue() {
- String value = "";
- WebElement selectedElement = findElement(
- By.className("v-select-twincol-selections"));
- List<WebElement> optionElements = selectedElement
- .findElements(By.tagName("option"));
- if (!optionElements.isEmpty()) {
- value = optionElements.get(0).getText();
- }
- return value;
- }
-
- @Override
- public void clear() {
- deselectAll();
- }
- }
|