|
| 1 | +package aquality.selenium.elements; |
| 2 | + |
| 3 | +import aquality.selenium.core.elements.ElementState; |
| 4 | +import aquality.selenium.elements.interfaces.IMultiChoiceBox; |
| 5 | +import org.apache.commons.lang3.StringUtils; |
| 6 | +import org.openqa.selenium.By; |
| 7 | +import org.openqa.selenium.WebElement; |
| 8 | +import org.openqa.selenium.support.ui.Select; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.function.Function; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class describing the Multi-choice ComboBox (dropdown list), i.e. the one having attribute {@code multiple} set to {@code true} |
| 16 | + */ |
| 17 | +public class MultiChoiceBox extends ComboBox implements IMultiChoiceBox { |
| 18 | + |
| 19 | + private static final String LOG_DESELECTING_VALUE = "loc.deselecting.value"; |
| 20 | + |
| 21 | + protected MultiChoiceBox(By locator, String name, ElementState state) { |
| 22 | + super(locator, name, state); |
| 23 | + } |
| 24 | + |
| 25 | + protected String getElementType() { |
| 26 | + return getLocalizationManager().getLocalizedMessage("loc.multichoicebox"); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public List<String> getSelectedValues() { |
| 31 | + logElementAction("loc.combobox.getting.selected.value"); |
| 32 | + return collectSelectedOptions(option -> option.getAttribute(Attributes.VALUE.toString()), "value"); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public List<String> getSelectedTexts() { |
| 37 | + logElementAction("loc.combobox.getting.selected.text"); |
| 38 | + return collectSelectedOptions(WebElement::getText, "text"); |
| 39 | + } |
| 40 | + |
| 41 | + private List<String> collectSelectedOptions(Function<WebElement, String> valueGetter, String valueType) { |
| 42 | + List<String> texts = doWithRetry(() -> |
| 43 | + new Select(getElement()).getAllSelectedOptions() |
| 44 | + .stream() |
| 45 | + .map(valueGetter) |
| 46 | + .collect(Collectors.toList())); |
| 47 | + String logValue = texts.stream().map(value -> String.format("'%s'", value)).collect(Collectors.joining(", ")); |
| 48 | + logElementAction(String.format("loc.combobox.selected.%s", valueType), logValue); |
| 49 | + return texts; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void selectAll() { |
| 54 | + logElementAction("loc.multichoicebox.select.all"); |
| 55 | + applyFunctionToOptionsThatContain(element -> element.getAttribute(Attributes.VALUE.toString()), |
| 56 | + Select::selectByValue, |
| 57 | + StringUtils.EMPTY); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void deselectAll() { |
| 62 | + logElementAction("loc.multichoicebox.deselect.all"); |
| 63 | + doWithRetry(() -> new Select(getElement()).deselectAll()); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void deselectByIndex(int index) { |
| 68 | + logElementAction(LOG_DESELECTING_VALUE, String.format("#%s", index)); |
| 69 | + doWithRetry(() -> new Select(getElement()).deselectByIndex(index)); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void deselectByValue(String value) { |
| 74 | + logElementAction(LOG_DESELECTING_VALUE, value); |
| 75 | + doWithRetry(() -> new Select(getElement()).deselectByValue(value)); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void deselectByContainingValue(String value) { |
| 80 | + logElementAction(LOG_DESELECTING_VALUE, value); |
| 81 | + applyFunctionToOptionsThatContain(element -> element.getAttribute(Attributes.VALUE.toString()), |
| 82 | + Select::deselectByValue, |
| 83 | + value); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void deselectByText(String text) { |
| 88 | + logElementAction("loc.multichoicebox.deselect.by.text", text); |
| 89 | + doWithRetry(() -> new Select(getElement()).deselectByVisibleText(text)); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void deselectByContainingText(String text) { |
| 94 | + logElementAction("loc.multichoicebox.deselect.by.text", text); |
| 95 | + applyFunctionToOptionsThatContain(WebElement::getText, |
| 96 | + Select::deselectByVisibleText, |
| 97 | + text); |
| 98 | + } |
| 99 | +} |
0 commit comments