Skip to content

Commit 9a463f3

Browse files
Merge pull request #5 from testla-project/feature/selectOption
added new action to select dropdown option
2 parents 64c0d35 + 93440b4 commit 9a463f3

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

src/main/java/testla/web/abilities/BrowseTheWeb.java

+50
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.microsoft.playwright.options.Cookie;
1313
import com.microsoft.playwright.options.KeyboardModifier;
1414
import com.microsoft.playwright.options.LoadState;
15+
import com.microsoft.playwright.options.SelectOption;
1516
import testla.screenplay.ability.Ability;
1617
import testla.screenplay.actor.IActor;
1718
import testla.web.Modes;
@@ -404,6 +405,55 @@ public void dblclick(String selector, SelectorOptions options) {
404405
utils.recursiveLocatorLookup(this.page, selector, options).dblclick();
405406
}
406407

408+
409+
/**
410+
* Select a Dropdown menu option.
411+
*
412+
* @param selector the selector of the element to hover over.
413+
* @param dropDownOption the dropDown option that should be selected.
414+
* @param optionMode the mode of the dropDownOption. Supported: "value", "index" and "label".
415+
*/
416+
public List<String> selectOption(String selector, String dropDownOption, String optionMode) {
417+
switch (optionMode) {
418+
case "value" -> {
419+
return this.page.selectOption(selector, new SelectOption().setValue(dropDownOption));
420+
}
421+
case "label" -> {
422+
return this.page.selectOption(selector, new SelectOption().setLabel(dropDownOption));
423+
}
424+
case "index" -> {
425+
return this.page.selectOption(selector, new SelectOption().setIndex(Integer.parseInt(dropDownOption)));
426+
}
427+
default -> throw new RuntimeException("Error: illegal optionMode! Please use either 'label', 'value' or 'index'.");
428+
}
429+
}
430+
431+
/**
432+
* Select a Dropdown menu option.
433+
*
434+
* @param selector the selector of the element to hover over.
435+
* @param dropDownOption the dropDown option that should be selected.
436+
* @param selectorOptions advanced selector lookup options.
437+
* @param optionMode the mode of the dropDownOption. Supported: "value", "index" and "label".
438+
*/
439+
public List<String> selectOption(String selector, String dropDownOption, SelectorOptions selectorOptions, String optionMode) {
440+
switch (optionMode) {
441+
case "value" -> {
442+
return utils.recursiveLocatorLookup(this.page, selector, selectorOptions)
443+
.selectOption(new SelectOption().setValue(dropDownOption));
444+
}
445+
case "label" -> {
446+
return utils.recursiveLocatorLookup(this.page, selector, selectorOptions)
447+
.selectOption(new SelectOption().setLabel(dropDownOption));
448+
}
449+
case "index" -> {
450+
return utils.recursiveLocatorLookup(this.page, selector, selectorOptions)
451+
.selectOption(new SelectOption().setIndex(Integer.parseInt(dropDownOption)));
452+
}
453+
default -> throw new RuntimeException("Error in Select.option: illegal optionMode! Please use either 'label', 'value' or 'index'.");
454+
}
455+
}
456+
407457
/**
408458
* Validate if a locator on the page is visible or hidden.
409459
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package testla.web.actions;
2+
3+
import testla.screenplay.action.Action;
4+
import testla.screenplay.actor.IActor;
5+
import testla.web.SelectorOptions;
6+
import testla.web.abilities.BrowseTheWeb;
7+
8+
9+
/**
10+
* Action Class. Set the value of a Selector of type select to the given option.
11+
*/
12+
public class Select extends Action {
13+
14+
private final String selector;
15+
private final String dropDownOption;
16+
private final SelectorOptions selectorOptions;
17+
private final String optionMode; // value, index, label
18+
19+
// only selector and option -> will select dropdown label
20+
private Select(String selector, String dropDownOption) {
21+
this.selector = selector;
22+
this.dropDownOption = dropDownOption;
23+
this.selectorOptions = null;
24+
this.optionMode = "label";
25+
}
26+
27+
// selector, option and SelectorOptions -> will select dropdown label
28+
private Select(String selector, String dropDownOption, SelectorOptions selectorOptions) {
29+
this.selector = selector;
30+
this.dropDownOption = dropDownOption;
31+
this.selectorOptions = selectorOptions;
32+
this.optionMode = "label";
33+
}
34+
35+
// selector and optionMode -> will select according to optionMode
36+
private Select(String selector, String dropDownOption, String optionMode) {
37+
this.selector = selector;
38+
this.dropDownOption = dropDownOption;
39+
this.selectorOptions = null;
40+
this.optionMode = optionMode;
41+
}
42+
43+
// selector, SelectorOptions and playwright SelectOption() -> will select according to playwright SelectOption()
44+
private Select(String selector, String dropDownOption, SelectorOptions selectorOptions, String optionMode) {
45+
this.selector = selector;
46+
this.dropDownOption = dropDownOption;
47+
this.selectorOptions = selectorOptions;
48+
this.optionMode = optionMode;
49+
}
50+
51+
52+
/**
53+
* Set the value of a Selector of type select to the given option.
54+
*
55+
* @param selector the string representing the (select) selector.
56+
* @param dropDownOption the label of the option.
57+
* @return new Select instance.
58+
*/
59+
public static Select option(String selector, String dropDownOption) {
60+
return new Select(selector, dropDownOption);
61+
}
62+
63+
/**
64+
* Set the value of a Selector of type select to the given option.
65+
*
66+
* @param selector the string representing the (select) selector.
67+
* @param dropDownOption the label of the option.
68+
* @param selectorOptions advanced selector lookup options.
69+
* @return new Select instance.
70+
*/
71+
public static Select option(String selector, String dropDownOption, SelectorOptions selectorOptions) {
72+
return new Select(selector, dropDownOption, selectorOptions);
73+
}
74+
75+
/**
76+
* Set the value of a Selector of type select to the given option.
77+
*
78+
* @param selector the string representing the (select) selector.
79+
* @param dropDownOption optionLabel the label/value/index of the option.
80+
* @param optionMode determines how the dropDownOption is looked after. Supported: "value", "label", "index".
81+
* @return new Select instance.
82+
*/
83+
public static Select option(String selector, String dropDownOption, String optionMode) {
84+
return new Select(selector, dropDownOption, optionMode);
85+
}
86+
87+
/**
88+
* Set the value of a Selector of type select to the given option.
89+
*
90+
* @param selector the string representing the (select) selector.
91+
* @param dropDownOption optionLabel the label/value/index of the option.
92+
* @param selectorOptions advanced selector lookup options.
93+
* @param optionMode determines how the dropDownOption is looked after. Supported: "value", "label", "index".
94+
* @return new Select instance.
95+
*/
96+
public static Select option(String selector, String dropDownOption, SelectorOptions selectorOptions, String optionMode) {
97+
return new Select(selector, dropDownOption, selectorOptions, optionMode);
98+
}
99+
100+
/**
101+
* Set the value of a Selector of type select to the given option.
102+
*
103+
* @param actor the actor.
104+
*/
105+
@Override
106+
public Object performAs(IActor actor) {
107+
if (this.selectorOptions == null) {
108+
return BrowseTheWeb.as(actor).selectOption(this.selector, this.dropDownOption, this.optionMode);
109+
} else {
110+
return BrowseTheWeb.as(actor).selectOption(this.selector, this.dropDownOption, this.selectorOptions, this.optionMode);
111+
}
112+
}
113+
114+
}
115+
116+

src/test/java/WebTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import testla.web.actions.Navigate;
2626
import testla.web.actions.Press;
2727
import testla.web.actions.Remove;
28+
import testla.web.actions.Select;
2829
import testla.web.actions.Set;
2930
import testla.web.actions.Type;
3031
import testla.web.actions.Wait;
@@ -190,6 +191,16 @@ void waitAndRecursiveLocatorTest() {
190191
);
191192
}
192193

194+
@Test
195+
void selectDropDownTest() {
196+
actor.attemptsTo(
197+
Navigate.to("https://the-internet.herokuapp.com/dropdown"),
198+
Wait.forLoadState(LoadState.NETWORKIDLE),
199+
200+
Select.option("[id='%s']", "2", new SelectorOptions().setReplacements("dropdown"), "value")
201+
);
202+
}
203+
193204
@Test
194205
void cookieTest() {
195206
BrowserContext context = actorPage.context();

0 commit comments

Comments
 (0)