def detour_to_login(self, fixture): browser = Browser(fixture.wsgi_app) browser.open('/inbox/') vassert(browser.location_path == '/accounts/login') browser.type('//input[@name="email"]', fixture.system_account.email) browser.type('//input[@name="password"]', fixture.system_account.password) browser.click('//input[@value="Log in"]') vassert(browser.location_path == '/inbox/')
def test_addressbook2(fixture): browser = Browser(fixture.wsgi_app) browser.open('/') browser.type(XPath.input_labelled('Name'), 'John') browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.click(XPath.button_labelled('Save')) vassert( browser.is_element_present( XPath.paragraph_containing('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_detour_to_login(web_fixture, party_account_fixture, workflow_web_fixture): fixture = workflow_web_fixture browser = Browser(fixture.wsgi_app) browser.open('/inbox/') assert browser.current_url.path == '/accounts/login' browser.type('//input[@name="login_form-email"]', party_account_fixture.system_account.email) browser.type('//input[@name="login_form-password"]', party_account_fixture.system_account.password) browser.click('//input[@value="Log in"]') assert browser.current_url.path == '/inbox/'
def test_detour_to_login(web_fixture, party_account_fixture, workflow_web_fixture): fixture = workflow_web_fixture browser = Browser(fixture.wsgi_app) browser.open('/inbox/') assert browser.current_url.path == '/accounts/login' browser.type(XPath.input_labelled('Email'), party_account_fixture.system_account.email) browser.type(XPath.input_labelled('Password'), party_account_fixture.system_account.password) browser.click(XPath.button_labelled('Log in')) assert browser.current_url.path == '/inbox/'
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_pageflow2(fixture): browser = Browser(fixture.wsgi_app) browser.open('/') vassert(browser.is_element_present('//ul[contains(@class,"nav")]')) browser.click(XPath.link_with_text('Add an address')) vassert(browser.location_path == '/add') browser.type(XPath.input_labelled('Name'), 'John') browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.click(XPath.button_labelled('Save')) vassert(browser.location_path == '/') vassert( browser.is_element_present( XPath.paragraph_containing('John: [email protected]')))
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_email_retained(web_fixture, session_scope_fixture): """The email address used when last logged in is always pre-populated on the Log in 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')) # Go away from the page, then back browser.click(XPath.link().with_text('Home')) browser.click(XPath.link().with_text('Log in')) # .. then the email is still pre-populated typed_value = browser.get_value(XPath.input_labelled('Email')) assert typed_value == '*****@*****.**'
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_parameterised1(fixture): browser = Browser(fixture.wsgi_app) browser.open('/') browser.click(XPath.link_with_text('Add an address')) browser.type(XPath.input_labelled('Name'), 'John') browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.click(XPath.button_labelled('Save')) vassert(browser.location_path == '/') browser.click(XPath.link_with_text('edit')) john = Session.query(parameterised1bootstrap.Address).one() vassert(browser.location_path == '/edit/%s' % john.id) browser.type(XPath.input_labelled('Name'), 'Johnny') browser.type(XPath.input_labelled('Email'), '*****@*****.**') browser.click(XPath.button_labelled('Update')) vassert(browser.location_path == '/') vassert( browser.is_element_present( XPath.paragraph_containing('Johnny: [email protected]')))
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_form_input_validation(web_fixture): """Validation of input happens in JS on the client, but also on the server if JS is bypassed.""" error_xpath = '//form[contains(@class, "reahl-form")]/label[contains(@class, "error")]' fixture = web_fixture class ModelObject(object): def handle_event(self): pass @exposed def events(self, events): events.an_event = Event(label='click me', action=Action(self.handle_event)) @exposed def fields(self, fields): fields.field_name = EmailField() model_object = ModelObject() class MyForm(Form): def __init__(self, view, name, other_view): super(MyForm, self).__init__(view, name) if self.exception: self.add_child( P(view, ','.join(self.exception.detail_messages))) self.define_event_handler(model_object.events.an_event, target=other_view) self.add_child(ButtonInput(self, model_object.events.an_event)) text_input = self.add_child( TextInput(self, model_object.fields.field_name)) if text_input.validation_error: self.add_child(self.create_error_label(text_input)) class MainUI(UserInterface): def assemble(self): self.define_page(HTML5Page).use_layout(BasicPageLayout()) home = self.define_view('/', title='Home page') other_view = self.define_view('/page2', title='Page 2') home.set_slot('main', MyForm.factory('myform', other_view)) wsgi_app = fixture.new_wsgi_app(site_root=MainUI, enable_js=True) # Case: form validation fails in JS on the client # - Form submission is blocked # - Error message is displayed fixture.reahl_server.set_app(wsgi_app) fixture.driver_browser.open('/') fixture.driver_browser.wait_for_element_not_visible(error_xpath) fixture.driver_browser.type('//input[@type="text"]', 'not@notvalid', trigger_blur=False, wait_for_ajax=False) fixture.driver_browser.press_tab() fixture.driver_browser.wait_for_element_visible(error_xpath) with fixture.driver_browser.no_page_load_expected(): fixture.driver_browser.click("//input[@value='click me']") error_text = fixture.driver_browser.get_text(error_xpath) assert error_text == 'field_name should be a valid email address' # .. but the error is removed again upon valid input fixture.driver_browser.type('//input[@type="text"]', '*****@*****.**') fixture.driver_browser.wait_for_element_not_visible(error_xpath) # Case: form validation fails on the server (assuming no JS on the client to block submission) # - ValidationException is raised (which is dealt with as any DomainException) browser = Browser(wsgi_app) browser.open('/') browser.type('//input[@type="text"]', 'not@notvalid') browser.click('//input[@type="submit"]') assert not hasattr(model_object, 'field_name') label = browser.get_html_for('//label') input_id = browser.get_id_of('//input[@type="text"]') assert label == '<label for="%s" class="error">field_name should be a valid email address</label>' % input_id assert Session.query(UserInput).filter_by(key='myform-field_name').count( ) == 1 # The invalid input was persisted exception = Session.query(PersistedException).one().exception assert isinstance(exception, ValidationException) # Is was persisted assert not exception.commit # Case: form validation passes (no-js) # - no ValidationException # - all input is translated to python and set as values on the model objects # - any saved input on the form is cleared browser.type('//input[@type="text"]', '*****@*****.**') browser.click("//input[@value='click me']") assert model_object.field_name == '*****@*****.**' assert Session.query(UserInput).filter_by( key='myform-field_name').count() == 0 # The invalid input was removed assert Session.query( PersistedException).count() == 0 # The exception was removed assert browser.current_url.path == '/page2' # Case: form validation passes (js) # - no ValidationException # - all input is translated to python and set as values on the model objects fixture.driver_browser.open('/') fixture.driver_browser.type('//input[@type="text"]', '*****@*****.**') fixture.driver_browser.wait_for_element_not_visible(error_xpath) fixture.driver_browser.click("//input[@value='click me']") assert model_object.field_name == '*****@*****.**' assert fixture.driver_browser.current_url.path == '/page2'
def test_exception_handling(reahl_system_fixture, web_fixture, sql_alchemy_fixture): """When a DomainException happens during the handling of an Event: The database is rolled back. The browser is redirected to GET the original view again (not the target). The screen still displays the values the user initially typed, not those on the ModelObject. """ fixture = web_fixture class ModelObject(Base): __tablename__ = 'test_event_handling_exception_handling' id = Column(Integer, primary_key=True) field_name = Column(Integer) def handle_event(self): self.field_name = 1 raise DomainException() @exposed def events(self, events): events.an_event = Event(label='click me', action=Action(self.handle_event)) @exposed def fields(self, fields): fields.field_name = IntegerField(default=3) with sql_alchemy_fixture.persistent_test_classes(ModelObject): model_object = ModelObject() Session.add(model_object) class MyForm(Form): def __init__(self, view, name, other_view): super(MyForm, self).__init__(view, name) self.define_event_handler(model_object.events.an_event, target=other_view) self.add_child(ButtonInput(self, model_object.events.an_event)) self.add_child(TextInput(self, model_object.fields.field_name)) class MainUI(UserInterface): def assemble(self): self.define_page(HTML5Page).use_layout(BasicPageLayout()) home = self.define_view('/', title='Home page') other_view = self.define_view('/page2', title='Page 2') home.set_slot('main', MyForm.factory('myform', other_view)) wsgi_app = fixture.new_wsgi_app(site_root=MainUI) browser = Browser( wsgi_app ) # Dont use a real browser, because it will also hit many other URLs for js and css which confuse the issue browser.open('/') assert not model_object.field_name browser.type("//input[@type='text']", '5') # any database stuff that happened when the form was submitted was rolled back # with CallMonitor(reahl_system_fixture.system_control.orm_control.rollback) as monitor: # browser.click(XPath.button_labelled('click me')) # assert monitor.times_called == 1 browser.click(XPath.button_labelled('click me')) # the value input by the user is still displayed on the form, NOT the actual value on the model object assert not model_object.field_name retained_value = browser.get_value("//input[@type='text']") assert retained_value == '5' # the browser is still on the page with the form which triggered the exception assert browser.current_url.path == '/'