|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2022 Wasiq Bhamla |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.github.wasiqb.boyka.actions; |
| 18 | + |
| 19 | +import static com.github.wasiqb.boyka.actions.CommonActions.getElementAttribute; |
| 20 | +import static com.github.wasiqb.boyka.actions.CommonActions.performElementAction; |
| 21 | +import static java.util.stream.Collectors.toList; |
| 22 | +import static org.apache.commons.lang3.StringUtils.EMPTY; |
| 23 | +import static org.apache.logging.log4j.LogManager.getLogger; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import com.github.wasiqb.boyka.builders.Locator; |
| 28 | +import com.github.wasiqb.boyka.enums.Message; |
| 29 | +import com.github.wasiqb.boyka.exception.FrameworkError; |
| 30 | +import org.apache.logging.log4j.Logger; |
| 31 | +import org.openqa.selenium.NoSuchElementException; |
| 32 | +import org.openqa.selenium.WebElement; |
| 33 | +import org.openqa.selenium.support.ui.Select; |
| 34 | + |
| 35 | +/** |
| 36 | + * Performs dropdown specific actions. |
| 37 | + * |
| 38 | + * @author Wasiq Bhamla |
| 39 | + * @since 30-Jul-2022 |
| 40 | + */ |
| 41 | +public final class DropDownActions { |
| 42 | + private static final Logger LOGGER = getLogger (); |
| 43 | + |
| 44 | + /** |
| 45 | + * Deselects all the selected values. |
| 46 | + * |
| 47 | + * @param locator {@link Locator} of drop down |
| 48 | + */ |
| 49 | + public static void deselectAll (final Locator locator) { |
| 50 | + LOGGER.traceEntry (); |
| 51 | + LOGGER.info ("Deselecting element located by: {}", locator); |
| 52 | + performElementAction (e -> { |
| 53 | + final var select = new Select (e); |
| 54 | + if (!select.isMultiple ()) { |
| 55 | + throw new FrameworkError (Message.ERROR_DESELECT_FROM_DROPDOWN.getMessageText ()); |
| 56 | + } |
| 57 | + select.deselectAll (); |
| 58 | + }, locator); |
| 59 | + LOGGER.traceExit (); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Deselects the option from the dropdown based on its index. |
| 64 | + * |
| 65 | + * @param locator {@link Locator} of dropdown |
| 66 | + * @param index index of the option to deselect |
| 67 | + */ |
| 68 | + public static void deselectByIndex (final Locator locator, final int index) { |
| 69 | + LOGGER.traceEntry (); |
| 70 | + LOGGER.info ("Deselecting element located by: {} by index: {}", locator, index); |
| 71 | + performElementAction (e -> { |
| 72 | + final var select = new Select (e); |
| 73 | + if (!select.isMultiple ()) { |
| 74 | + throw new FrameworkError (Message.ERROR_DESELECT_FROM_DROPDOWN.getMessageText ()); |
| 75 | + } |
| 76 | + select.deselectByIndex (index); |
| 77 | + }, locator); |
| 78 | + LOGGER.traceExit (); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Deselects the option from the dropdown based on its visible text. |
| 83 | + * |
| 84 | + * @param locator {@link Locator} of dropdown |
| 85 | + * @param text visible text of the option to deselect |
| 86 | + */ |
| 87 | + public static void deselectByText (final Locator locator, final String text) { |
| 88 | + LOGGER.traceEntry (); |
| 89 | + LOGGER.info ("Deselecting element located by: {} by visible text: {}", locator, text); |
| 90 | + performElementAction (e -> { |
| 91 | + final var select = new Select (e); |
| 92 | + if (!select.isMultiple ()) { |
| 93 | + throw new FrameworkError (Message.ERROR_DESELECT_FROM_DROPDOWN.getMessageText ()); |
| 94 | + } |
| 95 | + select.deselectByVisibleText (text); |
| 96 | + }, locator); |
| 97 | + LOGGER.traceExit (); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Deselects the option from the dropdown based on its value. |
| 102 | + * |
| 103 | + * @param locator {@link Locator} of dropdown |
| 104 | + * @param value value of the option to deselect |
| 105 | + */ |
| 106 | + public static void deselectByValue (final Locator locator, final String value) { |
| 107 | + LOGGER.traceEntry (); |
| 108 | + LOGGER.info ("Deselecting element located by: {} by value: {}", locator, value); |
| 109 | + performElementAction (e -> { |
| 110 | + final var select = new Select (e); |
| 111 | + if (!select.isMultiple ()) { |
| 112 | + throw new FrameworkError (Message.ERROR_DESELECT_FROM_DROPDOWN.getMessageText ()); |
| 113 | + } |
| 114 | + select.deselectByValue (value); |
| 115 | + }, locator); |
| 116 | + LOGGER.traceExit (); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Selects the value from dropdown based on index. |
| 121 | + * |
| 122 | + * @param locator locator of the dropdown |
| 123 | + * @param index index to be selected |
| 124 | + */ |
| 125 | + public static void selectByIndex (final Locator locator, final int index) { |
| 126 | + LOGGER.traceEntry (); |
| 127 | + LOGGER.info ("Selecting element located by: {} by index: {}", locator, index); |
| 128 | + performElementAction (e -> { |
| 129 | + final var select = new Select (e); |
| 130 | + select.selectByIndex (index); |
| 131 | + }, locator); |
| 132 | + LOGGER.traceExit (); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Selects the value from dropdown based on visible text. |
| 137 | + * |
| 138 | + * @param locator locator of the dropdown |
| 139 | + * @param text text to be selected |
| 140 | + */ |
| 141 | + public static void selectByText (final Locator locator, final String text) { |
| 142 | + LOGGER.traceEntry (); |
| 143 | + LOGGER.info ("Selecting element located by: {} by text: {}", locator, text); |
| 144 | + performElementAction (e -> { |
| 145 | + final var select = new Select (e); |
| 146 | + select.selectByVisibleText (text); |
| 147 | + }, locator); |
| 148 | + LOGGER.traceExit (); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Selects the value from dropdown based on value. |
| 153 | + * |
| 154 | + * @param locator locator of the dropdown |
| 155 | + * @param value value to be selected |
| 156 | + */ |
| 157 | + public static void selectByValue (final Locator locator, final String value) { |
| 158 | + LOGGER.traceEntry (); |
| 159 | + LOGGER.info ("Selecting element located by: {} by value: {}", locator, value); |
| 160 | + performElementAction (e -> { |
| 161 | + final var select = new Select (e); |
| 162 | + select.selectByValue (value); |
| 163 | + }, locator); |
| 164 | + LOGGER.traceExit (); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Returns the first selected value from dropdown. |
| 169 | + * |
| 170 | + * @param locator locator of the dropdown |
| 171 | + * |
| 172 | + * @return first selected value |
| 173 | + */ |
| 174 | + public static String selectedItem (final Locator locator) { |
| 175 | + LOGGER.traceEntry (); |
| 176 | + LOGGER.info ("Getting selected option from element located by: {}", locator); |
| 177 | + return getElementAttribute (element -> { |
| 178 | + final var select = new Select (element); |
| 179 | + try { |
| 180 | + return select.getFirstSelectedOption () |
| 181 | + .getText (); |
| 182 | + } catch (final NoSuchElementException e) { |
| 183 | + return EMPTY; |
| 184 | + } |
| 185 | + }, locator); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Gets all the selected values from the dropdown. |
| 190 | + * |
| 191 | + * @param locator locator of the dropdown |
| 192 | + * |
| 193 | + * @return list of selected values |
| 194 | + */ |
| 195 | + public static List<String> selectedItems (final Locator locator) { |
| 196 | + LOGGER.traceEntry (); |
| 197 | + LOGGER.info ("Getting all selected options from element located by: {}", locator); |
| 198 | + return getElementAttribute (e -> { |
| 199 | + final var select = new Select (e); |
| 200 | + return select.getAllSelectedOptions () |
| 201 | + .stream () |
| 202 | + .map (WebElement::getText) |
| 203 | + .collect (toList ()); |
| 204 | + }, locator); |
| 205 | + } |
| 206 | + |
| 207 | + private DropDownActions () { |
| 208 | + // Utility class. |
| 209 | + } |
| 210 | +} |
0 commit comments