def test_dropdown_menus_with_divider(web_fixture): """You can add a divider to a DropdownMenu.""" sub_menu = DropdownMenu(web_fixture.view) sub_menu.add_a(A(web_fixture.view, Url('/an/url'), description='sub menu item')) sub_menu.add_divider() sub_menu.add_a(A(web_fixture.view, Url('/another/url'), description='another sub menu item')) [item1, divider, item2] = sub_menu.html_representation.children assert 'dropdown-divider' in divider.get_attribute('class').split()
def __init__(self, view): super().__init__(view, 'simple_form') self.use_layout(FormLayout()) if self.exception: self.layout.add_alert_for_domain_exception(self.exception) domain_object = self.get_or_create_domain_object() link = self.add_child( A(view, Url('/'), description='Open another tab...')) link.set_attribute('target', '_blank') self.add_child( P(view, text= '...and increment the value there. Come back here and submit the value. A Concurrency error will be shown' )) #Your own widget that tracks changes self.add_child(MyConcurrencyWidget(view, domain_object)) self.layout.add_input( TextInput(self, domain_object.fields.some_field_value)) self.define_event_handler(domain_object.events.submit) self.add_child(Button(self, domain_object.events.submit)) self.define_event_handler(domain_object.events.increment) self.add_child(Button(self, domain_object.events.increment))
def test_dropdown_menus(web_fixture): """You can add a DropdownMenu as a dropdown inside a Nav.""" menu = Nav(web_fixture.view) sub_menu = DropdownMenu(web_fixture.view) sub_menu.add_a(A(web_fixture.view, Url('/an/url'), description='sub menu item')) menu.add_dropdown('Dropdown title', sub_menu) [item] = menu.html_representation.children assert item.tag_name == 'li' assert 'dropdown' in item.get_attribute('class') [toggle, added_sub_menu] = item.children assert 'dropdown-toggle' in toggle.get_attribute('class') assert 'button' in toggle.get_attribute('role') assert 'true' in toggle.get_attribute('aria-haspopup') assert 'dropdown' in toggle.get_attribute('data-toggle') title_text = toggle.children[0].value assert title_text == 'Dropdown title' assert added_sub_menu is sub_menu assert 'dropdown-menu' in added_sub_menu.html_representation.get_attribute('class').split() assert isinstance(added_sub_menu.html_representation, Div) [dropdown_item] = added_sub_menu.html_representation.children assert isinstance(dropdown_item, A) assert 'dropdown-item' in dropdown_item.get_attribute('class').split()
def dropdown_menus(fixture): """You can add a DropdownMenu as a dropdown inside a Nav.""" menu = Nav(fixture.view) sub_menu = DropdownMenu(fixture.view) sub_menu.add_a(A(fixture.view, Url('/an/url'), description='sub menu item')) menu.add_dropdown('Dropdown title', sub_menu) [item] = menu.html_representation.children vassert(item.tag_name == 'li') vassert('dropdown' in item.get_attribute('class')) [toggle, added_sub_menu] = item.children vassert('dropdown-toggle' in toggle.get_attribute('class')) vassert('dropdown' in toggle.get_attribute('data-toggle')) vassert('-' in toggle.get_attribute('data-target')) vassert('caret' in toggle.children[1].get_attribute('class')) title_text = toggle.children[0].value vassert(title_text == 'Dropdown title') vassert(added_sub_menu is sub_menu) vassert('dropdown-menu' in added_sub_menu.html_representation.get_attribute('class').split()) vassert(isinstance(added_sub_menu.html_representation, Div)) [dropdown_item] = added_sub_menu.html_representation.children vassert(isinstance(dropdown_item, A)) vassert('dropdown-item' in dropdown_item.get_attribute('class').split())
def set_brand_text(self, brand_text): """Sets the brand to be a link to the home page that contains the given text. :param brand_text: Text to use for branding. """ brand_a = A(self.view, Url('/'), description=brand_text) self.set_brand(brand_a)
def test_button_layouts_on_anchors(web_fixture): """A ButtonLayout can also be used to make an A (anchor) look like a button.""" anchor = A(web_fixture.view, href=Url('/an/href'), description='link text').use_layout(ButtonLayout()) tester = WidgetTester(anchor) [rendered_anchor] = tester.xpath(XPath.link().with_text('link text')) assert rendered_anchor.attrib['class'] == 'btn btn-secondary' assert 'aria-disabled' not in rendered_anchor.attrib assert 'tabindex' not in rendered_anchor.attrib anchor = A(web_fixture.view, href=Url('/an/href'), description='link text', write_check=lambda: False).use_layout(ButtonLayout()) tester = WidgetTester(anchor) [rendered_anchor] = tester.xpath(XPath.link().with_text('link text')) assert rendered_anchor.attrib['class'] == 'btn btn-secondary disabled' assert rendered_anchor.attrib['aria-disabled'] == 'true' assert rendered_anchor.attrib['tabindex'] == '-1'
def new_menu_item_a(self): description = 'The link' href = Url('/link') menu_item_a = A(self.web_fixture.view, self.href, description=description) return menu_item_a
def button_layouts_on_anchors(fixture): """A ButtonLayout can also be used to make an A (anchor) look like a button.""" anchor = A(fixture.view, href=Url('/an/href'), description='link text').use_layout(ButtonLayout()) tester = WidgetTester(anchor) [rendered_anchor] = tester.xpath(XPath.link_with_text('link text')) vassert(rendered_anchor.attrib['class'] == 'btn')
def disabled(self): """The mouse cursor is shown as no-access on disabled items.""" def not_allowed(): return False self.menu_item_with_state = A(self.view, Url('/another_url'), write_check=not_allowed) self.state_indicator_class = 'disabled'
def __init__(self, view): super().__init__(view) error_widget = self.body.insert_child(0, ErrorWidget(view)) error_widget.add_child(H(view, 1, text='Oops, something broke')) error_widget.add_child(P(view, text=error_widget.error_message)) error_widget.add_child( A(view, Url(error_widget.error_source_href), description='Click here to try again'))
def test_button_layouts_on_disabled_anchors(web_fixture): """Disabled A's are marked with a class so Bootstrap can style them appropriately.""" def can_write(): return False anchor = A(web_fixture.view, href=Url('/an/href'), description='link text', write_check=can_write) anchor.use_layout(ButtonLayout()) tester = WidgetTester(anchor) [rendered_anchor] = tester.xpath(XPath.link().with_text('link text')) assert rendered_anchor.attrib['class'] == 'btn btn-secondary disabled'
def __init__(self, view): super().__init__(view) self.add_child( Alert(view, 'This is an alert in danger color', severity='danger')) self.add_child( Alert(view, 'This is an alert in primary color', severity='primary')) self.add_child(A(view, Url('#'), description='Link styled as button')).use_layout( ButtonLayout(style='primary'))
def test_visual_feedback_on_items(web_fixture, visual_feedback_scenarios): """The state of a MenuItem is visually indicated to a user.""" menu = Nav(web_fixture.view) menu.add_a(A(web_fixture.view, Url('/an_url'))) menu.add_a(visual_feedback_scenarios.menu_item_with_state) [defaulted_item, item_with_state] = menu.html_representation.children [defaulted_a] = defaulted_item.children [a_with_state] = item_with_state.children assert visual_feedback_scenarios.state_indicator_class not in defaulted_a.get_attribute('class') assert visual_feedback_scenarios.state_indicator_class in a_with_state.get_attribute('class')
def add_control(self, previous=False): control_a = self.carousel_panel.add_child(A(self.view, self.url)) control_a.append_class('carousel-control-prev' if previous else 'carousel-control-next') control_a.set_attribute('role', 'button') control_a.set_attribute('data-slide', 'prev' if previous else 'next') span_icon = control_a.add_child(Span(self.view)) span_icon.append_class('carousel-control-%s-icon' % ('prev' if previous else 'next')) span_icon.set_attribute('aria-hidden', 'true') span_text = control_a.add_child(Span(self.view, text=_('Previous') if previous else _('Next'))) span_text.append_class('sr-only') return control_a
def test_rendering_active_menu_items(web_fixture, menu_item_scenarios): """A MenuItem is marked as active based on its active_regex or the A it represents.""" description = 'The link' href = Url('/link') menu = Nav(web_fixture.view) menu_item_a = A(web_fixture.view, href, description=description) menu.add_a(menu_item_a, active_regex=menu_item_scenarios.active_regex) tester = WidgetTester(menu) actual = tester.get_html_for('//li') active_str = '' if not menu_item_scenarios.active else 'active ' expected_menu_item_html = '<li class="nav-item"><a href="/link" class="%snav-link">The link</a></li>' % (active_str) assert actual == expected_menu_item_html
def active(self): """The currently active item is highlighted.""" current_url = Url(self.web_fixture.request.url) self.menu_item_with_state = A(self.web_fixture.view, current_url) self.state_indicator_class = 'active'