def test_raise_attribute_error(self): """ should raise AttributeError when trying to access a non-existent method on list and element """ with self.assertRaises(AttributeError): the_list = ElementList([Person(), Person()]) the_list.talk()
def test_attribute_error_content(self): "should raise AttributeError with right content" expected_message = "'ElementList' object has no attribute 'talk'" try: the_list = ElementList([Person(), Person()]) the_list.talk() except AttributeError, e: assert_equals(expected_message, e.message)
def test_attribute_error_for_empty(self): """ should raise AttributeError when the list is empty and someone tries to access a method or property on it """ with self.assertRaises(AttributeError): the_list = ElementList([]) the_list.unknown_method()
def test_call_method_on_first_element(self): """ when some method is missing on ElementList and is present in element, it should be passed """ the_list = ElementList([Person(), Person(), Person()]) the_list.walk() the_person = the_list.first self.assertEqual("walking", the_person.current_action)
def test_attribute_error_content(self): "should raise AttributeError with right content" with self.assertRaises(AttributeError) as cm: the_list = ElementList([Person(), Person()]) the_list.talk() expected_message = "'ElementList' object has no attribute 'talk'" e = cm.exception self.assertEqual(expected_message, e.args[0])
def find_by(self, finder, selector, original_find=None, original_query=None): elements = None end_time = time.time() + self.wait_time func_name = finder.im_func.func_name find_by = original_find or func_name[func_name.rfind('_by_') + 4:] query = original_query or selector while time.time() < end_time: try: elements = finder(selector) if not isinstance(elements, list): elements = [elements] except NoSuchElementException: pass if elements: return ElementList([self.element_class(element, self) for element in elements], find_by=find_by, query=query) return ElementList([], find_by=find_by, query=query)
def find_by_css(self, selector, original_find=None, original_query=None): find_by = original_find or "css" query = original_query or selector elements = self._element.find_elements_by_css_selector(selector) return ElementList( [self.__class__(element, self.parent) for element in elements], find_by=find_by, query=query, )
def test_is_element_present_not_preset_splinter(self, page, splinter, splinter_strategy): root_element = MagicMock() from splinter.element_list import ElementList root_element.configure_mock(**{ 'find_by_{0}.return_value'.format(splinter_strategy): ElementList([]) }) locator = (splinter_strategy, str(random.random())) assert not Region(page, root=root_element).is_element_present(*locator)
def test_is_element_displayed_not_present_splinter(self, page, splinter, splinter_strategy): root_element = Mock() locator = (splinter_strategy, str(random.random())) region = Region(page, root=root_element) from splinter.element_list import ElementList with patch('pypom.splinter_driver.Splinter.find_element', new_callable=MagicMock()) as mock_find_element: mock_find_element.return_value = ElementList([]) assert not region.is_element_displayed(*locator)
def test_not_found_exception_with_query_and_method(): """ should receive the find method and the query and use them in exception """ with pytest.raises(ElementDoesNotExist) as e: the_list = ElementList([], find_by="id", query="menu") the_list.first expected_message = 'no elements could be found with id "menu"' assert expected_message == str(e.value)
def test_is_element_present_splinter(self, page, splinter, splinter_strategy): root_element = Mock() locator = (splinter_strategy, str(random.random())) from splinter.element_list import ElementList with patch('pypom.splinter_driver.Splinter.find_element', new_callable=MagicMock()) as mock_find_element: mock_find_element.return_value = ElementList([Mock()]) assert Region(page, root=root_element).is_element_present(*locator) mock_find_element.assert_called_once_with(*locator, root=root_element)
def find_by_name(self, name): elements = [] index = 0 while True: try: control = self._browser.getControl(name=name, index=index) elements.append(control) index += 1 except IndexError: break return ElementList([ZopeTestBrowserControlElement(element, self) for element in elements], find_by="name", query=name)
def test_find_elements_splinter(self, page, splinter, splinter_strategy): locator = (splinter_strategy, str(random.random())) from splinter.element_list import ElementList page.driver.configure_mock(**{ 'find_by_{0}.return_value'.format(splinter_strategy): ElementList([]) }) Region(page).find_elements(*locator) getattr( page.driver, 'find_by_{0}'.format(splinter_strategy)).assert_called_once_with( locator[1])
def test_not_found_exception_with_query_and_method(self): """ should receive the find method and the query and use them in exception """ with self.assertRaises(ElementDoesNotExist) as cm: the_list = ElementList([], find_by="id", query="menu") the_list.first expected_message = 'no elements could be found with id "menu"' e = cm.exception self.assertEqual(expected_message, e.args[0])
def find_by_name(self, name): html = self.htmltree xpath = '//*[@name="%s"]' % name elements = [] for xpath_element in html.xpath(xpath): elements.append(xpath_element) find_by = "name" query = xpath return ElementList([DjangoClientControlElement(element, self) for element in elements], find_by=find_by, query=query)
def handle_step(self, step_info): for elem_info in step_info['elems']: name = list(elem_info.keys())[0] value = str(elem_info[name]) # potentially returns more than one element elem = self.browser.find_by_name(name) # infer action from HTML tag # fails if splinter/selenium changes their API input_type = elem._element.get_property('type') if input_type == "radio": elem = ElementList( filter(lambda e: e.value == str(value), elem)) elem.check() elif input_type == "checkbox": if value: elem.check() elif input_type == "select": elem.select(value) else: elem.fill(value)
def find_by_xpath(self, xpath): html = lxml.html.fromstring(self.html) elements = [] for xpath_element in html.xpath(xpath): if self._element_is_link(xpath_element): return self.find_link_by_text(xpath_element.text) elif self._element_is_control(xpath_element): return self.find_by_name(xpath_element.name) else: elements.append(xpath_element) return ElementList([ZopeTestBrowserElement(element) for element in elements])
def find_by_xpath(self, xpath, original_find=None, original_selector=None): html = lxml.html.fromstring(self.html) elements = [] for xpath_element in html.xpath(xpath): if self._element_is_link(xpath_element): return self.find_link_by_text(xpath_element.text) elif self._element_is_control(xpath_element): return self.find_by_name(xpath_element.name) else: elements.append(xpath_element) find_by = original_find or "xpath" query = original_selector or xpath return ElementList([ZopeTestBrowserElement(element, self) for element in elements], find_by=find_by, query=query)
def find_by_xpath(self, xpath, original_find=None, original_selector=None): html = self.htmltree elements = [] for xpath_element in html.xpath(xpath): if self._element_is_link(xpath_element): return self._find_links_by_xpath(xpath) elif self._element_is_control(xpath_element): elements.append((DjangoClientControlElement, xpath_element)) else: elements.append((DjangoClientElement, xpath_element)) find_by = original_find or "xpath" query = original_selector or xpath return ElementList([element_class(element, self) for element_class, element in elements], find_by=find_by, query=query)
def find_by(self, finder, selector, original_find=None, original_query=None, wait_time=None): """Wrapper for finding elements. Must be attached to a class. Returns: ElementList """ elements = None elem_list = [] func_name = getattr(getattr(finder, _meth_func), _func_name) find_by = original_find or func_name[func_name.rfind("_by_") + 4:] query = original_query or selector wait_time = wait_time or self.wait_time end_time = time.time() + wait_time while time.time() < end_time: try: elements = finder(selector) if not isinstance(elements, list): elements = [elements] except ( NoSuchElementException, StaleElementReferenceException, ): # This exception is sometimes thrown if the page changes # quickly pass if elements: elem_list = [ self.element_class(element, self) for element in elements ] break return ElementList(elem_list, find_by=find_by, query=query)
def find_by_xpath(self, xpath, original_find=None, original_selector=None): html = self.htmltree elements = [] for xpath_element in html.xpath(xpath): if self._element_is_link(xpath_element): return self._find_links_by_xpath(xpath) elif self._element_is_control(xpath_element) and xpath_element.name: return self.find_by_name(xpath_element.name) else: elements.append(self.get_control(xpath_element)) find_by = original_find or "xpath" query = original_selector or xpath return ElementList( [ZopeTestBrowserElement(element, self) for element in elements], find_by=find_by, query=query, )
def find_all(self, wait_time: Optional[int] = None) -> ElementList: """Get the shadowRoot element for any found elements. Returns: ElementList """ found_elements = self._element.find(wait_time) shadow_roots = [] for elem in found_elements: shadow_root = self.browser.execute_script( 'return arguments[0].shadowRoot', elem._element, ) shadow_roots.append(WebDriverElement(shadow_root, elem)) return ElementList( shadow_roots, find_by=self.strategy, query=self.locator, )
def find_by_tag(self, name): elements = self._element.cssselect(name) return ElementList( [self.__class__(element, self) for element in elements])
def find_by_id(self, id): elements = ElementList(self._element.find_elements_by_id(id)) return ElementList( [self.__class__(element, self.parent) for element in elements], find_by='id', query=id)
def test_method_that_verifies_if_the_list_is_empty(self): "should verify if the list is empty" the_list = ElementList([1, 2, 3]) self.assertFalse(the_list.is_empty()) self.assertTrue(ElementList([]).is_empty())
def find_by_name(self, name): elements = ElementList(self._element.find_elements_by_name(name)) return ElementList( [self.__class__(element, self.parent) for element in elements], find_by='name', query=name)
def find_by_tag(self, tag): elements = ElementList(self._element.find_elements_by_tag_name(tag)) return ElementList( [self.__class__(element, self.parent) for element in elements], find_by='tag', query=tag)
def find_by_id(self, id): elements = self._element.cssselect('#%s' % id) return ElementList( [self.__class__(element, self) for element in elements])
def find_by_xpath(self, selector): elements = ElementList(self._element.find_elements_by_xpath(selector)) return ElementList( [self.__class__(element, self.parent) for element in elements], find_by='xpath', query=selector)
def find_option_by_text(self, text): html = self.htmltree element = html.xpath('//option[normalize-space(text())="%s"]' % text)[0] control = DjangoClientControlElement(element.getparent(), self) return ElementList([DjangoClientOptionElement(element, control)], find_by="text", query=text)
def find_by_value(self, value): elements = self._element.cssselect('[value="%s"]' % value) return ElementList( [self.__class__(element, self) for element in elements])
def find_by_name(self, name): elements = self._element.cssselect('[name="%s"]' % name) return ElementList( [self.__class__(element, self) for element in elements])
def find_by_xpath(self, selector): elements = self._element.xpath(selector) return ElementList( [self.__class__(element, self) for element in elements])
def find_by_css(self, selector): elements = self._element.cssselect(selector) return ElementList( [self.__class__(element, self) for element in elements])
def _find_links_by_xpath(self, xpath): html = self.htmltree links = html.xpath(xpath) return ElementList([LxmlLinkElement(link, self) for link in links], find_by="xpath", query=xpath)
def find_by_id(self, id): # NOQA: A002 elements = self._element.cssselect("#%s" % id) return ElementList( [self.__class__(element, self) for element in elements])