1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515#
16- import time
1716
1817from selenium .common .exceptions import ElementClickInterceptedException
19- from selenium . webdriver . common . by import By
18+
2019from selenium .webdriver .common .keys import Keys
2120
2221from ..base_component import Selector
2625class MultiSelect (BaseControl ):
2726 """
2827 Entity-Component: Multiselect
29- Select Javascript framework: select2
3028 A dropdown which can select more than one values
3129 """
3230
@@ -37,20 +35,21 @@ def __init__(self, browser, container):
3735 """
3836 super ().__init__ (browser , container )
3937
38+ root_selector = container .select + ' [data-test="multiselect"]'
39+ self .elements .update ({"root" : Selector (select = root_selector )})
40+
4041 self .elements .update (
4142 {
42- "internal_container" : Selector (
43- select = container .select + ' [role="listbox"]'
44- ),
45- "dropdown" : Selector (select = container .select + ' [role="listbox"]' ),
4643 "selected" : Selector (
47- select = container .select
48- + ' button[data-test="selected-option"][role="option"]'
44+ select = root_selector + ' [data-test="selected-option"]'
4945 ),
46+ """
47+ Click on selected element deselects it
48+ """
5049 "deselect" : Selector (
51- select = container . select + ' [data-test="crossmark "]'
50+ select = root_selector + ' [data-test="selected-option "]'
5251 ),
53- "input" : Selector (select = container . select + ' [data-test="textbox"]' ),
52+ "input" : Selector (select = root_selector + ' [data-test="textbox"]' ),
5453 }
5554 )
5655
@@ -71,13 +70,12 @@ def search_get_list(self, value):
7170 """
7271 search with the multiselect input and return the list
7372 :param value: string value to search
74- :returns : list of values
73+ :return : list of values
7574 """
7675 self .search (value )
7776 self .wait_for_search_list ()
7877 searched_values = list (self ._list_visible_values ())
7978 self .input .send_keys (Keys .ESCAPE )
80- self .wait_for ("container" )
8179 return searched_values
8280
8381 def select (self , value ):
@@ -87,21 +85,18 @@ def select(self, value):
8785 :return: Bool returns true if selection was successful, else raises an exception
8886 """
8987 try :
90- time .sleep (1 )
9188 try :
9289 self .input .click ()
9390 except ElementClickInterceptedException :
9491 self .label_text .click ()
95- time .sleep (1 )
9692 self .input .click ()
97- time .sleep (1 )
98- popoverid = "#" + self .dropdown .get_attribute ("data-test-popover-id" )
9993
10094 except :
10195 raise Exception ("dropdown not found" )
10296
97+ popover_id = "#" + self .root .get_attribute ("data-test-popover-id" )
10398 self .elements .update (
104- {"values" : Selector (select = popoverid + ' [data-test="option"]' )}
99+ {"values" : Selector (select = popover_id + ' [data-test="option"]' )}
105100 )
106101 for each in self .get_elements ("values" ):
107102 if each .text .strip ().lower () == value .lower ():
@@ -119,11 +114,8 @@ def deselect(self, value):
119114 """
120115 for each in self .get_child_elements ("selected" ):
121116 if each .text .strip ().lower () == value .lower ():
122- time .sleep (1 )
123- each .find_element (
124- * list (self .elements ["deselect" ]._asdict ().values ())
125- ).click ()
126- self .wait_for ("internal_container" )
117+ each .click ()
118+ self .wait_for ("root" )
127119 return True
128120 else :
129121 raise ValueError ("{} not found in select list" .format (value ))
@@ -138,24 +130,23 @@ def deselect_all(self):
138130 def get_values (self ):
139131 """
140132 get list selected values
141- :returns : List of values selected within the multi-select
133+ :return : List of values selected within the multi-select
142134 """
143135 return [each .text .strip () for each in self .get_child_elements ("selected" )]
144136
145137 def list_of_values (self ):
146138 """
147139 Get list of possible values to select from dropdown
148- :returns : List of options within the multi-select dropdown
140+ :return : List of options within the multi-select dropdown
149141 """
150- self .wait_for ("internal_container" )
151- time .sleep (1 )
142+ self .wait_for ("root" )
152143 list_of_values = []
153144 self .input .click ()
154- popoverid = "#" + self .dropdown .get_attribute ("data-test-popover-id" )
145+ popover_id = "#" + self .root .get_attribute ("data-test-popover-id" )
155146 self .elements .update (
156147 {
157148 "values" : Selector (
158- select = popoverid + ' [data-test="option"] [data-test="label"]'
149+ select = popover_id + ' [data-test="option"] [data-test="label"]'
159150 )
160151 }
161152 )
@@ -172,14 +163,14 @@ def get_list_count(self):
172163 def _list_visible_values (self ):
173164 """
174165 Get list of values which are visible. Used while filtering
175- :returns : List of visible options within the multi-select dropdown
166+ :return : List of visible options within the multi-select dropdown
176167 """
177168 self .input .click ()
178- popoverid = "#" + self .dropdown .get_attribute ("data-test-popover-id" )
169+ popover_id = "#" + self .root .get_attribute ("data-test-popover-id" )
179170 self .elements .update (
180171 {
181172 "values" : Selector (
182- select = popoverid
173+ select = popover_id
183174 + ' [data-test="option"]:not([data-test-selected="true"]) [data-test="label"]'
184175 )
185176 }
0 commit comments