Skip to content

Commit 9751590

Browse files
Merge pull request #105 from aquality-automation/Feature/Add-Multichoice-Combobox
Feature/add multichoice combobox closes #65
2 parents aff8e90 + 3a4bf4f commit 9751590

File tree

17 files changed

+461
-13
lines changed

17 files changed

+461
-13
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-
selectOptionThatContains(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-
selectOptionThatContains(element -> element.getAttribute(Attributes.VALUE.toString()),
60+
applyFunctionToOptionsThatContain(element -> element.getAttribute(Attributes.VALUE.toString()),
6161
Select::selectByValue,
6262
value);
6363
}
6464

65-
private void selectOptionThatContains(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 @@ private void selectOptionThatContains(Function<WebElement, String> getValueFunc,
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected Map<Class<? extends aquality.selenium.core.elements.interfaces.IElemen
4545
typesMap.put(IButton.class, Button.class);
4646
typesMap.put(ICheckBox.class, CheckBox.class);
4747
typesMap.put(IComboBox.class, ComboBox.class);
48+
typesMap.put(IMultiChoiceBox.class, MultiChoiceBox.class);
4849
typesMap.put(ILabel.class, Label.class);
4950
typesMap.put(ILink.class, Link.class);
5051
typesMap.put(IRadioButton.class, RadioButton.class);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public enum ElementType {
66
BUTTON(IButton.class),
77
CHECKBOX(ICheckBox.class),
88
COMBOBOX(IComboBox.class),
9+
MULTICHOICEBOX(IMultiChoiceBox.class),
910
LABEL(ILabel.class),
1011
LINK(ILink.class),
1112
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/interfaces/IElementFactory.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,29 @@ default IComboBox getComboBox(By locator, String name, ElementState state) {
8080
return get(ElementType.COMBOBOX, locator, name, state);
8181
}
8282

83+
/**
84+
* Creates element that implements IMultiChoiceBox interface.
85+
*
86+
* @param locator Element locator
87+
* @param name Element name
88+
* @return Instance of element that implements IMultiChoiceBox interface
89+
*/
90+
default IMultiChoiceBox getMultiChoiceBox(By locator, String name) {
91+
return getMultiChoiceBox(locator, name, ElementState.DISPLAYED);
92+
}
93+
94+
/**
95+
* Creates element that implements IMultiChoiceBox interface.
96+
*
97+
* @param locator Element locator
98+
* @param name Element name
99+
* @param state Element state
100+
* @return Instance of element that implements IMultiChoiceBox interface
101+
*/
102+
default IMultiChoiceBox getMultiChoiceBox(By locator, String name, ElementState state) {
103+
return get(ElementType.MULTICHOICEBOX, locator, name, state);
104+
}
105+
83106
/**
84107
* Creates element that implements ILabel interface.
85108
*
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package aquality.selenium.elements.interfaces;
2+
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();
25+
26+
/**
27+
* Deselect all selected options
28+
*/
29+
void deselectAll();
30+
31+
/**
32+
* Deselect selected option by index
33+
*
34+
* @param index number of selected option
35+
*/
36+
void deselectByIndex(int index);
37+
38+
/**
39+
* Deselect selected option by value
40+
*
41+
* @param value argument value
42+
*/
43+
void deselectByValue(String value);
44+
45+
/**
46+
* Deselect selected option by containing value
47+
*
48+
* @param value partial option's value
49+
*/
50+
void deselectByContainingValue(String value);
51+
52+
/**
53+
* Deselect selected option by visible text
54+
*
55+
* @param text text to be deselected
56+
*/
57+
void deselectByText(String text);
58+
59+
/**
60+
* Deselect selected option by containing visible text
61+
*
62+
* @param text visible text
63+
*/
64+
void deselectByContainingText(String text);
65+
}

src/main/resources/localization/be.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": "Атрымліваем выбраны тэкст праз 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'",
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'",
4751
"loc.el.getattr": "Атрымліваем атрыбут '%1$s'",
4852
"loc.el.attr.value": "Значэнне атрыбута '%1$s': [%2$s]",
4953
"loc.el.attr.set": "Задаем значэнне атрыбута '%1$s': [%2$s]",
@@ -68,6 +72,7 @@
6872
"loc.scrolling.center.js": "Пракручваем старонку да цэнтра элемента праз JavaScript",
6973
"loc.scrolling.js": "Пракручваем старонку праз JavaScript",
7074
"loc.selecting.value": "Выбіраем значэнне - '%s'",
75+
"loc.deselecting.value": "Адмяняем выбар значэння - '%s'",
7176
"loc.send.text": "Задаем тэкст - '%s'",
7277
"loc.setting.value": "Задаем значэнне - '%s'",
7378
"loc.text.clearing": "Ачышчаем",

src/main/resources/localization/en.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": "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'",
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'",
4751
"loc.el.getattr": "Getting attribute '%1$s'",
4852
"loc.el.attr.value": "Value of attribute '%1$s': [%2$s]",
4953
"loc.el.attr.set": "Setting value of attribute '%1$s': [%2$s]",
@@ -68,6 +72,7 @@
6872
"loc.scrolling.center.js": "Scrolling to the center via JavaScript",
6973
"loc.scrolling.js": "Scrolling via JavaScript",
7074
"loc.selecting.value": "Selecting value - '%s'",
75+
"loc.deselecting.value": "Deselecting value - '%s'",
7176
"loc.send.text": "Setting text - '%s'",
7277
"loc.setting.value": "Setting value - '%s'",
7378
"loc.text.clearing": "Clearing",

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",

src/main/resources/localization/ru.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": "Получение текста выбранного значения посредством 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'",
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'",
4751
"loc.el.getattr": "Получение аттрибута '%1$s'",
4852
"loc.el.attr.value": "Значение аттрибута '%1$s'': [%2$s]",
4953
"loc.el.attr.set": "Установка значения атрибута '%1$s': [%2$s]",
@@ -68,6 +72,7 @@
6872
"loc.scrolling.center.js": "Скроллинг в центр (посредством JavaScript)",
6973
"loc.scrolling.js": "Скроллинг посредством JavaScript",
7074
"loc.selecting.value": "Выбор значения - '%s'",
75+
"loc.deselecting.value": "Отмена выбора значения - '%s'",
7176
"loc.send.text": "Ввод текста - '%s'",
7277
"loc.setting.value": "Установка значения - '%s'",
7378
"loc.text.clearing": "Очистка",

0 commit comments

Comments
 (0)