|
| 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 | + |
0 commit comments