def test_out_of_bound_widgets(web_fixture): """When you need to add a widget to the page, but not as a child/descendant.""" class MyPanel(Div): def __init__(self, view): super().__init__(view, css_id='main_panel') child_widget = self.add_child(P(view, text='Child Widget')) out_of_bound_widget = view.add_out_of_bound_widget( P(view, text='Out Of Bound Widget')) class MainUI(UserInterface): def assemble(self): self.define_page(HTML5Page).use_layout(BasicPageLayout()) home = self.define_view('/', title='Hello') home.set_slot('main', MyPanel.factory()) fixture = web_fixture wsgi_app = fixture.new_wsgi_app(site_root=MainUI) browser = Browser(wsgi_app) browser.open('/') main_panel = XPath.div().with_id('main_panel') assert browser.is_element_present( XPath.paragraph().with_text('Out Of Bound Widget')) assert not browser.is_element_present(XPath.paragraph().with_text( 'Out Of Bound Widget').inside_of(main_panel)) assert browser.is_element_present( XPath.paragraph().with_text('Child Widget').inside_of(main_panel))
def test_views_with_parameters(web_fixture, parameterised_scenarios): """Views can have arguments that originate from code, or are parsed from the URL.""" class UIWithParameterisedViews(UserInterface): def assemble(self): self.define_view('/aview', view_class=fixture.ParameterisedView, some_arg=fixture.argument) class MainUI(UserInterface): def assemble(self): self.define_page(HTML5Page).use_layout(BasicPageLayout()) self.define_user_interface('/a_ui', UIWithParameterisedViews, {'main': 'main'}, name='myui') fixture = parameterised_scenarios wsgi_app = web_fixture.new_wsgi_app(site_root=MainUI) browser = Browser(wsgi_app) if fixture.should_exist: browser.open(fixture.url) assert browser.title == 'View for: %s' % fixture.expected_value assert browser.is_element_present(XPath.paragraph().including_text( 'content for %s' % fixture.expected_value)) else: browser.open(fixture.url, status=404)
def test_pageflow1(web_fixture, pageflow1_scenario): browser = Browser(pageflow1_scenario.wsgi_app) browser.open('/') assert browser.is_element_present('//ul[contains(@class,"nav")]') browser.click(XPath.link().with_text('Add')) assert browser.current_url.path == '/add' browser.type(XPath.input_labelled('Name'), 'John') browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.click(XPath.button_labelled('Save')) assert browser.current_url.path == '/' assert browser.is_element_present(XPath.paragraph().including_text('John: [email protected]'))
def test_addressbook1(web_fixture, addressbook1_scenario): john = addressbook1.Address(name='John', email_address='*****@*****.**') john.save() browser = Browser(addressbook1_scenario.wsgi_app) browser.open('/') assert browser.is_element_present(XPath.paragraph().including_text('John: [email protected]'))
def test_addressbook2(web_fixture, addressbook2_scenario): browser = Browser(addressbook2_scenario.wsgi_app) browser.open('/') browser.type(XPath.input_labelled('Name'), 'John') browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.click(XPath.button_labelled('Save')) assert browser.is_element_present(XPath.paragraph().including_text('John: [email protected]'))
def test_language_menu(web_fixture): """A Nav can also be constructed to let a user choose to view the same page in another of the supported languages.""" class PanelWithMenu(Div): def __init__(self, view): super().__init__(view) self.add_child(Menu(view).with_languages()) self.add_child(P(view, text=_('This is an English sentence.'))) wsgi_app = web_fixture.new_wsgi_app(child_factory=PanelWithMenu.factory()) browser = Browser(wsgi_app) browser.open('/') assert browser.is_element_present(XPath.paragraph().including_text('This is an English sentence.')) browser.click(XPath.link().with_text('Afrikaans')) assert browser.is_element_present(XPath.paragraph().including_text('Hierdie is \'n sin in Afrikaans.')) browser.click(XPath.link().with_text('English (United Kingdom)')) assert browser.is_element_present(XPath.paragraph().including_text('This is an English sentence.'))
def test_domain_exception(web_fixture, session_scope_fixture): """Typing the wrong password results in an error message being shown to the user.""" browser = Browser(web_fixture.new_wsgi_app(site_root=SessionScopeUI)) user = session_scope_fixture.user browser.open('/') browser.click(XPath.link().with_text('Log in')) browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.type(XPath.input_labelled('Password'), 'wrong password') browser.click(XPath.button_labelled('Log in')) assert browser.is_element_present( XPath.div().including_text('The email/password given do not match'))
def test_logging_in(web_fixture, session_scope_fixture): """A user can log in by going to the Log in page. The name of the currently logged in user is displayed on the home page.""" browser = Browser(web_fixture.new_wsgi_app(site_root=SessionScopeUI)) user = session_scope_fixture.user browser.open('/') browser.click(XPath.link().with_text('Log in')) browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.type(XPath.input_labelled('Password'), 'topsecret') browser.click(XPath.button_labelled('Log in')) browser.click(XPath.link().with_text('Home')) assert browser.is_element_present( XPath.paragraph().including_text('Welcome John Doe'))
def test_parameterised1(web_fixture, parameterised1_scenario): browser = Browser(parameterised1_scenario.wsgi_app) browser.open('/') browser.click(XPath.link().with_text('Add')) browser.type(XPath.input_labelled('Name'), 'John') browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.click(XPath.button_labelled('Save')) assert browser.current_url.path == '/' browser.click(XPath.link().with_text('edit')) john = Session.query(parameterised1.Address).one() assert browser.current_url.path == '/edit/%s' % john.id browser.type(XPath.input_labelled('Name'), 'Johnny') browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.click(XPath.button_labelled('Update')) assert browser.current_url.path == '/' assert browser.is_element_present(XPath.paragraph().including_text('Johnny: [email protected]'))
def test_adding_an_address(web_fixture): """To add a new address, a user clicks on "Add Address" link on the menu, then supplies the information for the new address and clicks the Save button. Upon successful addition of the address, the user is returned to the home page where the new address is now listed.""" browser = Browser(web_fixture.new_wsgi_app(site_root=AddressBookUI)) browser.open('/') browser.click(XPath.link().with_text('Add')) actual_title = browser.title assert actual_title == 'Add', 'Expected to be on the Add an address page' browser.type(XPath.input_labelled('Name'), 'John Doe') browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.click(XPath.button_labelled('Save')) actual_title = browser.title assert actual_title == 'Show', 'Expected to be back on the home page after editing' assert browser.is_element_present(XPath.paragraph().including_text('John Doe: [email protected]')), \ 'Expected the newly added address to be listed on the home page'
def test_form_preserves_user_input_after_validation_exceptions_multichoice( web_fixture): """When a form is submitted and validation fails on the server, the user is presented with the values that were originally entered (even if they were invalid).""" fixture = web_fixture class ModelObject: @exposed def events(self, events): events.an_event = Event(label='click me') @exposed def fields(self, fields): choices = [ Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three')) ] fields.no_validation_exception_field = MultiChoiceField( choices, label='Make your invalid choice', default=[]) fields.validation_exception_field = MultiChoiceField( choices, label='Make your choice', default=[], required=True) model_object = ModelObject() class MyForm(Form): def __init__(self, view): super().__init__(view, 'my_form') self.define_event_handler(model_object.events.an_event) self.add_child(ButtonInput(self, model_object.events.an_event)) select_input = self.add_child( SelectInput(self, model_object.fields.no_validation_exception_field)) if select_input.validation_error: self.add_child(self.create_error_label(select_input)) select_input = self.add_child( SelectInput(self, model_object.fields.validation_exception_field)) if select_input.validation_error: self.add_child(self.create_error_label(select_input)) wsgi_app = web_fixture.new_wsgi_app(child_factory=MyForm.factory()) browser = Browser(wsgi_app) browser.open('/') no_validation_exception_input = '//select[@name="my_form-no_validation_exception_field[]"]' validation_exception_input = '//select[@name="my_form-validation_exception_field[]"]' browser.select_many(no_validation_exception_input, ['One', 'Two']) browser.select_none(validation_exception_input ) # select none to trigger the RequiredConstraint browser.click(XPath.button_labelled('click me')) assert browser.get_value(no_validation_exception_input) == ['1', '2'] assert not browser.get_value(validation_exception_input) label = browser.get_html_for('//label') input_id = browser.get_id_of(validation_exception_input) assert label == '<label for="%s" class="error">Make your choice is required</label>' % input_id #2. Submit again ths time not expecting validation exceptions, also expecting the validation error to be cleared and the domain should have all input browser.select_many(validation_exception_input, ['Two', 'Three']) browser.click(XPath.button_labelled('click me')) assert not browser.is_element_present('//label[@class="error"]') assert browser.get_value(no_validation_exception_input) == ['1', '2'] assert browser.get_value(validation_exception_input) == ['2', '3']