Robot Framework SeleniumLibrary
selectelement.py
Go to the documentation of this file.
1 # Copyright 2008-2011 Nokia Networks
2 # Copyright 2011-2016 Ryan Tomac, Ed Manlove and contributors
3 # Copyright 2016- Robot Framework Foundation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 from selenium.common.exceptions import NoSuchElementException
18 from selenium.webdriver.support.ui import Select
19 
20 from SeleniumLibrary.base import LibraryComponent, keyword
21 from SeleniumLibrary.utils import is_truthy, plural_or_not as s
22 
23 
25 
26  @keyword
27 
42  def get_list_items(self, locator, values=False):
43  options = self._get_options_get_options(locator)
44  if is_truthy(values):
45  return self._get_values_get_values(options)
46  else:
47  return self._get_labels_get_labels(options)
48 
49  @keyword
50 
58  def get_selected_list_label(self, locator):
59  select = self._get_select_list_get_select_list(locator)
60  return select.first_selected_option.text
61 
62  @keyword
63 
71  def get_selected_list_labels(self, locator):
72  options = self._get_selected_options_get_selected_options(locator)
73  return self._get_labels_get_labels(options)
74 
75  @keyword
76 
84  def get_selected_list_value(self, locator):
85  select = self._get_select_list_get_select_list(locator)
86  return select.first_selected_option.get_attribute('value')
87 
88  @keyword
89 
97  def get_selected_list_values(self, locator):
98  options = self._get_selected_options_get_selected_options(locator)
99  return self._get_values_get_values(options)
100 
101  @keyword
102 
120  def list_selection_should_be(self, locator, *expected):
121  self.infoinfo("Verifying list '%s' has option%s [ %s ] selected."
122  % (locator, s(expected), ' | '.join(expected)))
123  self.page_should_contain_listpage_should_contain_list(locator)
124  options = self._get_selected_options_get_selected_options(locator)
125  labels = self._get_labels_get_labels(options)
126  values = self._get_values_get_values(options)
127  if sorted(expected) not in [sorted(labels), sorted(values)]:
128  raise AssertionError("List '%s' should have had selection [ %s ] "
129  "but selection was [ %s ]."
130  % (locator, ' | '.join(expected),
131  self._format_selection_format_selection(labels, values)))
132 
133  def _format_selection(self, labels, values):
134  return ' | '.join('%s (%s)' % (label, value)
135  for label, value in zip(labels, values))
136 
137  @keyword
138 
143  def list_should_have_no_selections(self, locator):
144  self.infoinfo("Verifying list '%s' has no selections." % locator)
145  options = self._get_selected_options_get_selected_options(locator)
146  if options:
147  selection = self._format_selection_format_selection(self._get_labels_get_labels(options),
148  self._get_values_get_values(options))
149  raise AssertionError("List '%s' should have had no selection "
150  "but selection was [ %s ]."
151  % (locator, selection))
152 
153  @keyword
154 
162  def page_should_contain_list(self, locator, message=None, loglevel='TRACE'):
163  self.assert_page_containsassert_page_contains(locator, 'list', message, loglevel)
164 
165  @keyword
166 
174  def page_should_not_contain_list(self, locator, message=None, loglevel='TRACE'):
175  self.assert_page_not_containsassert_page_not_contains(locator, 'list', message, loglevel)
176 
177  @keyword
178 
183  def select_all_from_list(self, locator):
184  self.infoinfo("Selecting all options from list '%s'." % locator)
185  select = self._get_select_list_get_select_list(locator)
186  if not select.is_multiple:
187  raise RuntimeError("'Select All From List' works only with "
188  "multi-selection lists.")
189  for i in range(len(select.options)):
190  select.select_by_index(i)
191 
192  @keyword
193 
205  def select_from_list_by_index(self, locator, *indexes):
206  if not indexes:
207  raise ValueError("No indexes given.")
208  self.infoinfo("Selecting options from selection list '%s' by index%s %s."
209  % (locator, '' if len(indexes) == 1 else 'es',
210  ', '.join(indexes)))
211  select = self._get_select_list_get_select_list(locator)
212  for index in indexes:
213  select.select_by_index(int(index))
214 
215  @keyword
216 
226  def select_from_list_by_value(self, locator, *values):
227  if not values:
228  raise ValueError("No values given.")
229  self.infoinfo("Selecting options from selection list '%s' by value%s %s."
230  % (locator, s(values), ', '.join(values)))
231  select = self._get_select_list_get_select_list(locator)
232  for value in values:
233  select.select_by_value(value)
234 
235  @keyword
236 
246  def select_from_list_by_label(self, locator, *labels):
247  if not labels:
248  raise ValueError("No labels given.")
249  self.infoinfo("Selecting options from selection list '%s' by label%s %s."
250  % (locator, s(labels), ', '.join(labels)))
251  select = self._get_select_list_get_select_list(locator)
252  for label in labels:
253  select.select_by_visible_text(label)
254 
255  @keyword
256 
263  def unselect_all_from_list(self, locator):
264  self.infoinfo("Unselecting all options from list '%s'." % locator)
265  select = self._get_select_list_get_select_list(locator)
266  if not select.is_multiple:
267  raise RuntimeError("Un-selecting options works only with "
268  "multi-selection lists.")
269  select.deselect_all()
270 
271  @keyword
272 
280  def unselect_from_list_by_index(self, locator, *indexes):
281  if not indexes:
282  raise ValueError("No indexes given.")
283  self.infoinfo("Un-selecting options from selection list '%s' by index%s "
284  "%s." % (locator, '' if len(indexes) == 1 else 'es',
285  ', '.join(indexes)))
286  select = self._get_select_list_get_select_list(locator)
287  if not select.is_multiple:
288  raise RuntimeError("Un-selecting options works only with "
289  "multi-selection lists.")
290  for index in indexes:
291  select.deselect_by_index(int(index))
292 
293  @keyword
294 
301  def unselect_from_list_by_value(self, locator, *values):
302  if not values:
303  raise ValueError("No values given.")
304  self.infoinfo("Un-selecting options from selection list '%s' by value%s "
305  "%s." % (locator, s(values), ', '.join(values)))
306  select = self._get_select_list_get_select_list(locator)
307  if not select.is_multiple:
308  raise RuntimeError("Un-selecting options works only with "
309  "multi-selection lists.")
310  for value in values:
311  select.deselect_by_value(value)
312 
313  @keyword
314 
321  def unselect_from_list_by_label(self, locator, *labels):
322  if not labels:
323  raise ValueError("No labels given.")
324  self.infoinfo("Un-selecting options from selection list '%s' by label%s "
325  "%s." % (locator, s(labels), ', '.join(labels)))
326  select = self._get_select_list_get_select_list(locator)
327  if not select.is_multiple:
328  raise RuntimeError("Un-selecting options works only with "
329  "multi-selection lists.")
330  for label in labels:
331  select.deselect_by_visible_text(label)
332 
333  def _get_select_list(self, locator):
334  el = self.find_elementfind_element(locator, tag='list')
335  return Select(el)
336 
337  def _get_options(self, locator):
338  return self._get_select_list_get_select_list(locator).options
339 
340  def _get_selected_options(self, locator):
341  return self._get_select_list_get_select_list(locator).all_selected_options
342 
343  def _get_labels(self, options):
344  return [opt.text for opt in options]
345 
346  def _get_values(self, options):
347  return [opt.get_attribute('value') for opt in options]
def find_element(self, locator, tag=None, required=True, parent=None)
Find element matching locator.
Definition: context.py:72
def assert_page_not_contains(self, locator, tag=None, message=None, loglevel='TRACE')
def assert_page_contains(self, locator, tag=None, message=None, loglevel='TRACE')
def get_selected_list_label(self, locator)
Returns label of selected option from selection list locator.
def select_from_list_by_value(self, locator, *values)
Selects options from selection list locator by values.
def get_list_items(self, locator, values=False)
Returns all labels or values of selection list locator.
def unselect_from_list_by_index(self, locator, *indexes)
Unselects options from selection list locator by indexes.
def page_should_not_contain_list(self, locator, message=None, loglevel='TRACE')
Verifies selection list locator is not found from current page.
def get_selected_list_labels(self, locator)
Returns labels of selected options from selection list locator.
def unselect_from_list_by_label(self, locator, *labels)
Unselects options from selection list locator by labels.
def select_from_list_by_label(self, locator, *labels)
Selects options from selection list locator by labels.
def unselect_from_list_by_value(self, locator, *values)
Unselects options from selection list locator by values.
def get_selected_list_value(self, locator)
Returns value of selected option from selection list locator.
def select_all_from_list(self, locator)
Selects all options from multi-selection list locator.
def get_selected_list_values(self, locator)
Returns values of selected options from selection list locator.
def list_selection_should_be(self, locator, *expected)
Verifies selection list locator has expected options selected.
def select_from_list_by_index(self, locator, *indexes)
Selects options from selection list locator by indexes.
def unselect_all_from_list(self, locator)
Unselects all options from multi-selection list locator.
def list_should_have_no_selections(self, locator)
Verifies selection list locator has no options selected.
def page_should_contain_list(self, locator, message=None, loglevel='TRACE')
Verifies selection list locator is found from current page.