16 from collections
import namedtuple
18 from robot.utils
import plural_or_not
19 from selenium.webdriver.common.action_chains
import ActionChains
20 from selenium.webdriver.common.keys
import Keys
31 @keyword(name='Get WebElement')
40 @keyword(name='Get WebElements')
71 actual = actual_before = self.
find_elementfind_element(locator).text
72 expected_before = expected
74 actual = actual.lower()
75 expected = expected.lower()
76 if expected
not in actual:
78 message =
"Element '%s' should have contained text '%s' but "\
79 "its text was '%s'." % (locator, expected_before, actual_before)
80 raise AssertionError(message)
81 self.
infoinfo(
"Element '%s' contains text '%s'." % (locator, expected_before))
99 expected_before = expected
101 actual = actual.lower()
102 expected = expected.lower()
103 if expected
in actual:
105 message =
"Element '%s' should not contain text '%s' but " \
106 "it did." % (locator, expected_before)
107 raise AssertionError(message)
108 self.
infoinfo(
"Element '%s' does not contain text '%s'."
109 % (locator, expected_before))
123 raise AssertionError(
"Page should have contained text '%s' "
124 "but did not." % text)
125 self.
infoinfo(
"Current page contains text '%s'." % text)
153 loglevel='TRACE', limit=None):
160 self.
infoinfo(
'Current page contains {} element(s).'.format(count))
163 message = (
'Page should have contained "{}" element(s), '
164 'but it did contain "{}" element(s).'
165 .format(limit, count))
167 raise AssertionError(message)
176 message = (
"Locator '%s' should have matched %s time%s but "
178 % (locator, x, s(x), count, s(count)))
180 raise AssertionError(message)
181 self.
infoinfo(
"Current page contains %s elements matching '%s'."
193 raise AssertionError(
"Page should not have contained text '%s'."
195 self.
infoinfo(
"Current page does not contain text '%s'." % text)
225 self.
infoinfo(
"Assigning temporary id '%s' to element '%s'." % (id, locator))
227 self.
driverdriverdriver.execute_script(
"arguments[0].id = '%s';" % id, element)
240 raise AssertionError(
"Element '%s' is enabled." % locator)
253 raise AssertionError(
"Element '%s' is disabled." % locator)
265 focused = self.
driverdriverdriver.switch_to.active_element
267 if isinstance(focused, dict):
268 focused = focused[
'value']
269 if element != focused:
270 raise AssertionError(
"Element '%s' does not have focus." % locator)
287 if not self.
find_elementfind_element(locator).is_displayed():
289 message = (
"The element '%s' should be visible, but it "
291 raise AssertionError(message)
292 self.
infoinfo(
"Element '%s' is displayed." % locator)
301 element = self.
find_elementfind_element(locator, required=
False)
303 self.
infoinfo(
"Element '%s' did not exist." % locator)
304 elif not element.is_displayed():
305 self.
infoinfo(
"Element '%s' exists but is not displayed." % locator)
308 message = (
"The element '%s' should not be visible, "
309 "but it is." % locator)
310 raise AssertionError(message)
329 self.
infoinfo(
"Verifying element '%s' contains exact text '%s'."
330 % (locator, expected))
331 text = before_text = self.
find_elementfind_element(locator).text
334 expected = expected.lower()
337 message = (
"The text of element '%s' should have been '%s' "
339 % (locator, expected, before_text))
340 raise AssertionError(message)
357 self.
infoinfo(
"Verifying element '%s' does not contains exact text '%s'."
358 % (locator, not_expected))
360 before_not_expected = not_expected
363 not_expected = not_expected.lower()
364 if text == not_expected:
366 message = (
"The text of element '%s' was not supposed to be '%s'."
367 % (locator, before_not_expected))
368 raise AssertionError(message)
384 return self.
find_elementfind_element(locator).get_attribute(attribute)
398 current_expected = self.
find_elementfind_element(locator).get_attribute(attribute)
399 if current_expected != expected:
401 message = (
"Element '%s' attribute should have value '%s' but "
402 "its value was '%s'." % (locator, expected, current_expected))
403 raise AssertionError(message)
404 self.
infoinfo(
"Element '%s' attribute '%s' contains value '%s'." % (locator, attribute, expected))
418 return self.
find_elementfind_element(locator).location[
'x']
433 return element.size[
'width'], element.size[
'height']
451 for element
in elements:
453 old_element = arguments[0];
454 let newDiv = document.createElement('div');
455 newDiv.setAttribute("name", "covered");
456 newDiv.style.backgroundColor = 'blue';
457 newDiv.style.zIndex = '999';
458 newDiv.style.top = old_element.offsetTop + 'px';
459 newDiv.style.left = old_element.offsetLeft + 'px';
460 newDiv.style.height = old_element.offsetHeight + 'px';
461 newDiv.style.width = old_element.offsetWidth + 'px';
462 old_element.parentNode.insertBefore(newDiv, old_element);
463 old_element.remove();
464 newDiv.parentNode.style.overflow = 'hidden';
507 return self.
find_elementfind_element(locator).location[
'y']
523 self.
infoinfo(
"Clicking button '%s'." % locator)
524 element = self.
find_elementfind_element(locator, tag=
'input', required=
False)
526 element = self.
find_elementfind_element(locator, tag=
'button')
545 self.
infoinfo(
"Clicking image '%s'." % locator)
546 element = self.
find_elementfind_element(locator, tag=
'image', required=
False)
549 element = self.
find_elementfind_element(locator, tag=
'input')
568 self.
infoinfo(
"Clicking link '%s'." % locator)
569 self.
find_elementfind_element(locator, tag=
'link').click()
597 self.
infoinfo(
"Clicking element '%s'." % locator)
603 self.
infoinfo(
"Clicking %s '%s' with %s." % (tag
if tag[0]
else 'element', locator, modifier))
606 for item
in modifier:
607 action.key_down(item)
608 element = self.
find_elementfind_element(locator, tag=tag[0], required=
False)
610 element = self.
find_elementfind_element(locator, tag=tag[1])
611 action.click(element)
612 for item
in modifier:
626 self.
infoinfo(
"Clicking element '%s' at coordinates x=%s, y=%s."
627 % (locator, xoffset, yoffset))
630 action.move_to_element(element)
631 action.move_by_offset(xoffset, yoffset)
642 self.
infoinfo(
"Double clicking element '%s'." % locator)
645 action.double_click(element).perform()
657 self.
driverdriverdriver.execute_script(
"arguments[0].focus();", element)
669 ActionChains(self.
driverdriverdriver).move_to_element(element).perform()
685 action.drag_and_drop(element, target).perform()
702 action.drag_and_drop_by_offset(element, int(xoffset), int(yoffset))
717 self.
infoinfo(
"Simulating Mouse Down on element '%s'." % locator)
720 action.click_and_hold(element).perform()
729 self.
infoinfo(
"Simulating Mouse Out on element '%s'." % locator)
732 offsetx = (size[
'width'] / 2) + 1
733 offsety = (size[
'height'] / 2) + 1
735 action.move_to_element(element).move_by_offset(offsetx, offsety)
745 self.
infoinfo(
"Simulating Mouse Over on element '%s'." % locator)
748 action.move_to_element(element).perform()
757 self.
infoinfo(
"Simulating Mouse Up on element '%s'." % locator)
759 ActionChains(self.
driverdriverdriver).release(element).perform()
766 action.context_click(element).perform()
782 element = arguments[0];
783 eventName = arguments[1];
784 if (document.createEventObject) { // IE
785 return element.fireEvent('on' + eventName, document.createEventObject());
787 var evt = document.createEvent("HTMLEvents");
788 evt.initEvent(eventName, true, true);
789 return !element.dispatchEvent(evt);
791 self.
driverdriverdriver.execute_script(script, element, event)
796 if key.startswith(
'\\')
and len(key) > 1:
799 element.send_keys(key)
852 self.
infoinfo(
'Sending key(s) %s to %s element.' % (keys, locator))
854 self.
infoinfo(
'Sending key(s) %s to page.' % str(keys))
862 for parsed_key
in parsed_keys:
865 for key
in parsed_key:
871 for special_key
in special_keys:
872 self.
infoinfo(
'Releasing special key %s.' % special_key.original)
873 actions.key_up(special_key.converted)
877 self.
infoinfo(
'Sending key%s %s' % (plural_or_not(key.converted), key.converted))
879 actions.send_keys_to_element(element, key.converted)
881 actions.send_keys(key.converted)
884 if len(parsed_key) == 1
and element:
885 self.
infoinfo(
'Pressing special key %s to element.' % key.original)
886 actions.send_keys_to_element(element, key.converted)
887 elif len(parsed_key) == 1
and not element:
888 self.
infoinfo(
'Pressing special key %s to browser.' % key.original)
889 actions.send_keys(key.converted)
891 self.
infoinfo(
'Pressing special key %s down.' % key.original)
892 actions.key_down(key.converted)
893 special_keys.append(key)
903 return [link.get_attribute(
'id')
for link
in links]
913 element = self.
find_elementfind_element(locator, tag=
'link')
915 action.click_and_hold(element).perform()
951 element = self.
find_elementfind_element(locator, tag=
'image')
953 action.click_and_hold(element).perform()
1041 key = map.get(key_code)
1048 return getattr(Keys, key_name)
1049 except AttributeError:
1050 message =
"Unknown key named '%s'." % (key_name)
1051 self.
debugdebug(message)
1052 raise ValueError(message)
1060 subframes = self.
find_elementsfind_elements(
"xpath://frame|//iframe")
1061 self.
debugdebug(
'Current frame has %d subframes.' % len(subframes))
1062 for frame
in subframes:
1071 modifier = modifier.upper()
1072 modifiers = modifier.split(
'+')
1074 for item
in modifiers:
1077 if hasattr(Keys, item):
1078 keys.append(getattr(Keys, item))
1080 raise ValueError(
"'%s' modifier does not match to Selenium Keys"
1086 raise AssertionError(
'"keys" argument can not be empty.')
1091 list_keys.append(separate_keys)
1105 if char ==
'+' and one_key !=
'':
1106 list_keys.append(one_key)
1111 list_keys.append(one_key)
1115 KeysRecord = namedtuple(
'KeysRecord',
'converted, original')
1120 converted_keys.append(KeysRecord(getattr(Keys, key), key))
1122 converted_keys.append(KeysRecord(key, key))
1123 return converted_keys
1127 return hasattr(Keys, key)
1128 except UnicodeError:
def element_finder(self, value)
def find_element(self, locator, tag=None, required=True, parent=None)
Find element matching locator.
def find_elements(self, locator, tag=None, parent=None)
Find all elements matching locator.
def is_text_present(self, text)
def is_element_enabled(self, locator, tag=None)
def debug(self, msg, html=False)
def log_source(self, loglevel='INFO')
def assert_page_not_contains(self, locator, tag=None, message=None, loglevel='TRACE')
def info(self, msg, html=False)
def assert_page_contains(self, locator, tag=None, message=None, loglevel='TRACE')
def add_location_strategy(self, strategy_name, strategy_keyword, persist=False)
Adds a custom location strategy.
def _separate_key(self, key)
def _convert_special_keys(self, keys)
def mouse_down_on_image(self, locator)
Simulates a mouse down event on an image identified by locator.
def mouse_over(self, locator)
Simulates hovering mouse over the element locator.
def get_webelement(self, locator)
Returns the first WebElement matching the given locator.
def page_should_contain(self, text, loglevel='TRACE')
Verifies that current page contains text.
def element_should_contain(self, locator, expected, message=None, ignore_case=False)
Verifies that element locator contains text expected.
def clear_element_text(self, locator)
Clears the value of text entry element identified by locator.
def get_webelements(self, locator)
Returns list of WebElement objects matching the locator.
def _map_named_key_code_to_special_key(self, key_name)
def page_should_not_contain_link(self, locator, message=None, loglevel='TRACE')
Verifies link identified by locator is not found from current page.
def element_text_should_not_be(self, locator, not_expected, message=None, ignore_case=False)
Verifies that element locator does not contain exact text not_expected.
def get_element_size(self, locator)
Returns width and height of element identified by locator.
def _page_contains(self, text)
def element_should_be_disabled(self, locator)
Verifies that element identified with locator is disabled.
def set_focus_to_element(self, locator)
Sets focus to element identified by locator.
def _click_with_modifier(self, locator, tag, modifier)
def simulate_event(self, locator, event)
Simulates event on element identified by locator.
def element_should_not_be_visible(self, locator, message=None)
Verifies that the element identified by locator is NOT visible.
def click_element_at_coordinates(self, locator, xoffset, yoffset)
Click element locator at xoffset/yoffset.
def press_key(self, locator, key)
DEPRECATED in SeleniumLibrary 4.0.
def remove_location_strategy(self, strategy_name)
Removes a previously added custom location strategy.
def _press_keys(self, locator, parsed_keys)
def get_all_links(self)
Returns a list containing ids of all links found in current page.
def page_should_not_contain_image(self, locator, message=None, loglevel='TRACE')
Verifies image identified by locator is found from current page.
def open_context_menu(self, locator)
Opens context menu on element identified by locator.
def mouse_up(self, locator)
Simulates releasing the left mouse button on the element locator.
def _selenium_keys_has_attr(self, key)
def get_element_count(self, locator)
Returns number of elements matching locator.
def element_should_be_visible(self, locator, message=None)
Verifies that the element identified by locator is visible.
def drag_and_drop_by_offset(self, locator, xoffset, yoffset)
Drags element identified with locator by xoffset/yoffset.
def get_text(self, locator)
Returns the text value of element identified by locator.
def page_should_not_contain(self, text, loglevel='TRACE')
Verifies the current page does not contain text.
def element_should_not_contain(self, locator, expected, message=None, ignore_case=False)
Verifies that element locator does not contains text expected.
def element_text_should_be(self, locator, expected, message=None, ignore_case=False)
Verifies that element locator contains exact text expected.
def click_image(self, locator, modifier=False)
Clicks an image identified by locator.
def _map_ascii_key_code_to_key(self, key_code)
def _press_keys_normal_keys(self, actions, element, key)
def get_value(self, locator)
Returns the value attribute of element identified by locator.
def mouse_down_on_link(self, locator)
Simulates a mouse down event on a link identified by locator.
def get_element_attribute(self, locator, attribute)
Returns value of attribute from element locator.
def mouse_down(self, locator)
Simulates pressing the left mouse button on the element locator.
def mouse_out(self, locator)
Simulates moving mouse away from the element locator.
def _parse_aliases(self, key)
def press_keys(self, locator=None, *keys)
Simulates user pressing key(s) to an element or on the active browser.
def click_link(self, locator, modifier=False)
Clicks a link identified by locator.
def parse_modifier(self, modifier)
def element_attribute_value_should_be(self, locator, attribute, expected, message=None)
Verifies element identified by locator contains expected attribute value.
def scroll_element_into_view(self, locator)
Scrolls an element identified by locator into view.
def page_should_contain_image(self, locator, message=None, loglevel='TRACE')
Verifies image identified by locator is found from current page.
def page_should_not_contain_element(self, locator, message=None, loglevel='TRACE')
Verifies that element locator is found on the current page.
def element_should_be_enabled(self, locator)
Verifies that element identified with locator is enabled.
def element_should_be_focused(self, locator)
Verifies that element identified with locator is focused.
def drag_and_drop(self, locator, target)
Drags element identified by locator into target element.
def page_should_contain_element(self, locator, message=None, loglevel='TRACE', limit=None)
Verifies that element locator is found on the current page.
def click_button(self, locator, modifier=False)
Clicks button identified by locator.
def page_should_contain_link(self, locator, message=None, loglevel='TRACE')
Verifies link identified by locator is found from current page.
def assign_id_to_element(self, locator, id)
Assigns temporary id to element specified by locator.
def click_element(self, locator, modifier=False)
Click element identified by locator.
def _parse_keys(self, *keys)
def get_horizontal_position(self, locator)
Returns horizontal position of element identified by locator.
def get_vertical_position(self, locator)
Returns vertical position of element identified by locator.
def cover_element(self, locator)
Will cover elements identified by locator with a blue div without breaking page layout.
def double_click_element(self, locator)
Double click element identified by locator.
def locator_should_match_x_times(self, locator, x, message=None, loglevel='TRACE')
DEPRECATED in SeleniumLibrary 4.0.
def _press_keys_special_keys(self, actions, element, parsed_key, key, special_keys)