def test_pattern_constraint_js(web_fixture, constraint_rendering_fixture): fixture = constraint_rendering_fixture allow_pattern = '(ab)+' constraint = PatternConstraint(pattern=allow_pattern) class MyForm(Form): def __init__(self, view, name): super().__init__(view, name) field = fixture.model_object.fields.an_attribute.with_validation_constraint( constraint) self.add_child(TextInput(self, field)) wsgi_app = web_fixture.new_wsgi_app(child_factory=MyForm.factory('myform'), enable_js=True) web_fixture.reahl_server.set_app(wsgi_app) web_fixture.driver_browser.open('/') web_fixture.driver_browser.type('//input[@type="text"]', 'aba', trigger_blur=False, wait_for_ajax=False) web_fixture.driver_browser.press_tab() web_fixture.driver_browser.wait_for_element_visible(fixture.error_xpath) web_fixture.driver_browser.type('//input[@type="text"]', 'ababab', trigger_blur=False, wait_for_ajax=False) web_fixture.driver_browser.press_tab() web_fixture.driver_browser.wait_for_element_not_visible( fixture.error_xpath)
def pattern_constraint_js(self, fixture): allow_pattern = '(ab)+' constraint = PatternConstraint(pattern=allow_pattern) class MyForm(Form): def __init__(self, view, name): super(MyForm, self).__init__(view, name) field = fixture.model_object.fields.an_attribute field.add_validation_constraint(constraint) self.add_child(TextInput(self, field)) wsgi_app = fixture.new_wsgi_app(child_factory=MyForm.factory('myform'), enable_js=True) fixture.reahl_server.set_app(wsgi_app) fixture.driver_browser.open('/') fixture.driver_browser.type('//input[@type="text"]', 'aba') fixture.driver_browser.press_tab('//input') fixture.driver_browser.wait_for_element_visible(fixture.error_xpath) fixture.driver_browser.type('//input[@type="text"]', 'ababab') fixture.driver_browser.press_tab('//input') fixture.driver_browser.wait_for_element_not_visible( fixture.error_xpath)
def pattern_constraint(self, fixture): allow_pattern = '(ab)+' pattern_constraint = PatternConstraint(pattern=allow_pattern) #pattern is returned as parameter vassert( pattern_constraint.parameters == allow_pattern ) #case: valid valid_input = 'ababab' with expected(NoException): pattern_constraint.validate_input(valid_input ) #case invalid with expected(PatternConstraint): pattern_constraint.validate_input('aba') #case: faulty pattern faulty_pattern_constraint = PatternConstraint(pattern='??????') with expected(ProgrammerError): faulty_pattern_constraint.validate_input(valid_input)
def fields(self, fields): fields.some_field = Field( label='Some field', default='not set').with_validation_constraint( PatternConstraint('((?!invalidinput).)*'))