Skip to content

Commit 1750a34

Browse files
authored
feat(java): ✨ added support to dropdown and multi select elements (#121)
* feat(java): ✨ added support to dropdown and multi select elements * docs(website): 📝 updated documentations and dependencies
1 parent 603a092 commit 1750a34

File tree

25 files changed

+760
-91
lines changed

25 files changed

+760
-91
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v16.15.0
1+
v16.16.0
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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+
}

core-java/src/main/java/com/github/wasiqb/boyka/actions/ElementActions.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ public static boolean isSelected (final Locator locator) {
9999
return LOGGER.traceExit (getElementAttribute (WebElement::isSelected, locator));
100100
}
101101

102+
/**
103+
* Gets the styling attribute of the element.
104+
*
105+
* @param locator locator of the element
106+
* @param attribute attribute of the element
107+
*
108+
* @return value of the styling attribute of the element
109+
*/
110+
public static String styleOf (final Locator locator, final String attribute) {
111+
LOGGER.traceEntry ();
112+
LOGGER.info ("Getting attribute: {} of element located by: {}", attribute, locator);
113+
LOGGER.traceExit ();
114+
return getElementAttribute (e -> e.getCssValue (attribute), locator);
115+
}
116+
102117
/**
103118
* Submit the element.
104119
*
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.DropDownActions.selectedItem;
20+
import static com.github.wasiqb.boyka.actions.DropDownActions.selectedItems;
21+
import static com.google.common.truth.Truth.assertThat;
22+
23+
import com.github.wasiqb.boyka.builders.Locator;
24+
import com.google.common.truth.IterableSubject;
25+
import com.google.common.truth.StringSubject;
26+
27+
/**
28+
* Verify dropdown actions.
29+
*
30+
* @author Wasiq Bhamla
31+
* @since 30-Jul-2022
32+
*/
33+
public final class VerifyDropDownActions {
34+
/**
35+
* Verify selected item.
36+
*
37+
* @param locator {@link Locator} of dropdown
38+
*
39+
* @return {@link StringSubject} of selected item
40+
*/
41+
public static StringSubject verifySelectedItem (final Locator locator) {
42+
return assertThat (selectedItem (locator));
43+
}
44+
45+
/**
46+
* Verify selected items.
47+
*
48+
* @param locator {@link Locator} of dropdown
49+
*
50+
* @return {@link IterableSubject} of selected items
51+
*/
52+
public static IterableSubject verifySelectedItems (final Locator locator) {
53+
return assertThat (selectedItems (locator));
54+
}
55+
56+
private VerifyDropDownActions () {
57+
// Utility class.
58+
}
59+
}

core-java/src/main/java/com/github/wasiqb/boyka/actions/VerifyElementActions.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.github.wasiqb.boyka.actions.ElementActions.isDisplayed;
2121
import static com.github.wasiqb.boyka.actions.ElementActions.isEnabled;
2222
import static com.github.wasiqb.boyka.actions.ElementActions.isSelected;
23+
import static com.github.wasiqb.boyka.actions.ElementActions.styleOf;
2324
import static com.github.wasiqb.boyka.actions.ElementActions.textOf;
2425
import static com.google.common.truth.Truth.assertThat;
2526
import static org.apache.logging.log4j.LogManager.getLogger;
@@ -95,6 +96,21 @@ public static BooleanSubject verifyElementSelected (final Locator locator) {
9596
return assertThat (isSelected (locator));
9697
}
9798

99+
/**
100+
* Verify style of element.
101+
*
102+
* @param locator locator of element
103+
* @param attribute attribute to verify
104+
*
105+
* @return {@link StringSubject} to verify the result
106+
*/
107+
public static StringSubject verifyStyleOf (final Locator locator, final String attribute) {
108+
LOGGER.traceEntry ();
109+
LOGGER.info ("Verifying style of {}", locator);
110+
LOGGER.traceExit ();
111+
return assertThat (styleOf (locator, attribute));
112+
}
113+
98114
/**
99115
* Verify text of element.
100116
*

core-java/src/main/java/com/github/wasiqb/boyka/enums/Message.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public enum Message {
4646
* Element not found.
4747
*/
4848
ELEMENT_NOT_FOUND ("Element not found for locator {0}..."),
49+
/**
50+
* Deselect from dropdown Error.
51+
*/
52+
ERROR_DESELECT_FROM_DROPDOWN ("Cannot deselect from dropdown, it should be Multi-select box..."),
4953
/**
5054
* Error executing request
5155
*/

core-java/src/test/java/com/github/wasiqb/boyka/testng/web/theinternet/DoubleClickTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
package com.github.wasiqb.boyka.testng.web.theinternet;
1818

1919
import static com.github.wasiqb.boyka.actions.DriverActions.navigateTo;
20-
import static com.github.wasiqb.boyka.actions.DriverActions.waitUntil;
2120
import static com.github.wasiqb.boyka.actions.MouseActions.doubleClickOn;
22-
import static com.github.wasiqb.boyka.actions.VerifyDriverActions.verifyAcceptAlert;
21+
import static com.github.wasiqb.boyka.actions.VerifyElementActions.verifyStyleOf;
2322
import static com.github.wasiqb.boyka.manager.DriverManager.closeDriver;
2423
import static com.github.wasiqb.boyka.manager.DriverManager.createDriver;
2524
import static com.github.wasiqb.boyka.testng.web.theinternet.pages.DoubleClickPage.doubleClickPage;
26-
import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
2725

2826
import com.github.wasiqb.boyka.enums.ApplicationType;
2927
import org.testng.annotations.AfterClass;
@@ -38,7 +36,7 @@
3836
* @since 26-Jul-2022
3937
*/
4038
public class DoubleClickTest {
41-
private static final String URL = "https://demo.guru99.com/test/simple_context_menu.html";
39+
private static final String URL = "https://webdriveruniversity.com/Actions/index.html";
4240

4341
/**
4442
* Setup test class by initialising driver.
@@ -66,8 +64,7 @@ public void tearDownClass () {
6664
*/
6765
@Test (description = "Double click test")
6866
public void testDoubleClick () {
69-
doubleClickOn (doubleClickPage ().getButton ());
70-
waitUntil (alertIsPresent ());
71-
verifyAcceptAlert ().isEqualTo ("You double clicked me.. Thank You..");
67+
doubleClickOn (doubleClickPage ().getDoubleClick ());
68+
verifyStyleOf (doubleClickPage ().getDoubleClick (), "background-color").isEqualTo ("rgba(147, 203, 90, 1)");
7269
}
7370
}

core-java/src/test/java/com/github/wasiqb/boyka/testng/web/theinternet/DragDropTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @since 26-Jul-2022
3737
*/
3838
public class DragDropTest {
39-
private static final String URL = "https://demoqa.com/droppable/";
39+
private static final String URL = "https://webdriveruniversity.com/Actions/index.html";
4040

4141
/**
4242
* Setup test class by initialising driver.

0 commit comments

Comments
 (0)