Skip to content

Commit e1e6e68

Browse files
committed
Add selectAll, getSelectedTexts, getSelectedValues methods
Rename to MultiChoiceBox Add localization values Add test, add to TestSuite.xml Resolve GitHub comments
1 parent 7d224dd commit e1e6e68

File tree

14 files changed

+199
-99
lines changed

14 files changed

+199
-99
lines changed

src/main/java/aquality/selenium/elements/ComboBox.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,20 @@ public void clickAndSelectByText(String value) {
4949
@Override
5050
public void selectByContainingText(String text) {
5151
logElementAction("loc.combobox.select.by.text", text);
52-
applySelectFuncToOptionThatContains(WebElement::getText,
52+
applyFunctionToOptionsThatContain(WebElement::getText,
5353
Select::selectByVisibleText,
5454
text);
5555
}
5656

5757
@Override
5858
public void selectByContainingValue(String value) {
5959
logElementAction(LOG_SELECTING_VALUE, value);
60-
applySelectFuncToOptionThatContains(element -> element.getAttribute(Attributes.VALUE.toString()),
60+
applyFunctionToOptionsThatContain(element -> element.getAttribute(Attributes.VALUE.toString()),
6161
Select::selectByValue,
6262
value);
6363
}
6464

65-
protected void applySelectFuncToOptionThatContains(Function<WebElement, String> getValueFunc, BiConsumer<Select, String> selectFunc, String value){
65+
protected void applyFunctionToOptionsThatContain(Function<WebElement, String> getValueFunc, BiConsumer<Select, String> selectFunc, String value){
6666
doWithRetry(() -> {
6767
Select select = new Select(getElement());
6868
List<WebElement> elements = select.getOptions();
@@ -77,7 +77,7 @@ protected void applySelectFuncToOptionThatContains(Function<WebElement, String>
7777
}
7878
if (!isSelected){
7979
throw new InvalidElementStateException(String.format(getLocalizationManager().getLocalizedMessage(
80-
"loc.combobox.impossible.to.select.contain.value.or.text"), value, getName()));
80+
"loc.combobox.impossible.to.find.option.contain.value.or.text"), value, getName()));
8181
}
8282
});
8383
}

src/main/java/aquality/selenium/elements/ElementFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ protected Map<Class<? extends aquality.selenium.core.elements.interfaces.IElemen
4343
typesMap.put(IButton.class, Button.class);
4444
typesMap.put(ICheckBox.class, CheckBox.class);
4545
typesMap.put(IComboBox.class, ComboBox.class);
46-
typesMap.put(IMultiChoiceComboBox.class, MultiChoiceComboBox.class);
46+
typesMap.put(IMultiChoiceBox.class, MultiChoiceBox.class);
4747
typesMap.put(ILabel.class, Label.class);
4848
typesMap.put(ILink.class, Link.class);
4949
typesMap.put(IRadioButton.class, RadioButton.class);

src/main/java/aquality/selenium/elements/ElementType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public enum ElementType {
66
BUTTON(IButton.class),
77
CHECKBOX(ICheckBox.class),
88
COMBOBOX(IComboBox.class),
9-
MULTICHOICECOMBOBOX(IMultiChoiceComboBox.class),
9+
MULTICHOICECOMBOBOX(IMultiChoiceBox.class),
1010
LABEL(ILabel.class),
1111
LINK(ILink.class),
1212
RADIOBUTTON(IRadioButton.class),
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}

src/main/java/aquality/selenium/elements/MultiChoiceComboBox.java

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/main/java/aquality/selenium/elements/interfaces/IElementFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ default IComboBox getComboBox(By locator, String name, ElementState state) {
8787
* @param name Element name
8888
* @return Instance of element that implements IMultiChoiceComboBox interface
8989
*/
90-
default IMultiChoiceComboBox getMultiChoiceComboBox(By locator, String name) {
90+
default IMultiChoiceBox getMultiChoiceComboBox(By locator, String name) {
9191
return getMultiChoiceComboBox(locator, name, ElementState.DISPLAYED);
9292
}
9393

@@ -99,7 +99,7 @@ default IMultiChoiceComboBox getMultiChoiceComboBox(By locator, String name) {
9999
* @param state Element state
100100
* @return Instance of element that implements IMultiChoiceComboBox interface
101101
*/
102-
default IMultiChoiceComboBox getMultiChoiceComboBox(By locator, String name, ElementState state) {
102+
default IMultiChoiceBox getMultiChoiceComboBox(By locator, String name, ElementState state) {
103103
return get(ElementType.MULTICHOICECOMBOBOX, locator, name, state);
104104
}
105105

src/main/java/aquality/selenium/elements/interfaces/IMultiChoiceComboBox.java renamed to src/main/java/aquality/selenium/elements/interfaces/IMultiChoiceBox.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
package aquality.selenium.elements.interfaces;
22

3-
public interface IMultiChoiceComboBox extends IComboBox {
3+
import java.util.List;
4+
5+
public interface IMultiChoiceBox extends IComboBox {
6+
7+
/**
8+
* Gets value of all selected options
9+
*
10+
* @return selected values
11+
*/
12+
List<String> getSelectedValues();
13+
14+
/**
15+
* Gets text of all selected options
16+
*
17+
* @return selected text
18+
*/
19+
List<String> getSelectedTexts();
20+
21+
/**
22+
* Select all options
23+
*/
24+
void selectAll();
425

526
/**
627
* Deselect all selected options

src/main/resources/localization/be.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@
4343
"loc.combobox.get.text.js": "Атрымліваем выбраны тэкст праз JavaScript",
4444
"loc.combobox.texts": "Спіс тэкстаў опцыяў: [%s]",
4545
"loc.combobox.values": "Спіс значэнняў: [%s]",
46-
"loc.combobox.impossible.to.select.contain.value.or.text": "Немагчыма выбраць опцыю, якая змяшчае значэнне/тэкст '%1$s' у камбабоксе '%2$s'",
47-
"loc.multichoicecombobox": "Камбабокс з мульцівыбарам",
48-
"loc.multichoicecombobox.deselect.all": "Адмяняем выбар усіх значэнняў",
49-
"loc.multichoicecombobox.deselect.by.text": "Адмяняем выбар значэння з тэкстам '%s'",
46+
"loc.combobox.impossible.to.find.option.contain.value.or.text": "Немагчыма знайсці опцыю, якая змяшчае значэнне/тэкст '%1$s' у камбабоксе '%2$s'",
47+
"loc.multichoicebox": "Камбабокс з мульцівыбарам",
48+
"loc.multichoicebox.select.all": "Выбіраем усе значэнні",
49+
"loc.multichoicebox.deselect.all": "Адмяняем выбар усіх значэнняў",
50+
"loc.multichoicebox.deselect.by.text": "Адмяняем выбар значэння з тэкстам '%s'",
5051
"loc.el.getattr": "Атрымліваем атрыбут '%1$s'",
5152
"loc.el.attr.value": "Значэнне атрыбута '%1$s': [%2$s]",
5253
"loc.el.attr.set": "Задаем значэнне атрыбута '%1$s': [%2$s]",

src/main/resources/localization/en.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@
4343
"loc.combobox.get.text.js": "Getting selected text via JavaScript",
4444
"loc.combobox.texts": "Option texts: [%s]",
4545
"loc.combobox.values": "Option values: [%s]",
46-
"loc.combobox.impossible.to.select.contain.value.or.text": "It is impossible to select option that contains value/text '%1$s' from combobox '%2$s'",
47-
"loc.multichoicecombobox": "Multi-choice ComboBox",
48-
"loc.multichoicecombobox.deselect.all": "Deselect all",
49-
"loc.multichoicecombobox.deselect.by.text": "Deselecting value by text '%s'",
46+
"loc.combobox.impossible.to.find.option.contain.value.or.text": "It is impossible to find an option that contains value/text '%1$s' from combobox '%2$s'",
47+
"loc.multichoicebox": "Multi-choice ComboBox",
48+
"loc.multichoicebox.select.all": "Select all",
49+
"loc.multichoicebox.deselect.all": "Deselect all",
50+
"loc.multichoicebox.deselect.by.text": "Deselecting value by text '%s'",
5051
"loc.el.getattr": "Getting attribute '%1$s'",
5152
"loc.el.attr.value": "Value of attribute '%1$s': [%2$s]",
5253
"loc.el.attr.set": "Setting value of attribute '%1$s': [%2$s]",

src/main/resources/localization/pl.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
"loc.combobox.get.text.js": "Pobieranie wybranego tekstu przez JavaScript",
4444
"loc.combobox.texts": "Lista tekstów opcji: [%s]",
4545
"loc.combobox.values": "Lista wartości: [%s]",
46-
"loc.combobox.impossible.to.select.contain.value.or.text": "Wybieranie wartości ze znaczeniem/tekstem '%1$s' w polu kombi '%2$s' nie jest możliwe",
46+
"loc.combobox.impossible.to.find.option.contain.value.or.text": "Znalezienie wartości ze znaczeniem/tekstem '%1$s' w polu kombi '%2$s' nie jest możliwe",
47+
"loc.multichoicebox": "Pole kombi z multiwyborem",
48+
"loc.multichoicebox.select.all": "Wybieranie wszystkich wartości",
49+
"loc.multichoicebox.deselect.all": "Anulowanie wybierania wszystkich wartości",
50+
"loc.multichoicebox.deselect.by.text": "Anulowanie wybierania wartości z tekstem '%s'",
4751
"loc.el.getattr": "Pobieranie atrybutu '%1$s'",
4852
"loc.el.attr.value": "Wartość atrybutu '%1$s': [%2$s]",
4953
"loc.el.attr.set": "Ustawianie wartości atrybutu '%1$s': [%2$s]",
@@ -68,6 +72,7 @@
6872
"loc.scrolling.center.js": "Przewijanie do centrum przez JavaScript",
6973
"loc.scrolling.js": "Przewijanie przez JavaScript",
7074
"loc.selecting.value": "Wybieranie wartości - '%s'",
75+
"loc.deselecting.value": "Anulowanie wybierania wartości - '%s'",
7176
"loc.send.text": "Ustawianie tekstu - '%s'",
7277
"loc.setting.value": "Ustawienie wartości - '%s'",
7378
"loc.text.clearing": "Oczyszczenie",

0 commit comments

Comments
 (0)