Robot Framework SeleniumLibrary
formelement.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 import os
18 
19 from SeleniumLibrary.base import LibraryComponent, keyword
20 from SeleniumLibrary.errors import ElementNotFound
21 from SeleniumLibrary.utils import is_noney, is_truthy
22 
23 
25 
26  @keyword
27 
34  def submit_form(self, locator=None):
35  self.infoinfo("Submitting form '%s'." % locator)
36  if is_noney(locator):
37  locator = 'tag:form'
38  element = self.find_elementfind_element(locator, tag='form')
39  element.submit()
40 
41  @keyword
42 
47  def checkbox_should_be_selected(self, locator):
48  self.infoinfo("Verifying checkbox '%s' is selected." % locator)
49  element = self._get_checkbox_get_checkbox(locator)
50  if not element.is_selected():
51  raise AssertionError("Checkbox '%s' should have been selected "
52  "but was not." % locator)
53 
54  @keyword
55 
60  def checkbox_should_not_be_selected(self, locator):
61  self.infoinfo("Verifying checkbox '%s' is not selected." % locator)
62  element = self._get_checkbox_get_checkbox(locator)
63  if element.is_selected():
64  raise AssertionError("Checkbox '%s' should not have been "
65  "selected." % locator)
66 
67  @keyword
68 
76  def page_should_contain_checkbox(self, locator, message=None, loglevel='TRACE'):
77  self.assert_page_containsassert_page_contains(locator, 'checkbox', message, loglevel)
78 
79  @keyword
80 
88  def page_should_not_contain_checkbox(self, locator, message=None, loglevel='TRACE'):
89  self.assert_page_not_containsassert_page_not_contains(locator, 'checkbox', message, loglevel)
90 
91  @keyword
92 
99  def select_checkbox(self, locator):
100  self.infoinfo("Selecting checkbox '%s'." % locator)
101  element = self._get_checkbox_get_checkbox(locator)
102  if not element.is_selected():
103  element.click()
104 
105  @keyword
106 
113  def unselect_checkbox(self, locator):
114  self.infoinfo("Unselecting checkbox '%s'." % locator)
115  element = self._get_checkbox_get_checkbox(locator)
116  if element.is_selected():
117  element.click()
118 
119  @keyword
120 
129  def page_should_contain_radio_button(self, locator, message=None, loglevel='TRACE'):
130  self.assert_page_containsassert_page_contains(locator, 'radio button', message, loglevel)
131 
132  @keyword
133 
142  def page_should_not_contain_radio_button(self, locator, message=None, loglevel='TRACE'):
143  self.assert_page_not_containsassert_page_not_contains(locator, 'radio button', message,
144  loglevel)
145 
146  @keyword
147 
151  def radio_button_should_be_set_to(self, group_name, value):
152  self.infoinfo("Verifying radio button '%s' has selection '%s'."
153  % (group_name, value))
154  elements = self._get_radio_buttons_get_radio_buttons(group_name)
155  actual_value = self._get_value_from_radio_buttons_get_value_from_radio_buttons(elements)
156  if actual_value is None or actual_value != value:
157  raise AssertionError("Selection of radio button '%s' should have "
158  "been '%s' but was '%s'."
159  % (group_name, value, actual_value))
160 
161  @keyword
162 
166  def radio_button_should_not_be_selected(self, group_name):
167  self.infoinfo("Verifying radio button '%s' has no selection." % group_name)
168  elements = self._get_radio_buttons_get_radio_buttons(group_name)
169  actual_value = self._get_value_from_radio_buttons_get_value_from_radio_buttons(elements)
170  if actual_value is not None:
171  raise AssertionError("Radio button group '%s' should not have "
172  "had selection, but '%s' was selected."
173  % (group_name, actual_value))
174 
175  @keyword
176 
187  def select_radio_button(self, group_name, value):
188  self.infoinfo("Selecting '%s' from radio button '%s'."
189  % (value, group_name))
190  element = self._get_radio_button_with_value_get_radio_button_with_value(group_name, value)
191  if not element.is_selected():
192  element.click()
193 
194  @keyword
195 
209  def choose_file(self, locator, file_path):
210  if not os.path.isfile(file_path):
211  raise ValueError("File '%s' does not exist on the local file "
212  "system." % file_path)
213  self.find_elementfind_element(locator).send_keys(file_path)
214 
215  @keyword
216 
240  def input_password(self, locator, password, clear=True):
241  self.infoinfo("Typing password into text field '%s'." % locator)
242  self._input_text_into_text_field_input_text_into_text_field(locator, password, clear)
243 
244  @keyword
245 
258  def input_text(self, locator, text, clear=True):
259  self.infoinfo("Typing text '%s' into text field '%s'." % (text, locator))
260  self._input_text_into_text_field_input_text_into_text_field(locator, text, clear)
261 
262  @keyword
263 
271  def page_should_contain_textfield(self, locator, message=None, loglevel='TRACE'):
272  self.assert_page_containsassert_page_contains(locator, 'text field', message, loglevel)
273 
274  @keyword
275 
283  def page_should_not_contain_textfield(self, locator, message=None, loglevel='TRACE'):
284  self.assert_page_not_containsassert_page_not_contains(locator, 'text field', message, loglevel)
285 
286  @keyword
287 
294  def textfield_should_contain(self, locator, expected, message=None):
295  actual = self._get_value_get_value(locator, 'text field')
296  if expected not in actual:
297  if is_noney(message):
298  message = "Text field '%s' should have contained text '%s' "\
299  "but it contained '%s'." % (locator, expected, actual)
300  raise AssertionError(message)
301  self.infoinfo("Text field '%s' contains text '%s'." % (locator, expected))
302 
303  @keyword
304 
311  def textfield_value_should_be(self, locator, expected, message=None):
312  actual = self._get_value_get_value(locator, 'text field')
313  if actual != expected:
314  if is_noney(message):
315  message = "Value of text field '%s' should have been '%s' "\
316  "but was '%s'." % (locator, expected, actual)
317  raise AssertionError(message)
318  self.infoinfo("Content of text field '%s' is '%s'." % (locator, expected))
319 
320  @keyword
321 
328  def textarea_should_contain(self, locator, expected, message=None):
329  actual = self._get_value_get_value(locator, 'text area')
330  if expected not in actual:
331  if is_noney(message):
332  message = "Text area '%s' should have contained text '%s' " \
333  "but it had '%s'." % (locator, expected, actual)
334  raise AssertionError(message)
335  self.infoinfo("Text area '%s' contains text '%s'." % (locator, expected))
336 
337  @keyword
338 
345  def textarea_value_should_be(self, locator, expected, message=None):
346  actual = self._get_value_get_value(locator, 'text area')
347  if expected != actual:
348  if is_noney(message):
349  message = "Text area '%s' should have had text '%s' " \
350  "but it had '%s'." % (locator, expected, actual)
351  raise AssertionError(message)
352  self.infoinfo("Content of text area '%s' is '%s'." % (locator, expected))
353 
354  @keyword
355 
364  def page_should_contain_button(self, locator, message=None, loglevel='TRACE'):
365  try:
366  self.assert_page_containsassert_page_contains(locator, 'input', message, loglevel)
367  except AssertionError:
368  self.assert_page_containsassert_page_contains(locator, 'button', message, loglevel)
369 
370  @keyword
371 
380  def page_should_not_contain_button(self, locator, message=None, loglevel='TRACE'):
381  self.assert_page_not_containsassert_page_not_contains(locator, 'button', message, loglevel)
382  self.assert_page_not_containsassert_page_not_contains(locator, 'input', message, loglevel)
383 
384  def _get_value(self, locator, tag):
385  return self.find_elementfind_element(locator, tag).get_attribute('value')
386 
387  def _get_checkbox(self, locator):
388  return self.find_elementfind_element(locator, tag='checkbox')
389 
390  def _get_radio_buttons(self, group_name):
391  xpath = "xpath://input[@type='radio' and @name='%s']" % group_name
392  self.debugdebug('Radio group locator: ' + xpath)
393  elements = self.find_elementsfind_elements(xpath)
394  if not elements:
395  raise ElementNotFound("No radio button with name '%s' found."
396  % group_name)
397  return elements
398 
399  def _get_radio_button_with_value(self, group_name, value):
400  xpath = "xpath://input[@type='radio' and @name='%s' and " \
401  "(@value='%s' or @id='%s')]" % (group_name, value, value)
402  self.debugdebug('Radio group locator: ' + xpath)
403  try:
404  return self.find_elementfind_element(xpath)
405  except ElementNotFound:
406  raise ElementNotFound("No radio button with name '%s' and "
407  "value '%s' found." % (group_name, value))
408 
409  def _get_value_from_radio_buttons(self, elements):
410  for element in elements:
411  if element.is_selected():
412  return element.get_attribute('value')
413  return None
414 
415  def _input_text_into_text_field(self, locator, text, clear):
416  element = self.find_elementfind_element(locator)
417  if is_truthy(clear):
418  element.clear()
419  element.send_keys(text)
def find_element(self, locator, tag=None, required=True, parent=None)
Find element matching locator.
Definition: context.py:72
def find_elements(self, locator, tag=None, parent=None)
Find all elements matching locator.
Definition: context.py:88
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 input_text(self, locator, text, clear=True)
Types the given text into text field identified by locator.
Definition: formelement.py:258
def page_should_not_contain_radio_button(self, locator, message=None, loglevel='TRACE')
Verifies radio button locator is not found from current page.
Definition: formelement.py:142
def page_should_contain_radio_button(self, locator, message=None, loglevel='TRACE')
Verifies radio button locator is found from current page.
Definition: formelement.py:129
def page_should_contain_button(self, locator, message=None, loglevel='TRACE')
Verifies button locator is found from current page.
Definition: formelement.py:364
def input_password(self, locator, password, clear=True)
Types the given password into text field identified by locator.
Definition: formelement.py:240
def textfield_value_should_be(self, locator, expected, message=None)
Verifies text field locator has exactly text expected.
Definition: formelement.py:311
def page_should_contain_checkbox(self, locator, message=None, loglevel='TRACE')
Verifies checkbox locator is found from current page.
Definition: formelement.py:76
def page_should_not_contain_checkbox(self, locator, message=None, loglevel='TRACE')
Verifies checkbox locator is not found from current page.
Definition: formelement.py:88
def select_radio_button(self, group_name, value)
Sets radio button group group_name to value.
Definition: formelement.py:187
def _input_text_into_text_field(self, locator, text, clear)
Definition: formelement.py:415
def radio_button_should_be_set_to(self, group_name, value)
Verifies radio button group group_name is set to value.
Definition: formelement.py:151
def checkbox_should_be_selected(self, locator)
Verifies checkbox locator is selected/checked.
Definition: formelement.py:47
def choose_file(self, locator, file_path)
Inputs the file_path into file input field locator.
Definition: formelement.py:209
def page_should_not_contain_button(self, locator, message=None, loglevel='TRACE')
Verifies button locator is not found from current page.
Definition: formelement.py:380
def radio_button_should_not_be_selected(self, group_name)
Verifies radio button group group_name has no selection.
Definition: formelement.py:166
def page_should_not_contain_textfield(self, locator, message=None, loglevel='TRACE')
Verifies text field locator is not found from current page.
Definition: formelement.py:283
def textarea_value_should_be(self, locator, expected, message=None)
Verifies text area locator has exactly text expected.
Definition: formelement.py:345
def checkbox_should_not_be_selected(self, locator)
Verifies checkbox locator is not selected/checked.
Definition: formelement.py:60
def submit_form(self, locator=None)
Submits a form identified by locator.
Definition: formelement.py:34
def unselect_checkbox(self, locator)
Removes selection of checkbox identified by locator.
Definition: formelement.py:113
def page_should_contain_textfield(self, locator, message=None, loglevel='TRACE')
Verifies text field locator is found from current page.
Definition: formelement.py:271
def textfield_should_contain(self, locator, expected, message=None)
Verifies text field locator contains text expected.
Definition: formelement.py:294
def _get_radio_button_with_value(self, group_name, value)
Definition: formelement.py:399
def textarea_should_contain(self, locator, expected, message=None)
Verifies text area locator contains text expected.
Definition: formelement.py:328
def select_checkbox(self, locator)
Selects checkbox identified by locator.
Definition: formelement.py:99