def test_default_behaviour(web_fixture, popup_a_fixture): """If you click on the A, a popupwindow opens with its contents the specified element on the target page.""" class PopupTestPanel(Div): def __init__(self, view): super().__init__(view) self.add_child(PopupA(view, view.as_bookmark(), '#contents')) popup_contents = self.add_child( P(view, text='this is the content of the popup')) popup_contents.set_id('contents') wsgi_app = web_fixture.new_wsgi_app(enable_js=True, child_factory=PopupTestPanel.factory()) web_fixture.reahl_server.set_app(wsgi_app) browser = web_fixture.driver_browser browser.open('/') # The A is rendered correctly assert browser.is_element_present( "//a[@title='Home page' and text()='Home page' and @href='/']") # subsequent behaviour browser.click(XPath.link().with_text('Home page')) browser.wait_for(popup_a_fixture.is_popped_up) #check some bootstrap attributes dialog_xpath = "//div[@class='modal fade show' and @tabindex='-1']/div[@class='modal-dialog']/div[@class='modal-content']" assert browser.is_element_present(dialog_xpath) browser.click(XPath.button_labelled('Close')) browser.wait_for_not(popup_a_fixture.is_popped_up)
def test_customising_dialog_buttons(web_fixture, popup_a_fixture): """The buttons of the dialog can be customised.""" class PopupTestPanel(Div): def __init__(self, view): super().__init__(view) popup_a = self.add_child( PopupA(view, view.as_bookmark(), '#contents')) popup_a.add_js_button('Butt1') popup_a.add_js_button('Butt2') popup_contents = self.add_child( P(view, text='this is the content of the popup')) popup_contents.set_id('contents') wsgi_app = web_fixture.new_wsgi_app(enable_js=True, child_factory=PopupTestPanel.factory()) web_fixture.reahl_server.set_app(wsgi_app) browser = web_fixture.driver_browser button1_xpath = XPath.button_labelled('Butt1') button2_xpath = XPath.button_labelled('Butt2') browser.open('/') browser.click(XPath.link().with_text('Home page')) browser.wait_for(popup_a_fixture.is_popped_up) assert browser.is_element_present(button1_xpath) assert browser.is_element_present(button2_xpath)
def test_workings_of_check_checkbox_button(web_fixture, popup_a_fixture): """A CheckCheckBoxButton checks the checkbox on the original page when clicked.""" class PopupTestPanel(Div): @exposed def fields(self, fields): fields.field = BooleanField(label='a checkbox') def __init__(self, view): super().__init__(view) popup_a = self.add_child( PopupA(view, view.as_bookmark(), '#contents')) popup_contents = self.add_child( P(view, text='this is the content of the popup')) popup_contents.set_id('contents') form = self.add_child(Form(view, 'aform')).use_layout(FormLayout()) checkbox = form.layout.add_input( CheckboxInput(form, self.fields.field)) popup_a.add_js_button('Checkit', CheckCheckboxScript(checkbox)) wsgi_app = web_fixture.new_wsgi_app(enable_js=True, child_factory=PopupTestPanel.factory()) web_fixture.reahl_server.set_app(wsgi_app) browser = web_fixture.driver_browser browser.open('/') browser.click(XPath.link().with_text('Home page')) browser.wait_for(popup_a_fixture.is_popped_up) browser.click(XPath.button_labelled('Checkit')) browser.wait_for_not(popup_a_fixture.is_popped_up) assert browser.is_selected(XPath.input_labelled('a checkbox'))
def test_example(web_fixture, dynamic_example_fixture): fixture = dynamic_example_fixture wsgi_application = web_fixture.new_wsgi_app(site_root=DynamicUI, enable_js=True) web_fixture.reahl_server.set_app(wsgi_application) browser = fixture.browser browser.open('/') # Check calculating totals browser.type(fixture.percentage_input_for('Fund A'), '80') browser.wait_for(fixture.percentage_total_is, '80') browser.type(fixture.percentage_input_for('Fund B'), '80') browser.wait_for(fixture.percentage_total_is, '160') # Check DomainException upon submit with incorrect totals browser.click(XPath.button_labelled('Submit')) browser.wait_for_element_visible(fixture.domain_exception_alert) browser.type(fixture.percentage_input_for('Fund B'), '20') browser.wait_for(fixture.percentage_total_is, '100') # Check successful submit browser.click(XPath.button_labelled('Submit')) browser.wait_for_element_not_visible(fixture.domain_exception_alert)
def test_async_validation(web_fixture, per_file_constrained_file_upload_input_fixture): """Validations are checked in JavaScript before uploading. """ # Only tested for the FileUploadInput, as it uses the FileInput # in its own implementation, in a NestedForm, and has to pass on the # filesize constraint all the way. This way, we test all of that. fixture = per_file_constrained_file_upload_input_fixture web_fixture.reahl_server.set_app(fixture.new_wsgi_app(enable_js=True)) browser = web_fixture.driver_browser browser.open('/') assert not fixture.uploaded_file_is_listed(fixture.valid_file.name) assert not fixture.uploaded_file_is_listed(fixture.invalid_file.name) browser.type(XPath.input_labelled('Choose file(s)'), fixture.invalid_file.name) assert not fixture.uploaded_file_is_listed(fixture.invalid_file.name) assert browser.is_element_present(XPath.span().including_text( fixture.validation_error_message)) browser.type(XPath.input_labelled('Choose file(s)'), fixture.valid_file.name) assert fixture.uploaded_file_is_listed(fixture.valid_file.name)
def test_queueing_async_uploads(web_fixture, large_file_upload_input_fixture): """Asynchronous uploads do not happen concurrently, they are queued one after another. """ fixture = large_file_upload_input_fixture fixture.run_hook_after = True web_fixture.reahl_server.set_app(fixture.new_wsgi_app(enable_js=True)) browser = web_fixture.driver_browser browser.open('/') assert not fixture.file_was_uploaded(fixture.file_to_upload1.name) assert not fixture.uploaded_file_is_listed(fixture.file_to_upload1.name) with web_fixture.reahl_server.in_background(wait_till_done_serving=False): browser.type(XPath.input_labelled('Choose file(s)'), fixture.file_to_upload1.name, wait_for_ajax=False) # Upload will block, see fixture browser.type(XPath.input_labelled('Choose file(s)'), fixture.file_to_upload2.name, wait_for_ajax=False) # Upload will block, see fixture progress1 = browser.get_attribute('//ul/li[1]/progress', 'value') assert progress1 == '100' progress2 = browser.get_attribute('//ul/li[2]/progress', 'value') assert progress2 == '0' fixture.simulate_large_file_upload_done() browser.wait_for(fixture.uploaded_file_is_listed, fixture.file_to_upload2.name) assert fixture.uploaded_file_is_listed(fixture.file_to_upload1.name) assert fixture.uploaded_file_is_listed(fixture.file_to_upload2.name) assert fixture.file_was_uploaded(fixture.file_to_upload1.name) assert fixture.file_was_uploaded(fixture.file_to_upload2.name)
def test_table_totals(web_fixture, table_fixture, scenario): """You can pass footer_items to with_data to add a footer with a row for each footer_item. Each column with footer content defined will have that content rendered in its footer row, columns inbetween will be collapsed with a colspan to ensure alignment. """ class MainWidget(Div): def __init__(self, view): super().__init__(view) table = Table(view) table.with_data([ StaticColumn(Field(label='Another Row Number'), 'row_another'), StaticColumn(Field(label='Row Number'), 'row'), scenario.total_column ], table_fixture.data, footer_items=[EmptyStub(total=123)]) self.add_child(table) fixture = table_fixture wsgi_app = web_fixture.new_wsgi_app(enable_js=True, child_factory=MainWidget.factory()) web_fixture.reahl_server.set_app(wsgi_app) browser = web_fixture.driver_browser browser.open('/') filler_cell = XPath.table_cell()[1].inside_of(XPath.table_footer()) assert browser.get_attribute(filler_cell, 'colspan') == '2' total_cell = XPath.table_cell()[2].inside_of(XPath.table_footer()) assert browser.get_text(total_cell) == scenario.expected_total assert not browser.get_attribute(total_cell, 'colspan')
def test_clear_form_inputs_on_optimistic_concurrency(web_fixture, sql_alchemy_fixture, concurrency_fixture, scenario): """A concurrency error is detected upon submit after an exception. When a user resets inputs upon such a concurrency error, previous form exceptions and input data are cleared. """ fixture = concurrency_fixture with sql_alchemy_fixture.persistent_test_classes(fixture.ModelObject): Session.add(fixture.model_object) wsgi_app = web_fixture.new_wsgi_app( child_factory=fixture.MyForm.factory()) web_fixture.reahl_server.set_app(wsgi_app) browser = web_fixture.driver_browser browser.open('/') # Concurrency error is detected on submit after an exception scenario.cause_exception_on_submit(browser) assert fixture.is_any_error_displayed() assert not fixture.is_concurrency_error_displayed() fixture.make_concurrent_change_in_backend() browser.type(XPath.input_labelled('Some field'), 'valid input') browser.click(XPath.button_labelled('Submit')) assert fixture.is_concurrency_error_displayed() # Previous error and inputs are cleared browser.click(XPath.button_labelled('Reset input')) assert browser.get_value( XPath.input_labelled('Some field')) == 'changed by someone else' assert not fixture.is_any_error_displayed()
def test_prevent_duplicate_upload_js(web_fixture, file_upload_input_fixture): """The user is prevented from uploading more than one file with the same name on the client side. """ fixture = file_upload_input_fixture web_fixture.reahl_server.set_app(fixture.new_wsgi_app(enable_js=True)) browser = web_fixture.driver_browser error_locator = XPath.span().including_text( 'uploaded files should all have different names') def error_is_visible(): return browser.is_visible(error_locator) browser.open('/') browser.type(XPath.input_labelled('Choose file(s)'), fixture.file_to_upload1.name) browser.wait_for_not(error_is_visible) browser.type(XPath.input_labelled('Choose file(s)'), fixture.file_to_upload2.name) browser.wait_for_not(error_is_visible) with web_fixture.reahl_server.paused(): browser.type(XPath.input_labelled('Choose file(s)'), fixture.file_to_upload1.name) assert not fixture.upload_file_is_queued(fixture.file_to_upload1.name) browser.wait_for(error_is_visible) browser.click( XPath.button_labelled('Remove', filename=fixture.file_to_upload2_name)) browser.wait_for_not(error_is_visible)
def test_checkbox_basics_with_boolean_field(web_fixture, checkbox_fixture): """CheckboxInput can be used to toggle a single yes/no answer, in which case it renders a single checkbox.""" web_fixture.reahl_server.set_app( web_fixture.new_wsgi_app( child_factory=checkbox_fixture.Form.factory())) browser = web_fixture.driver_browser browser.open('/') checkbox = XPath.input_labelled('Subscribe to newsletter?') assert not browser.is_selected(checkbox) assert browser.is_element_present( XPath.label().with_text('Subscribe to newsletter?')) assert browser.is_element_present( '//div[@class="reahl-checkbox-input reahl-primitiveinput"]/div/input/following-sibling::label' ) assert browser.get_attribute('//label', 'class') == 'custom-control-label' checkbox_classes = browser.get_attribute( '//div/input[@type="checkbox"]/..', 'class').split(' ') assert 'custom-control' in checkbox_classes assert 'custom-checkbox' in checkbox_classes assert browser.get_attribute('//div/input[@type="checkbox"]', 'class') == 'custom-control-input' browser.set_selected(checkbox) browser.click(XPath.button_labelled('Submit')) assert browser.is_selected(checkbox)
def test_navigating_the_page_numbers(web_fixture, page_menu_fixture): """One can navigate the range of page links displayed by the PageMenu using the special links.""" fixture = page_menu_fixture fixture.number_of_pages = 30 fixture.max_page_links = 5 web_fixture.reahl_server.set_app(page_menu_fixture.wsgi_app) browser = web_fixture.driver_browser browser.open('/') # Case: next link browser.click(XPath.link().with_text_starting('»')) assert browser.wait_for(fixture.page_range_links_match, 'p6,p7,p8,p9,p10') browser.click(XPath.link().with_text_starting('»')) assert browser.wait_for(fixture.page_range_links_match, 'p11,p12,p13,p14,p15') # Case: prev link browser.click(XPath.link().with_text_starting('«')) assert browser.wait_for(fixture.page_range_links_match, 'p6,p7,p8,p9,p10') # Case: last link browser.click(XPath.link().with_text_starting('→')) assert browser.wait_for(fixture.page_range_links_match, 'p26,p27,p28,p29,p30') # Case: first link browser.click(XPath.link().with_text_starting('←')) assert browser.wait_for(fixture.page_range_links_match, 'p1,p2,p3,p4,p5')
def test_file_upload_input_list_files_clearing(web_fixture, file_upload_input_fixture, exception_scenario): """The list of uploaded files displayed by the FileUploadInput is cleared once the Form is successfully submitted.""" fixture = file_upload_input_fixture web_fixture.reahl_server.set_app(fixture.wsgi_app) browser = web_fixture.driver_browser browser.open('/') # Upload one file browser.type(XPath.input_labelled('Choose file(s)'), fixture.file_to_upload1.name) browser.click(XPath.button_labelled('Upload')) assert fixture.uploaded_file_is_listed(fixture.file_to_upload1.name) # Submit the form: browser.click(XPath.button_labelled('Submit')) if fixture.domain_object.throws_exception: assert fixture.uploaded_file_is_listed(fixture.file_to_upload1.name) else: assert not fixture.uploaded_file_is_listed( fixture.file_to_upload1.name)
def test_deleting_several_address(web_fixture, fixture): """To delete several address, a user "checks" the box next to each of the Addresses on the "Addresses" page she wants to delete. Upon clicking the "Delete Selected" Button, the page refreshes, and the remaining addresses appear.""" fixture.create_addresses() #create some data to play with web_fixture.reahl_server.set_app( web_fixture.new_wsgi_app(site_root=AddressBookUI)) fixture.browser = web_fixture.driver_browser fixture.browser.open('/') name_of_address_1 = 'friend 1' name_of_address_13 = 'friend 13' name_of_address_20 = 'friend 20' assert fixture.address_is_listed_as(name_of_address_1) assert fixture.address_is_listed_as(name_of_address_13) assert fixture.address_is_listed_as(name_of_address_20) fixture.browser.click(XPath.checkbox().inside_of( XPath.table_row()[1].inside_of(XPath.table_body()))) fixture.browser.click(XPath.checkbox().inside_of( XPath.table_row()[13].inside_of(XPath.table_body()))) fixture.browser.click(XPath.checkbox().inside_of( XPath.table_row()[20].inside_of(XPath.table_body()))) fixture.browser.click(XPath.button_labelled('Delete Selected')) assert fixture.is_on_home_page() assert not fixture.address_is_listed_as(name_of_address_1) assert not fixture.address_is_listed_as(name_of_address_13) assert not fixture.address_is_listed_as(name_of_address_20)
def test_marshalling_of_checkbox_select_input(web_fixture, checkbox_fixture): """CheckboxSelectInput is used to choose many things from a list.""" fixture = checkbox_fixture choices = [Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two')), Choice(3, IntegerField(label='Three'))] fixture.field = MultiChoiceField(choices) model_object = fixture.model_object model_object.an_attribute = [1] wsgi_app = web_fixture.new_wsgi_app(child_factory=fixture.new_Form(input_widget_class=CheckboxSelectInput).factory('myform')) web_fixture.reahl_server.set_app(wsgi_app) web_fixture.driver_browser.open('/') assert web_fixture.driver_browser.is_selected(XPath.input_labelled('One')) assert not web_fixture.driver_browser.is_selected(XPath.input_labelled('Two')) assert not web_fixture.driver_browser.is_selected(XPath.input_labelled('Three')) # Case: checkbox is submitted with form (ie checked) web_fixture.driver_browser.set_deselected(XPath.input_labelled('One')) web_fixture.driver_browser.set_selected(XPath.input_labelled('Two')) web_fixture.driver_browser.set_selected(XPath.input_labelled('Three')) web_fixture.driver_browser.click(XPath.button_labelled('click me')) assert model_object.an_attribute == [2, 3] assert fixture.checkbox.value == '2,3' assert not web_fixture.driver_browser.is_selected(XPath.input_labelled('One')) assert web_fixture.driver_browser.is_selected(XPath.input_labelled('Two')) assert web_fixture.driver_browser.is_selected(XPath.input_labelled('Three'))
def test_verify_from_menu(web_fixture, party_account_fixture, accounts_web_fixture): fixture = accounts_web_fixture account = party_account_fixture.new_system_account(activated=False) activation_request = VerifyEmailRequest( email=account.email, subject_config='accounts.activation_subject', email_config='accounts.activation_email') Session.add(activation_request) deferred_activation = ActivateAccount(system_account=account, requirements=[activation_request]) Session.add(deferred_activation) secret_key = activation_request.as_secret_key() assert not account.status.is_active() fixture.browser.open('/a_ui/verify') fixture.browser.type(XPath.input_labelled('Email'), account.email) fixture.browser.type(XPath.input_labelled('Secret key'), secret_key) fixture.browser.type(XPath.input_labelled('Password'), account.password) fixture.browser.click(XPath.button_labelled('Verify')) assert fixture.browser.current_url.path == '/a_ui/thanks' assert account.status.is_active()
def test_required_constraint_js(web_fixture, constraint_rendering_fixture): fixture = constraint_rendering_fixture constraint = RequiredConstraint() class MyForm(Form): def __init__(self, view, name): super().__init__(view, name) self.use_layout(FormLayout()) field = fixture.model_object.fields.an_attribute.with_validation_constraint( constraint) self.layout.add_input(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(XPath.input_labelled('an attribute'), 'something', 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) web_fixture.driver_browser.type(XPath.input_labelled('an attribute'), '') web_fixture.driver_browser.press_keys( web_fixture.driver_browser.Keys.BACK_SPACE, locator=XPath.input_labelled( 'an attribute')) # To trigger validation on the field web_fixture.driver_browser.wait_for_element_visible(fixture.error_xpath)
def test_register_help_pending(web_fixture, party_account_fixture, accounts_web_fixture): fixture = accounts_web_fixture verification_requests = Session.query(VerifyEmailRequest) unactivated_account = party_account_fixture.new_system_account( email='*****@*****.**', activated=False) activation_request = VerifyEmailRequest( email=unactivated_account.email, subject_config='accounts.activation_subject', email_config='accounts.activation_email') Session.add(activation_request) deferred_activation = ActivateAccount(system_account=unactivated_account, requirements=[activation_request]) Session.add(deferred_activation) fixture.browser.open('/a_ui/registerHelp') fixture.browser.type(XPath.input_labelled('Email'), unactivated_account.email) fixture.browser.click(XPath.button_labelled('Investigate')) assert fixture.browser.current_url.path == '/a_ui/registerHelp/pending' assert verification_requests.count() == 1 party_account_fixture.mailer.reset() fixture.browser.click(XPath.button_labelled('Send')) assert verification_requests.count() == 1 assert party_account_fixture.mailer.mail_sent assert fixture.browser.current_url.path == '/a_ui/registerHelp/pending/sent'
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_button_layouts(web_fixture): """A ButtonLayout can be be used on a Button to customise various visual effects.""" event = Event(label='click me') event.bind('event', web_fixture) form = Form(web_fixture.view, 'test') form.define_event_handler(event) # Case: the defaults button = Button(form, event) tester = WidgetTester(button) [button] = tester.xpath(XPath.button_labelled('click me')) assert button.attrib['class'] == 'btn btn-secondary reahl-primitiveinput' # Case: possible effects form = Form(web_fixture.view, 'test') form.define_event_handler(event) button = Button(form, event, style='secondary', outline=True, size='sm', active=True, wide=True, text_wrap=False) tester = WidgetTester(button) [button] = tester.xpath(XPath.button_labelled('click me')) assert button.attrib[ 'class'] == 'active btn btn-block btn-outline-secondary btn-sm reahl-primitiveinput text-nowrap'
def test_clicking_on_different_tabs_switch(web_fixture, panel_switch_fixture, tabbed_panel_ajax_fixture): """Clicking on tabs change the contents that are displayed as well as the active tab.""" if not panel_switch_fixture.enable_js: panel_switch_fixture.ensure_disabled_js_files_not_cached() wsgi_app = tabbed_panel_ajax_fixture.new_wsgi_app( enable_js=panel_switch_fixture.enable_js) web_fixture.reahl_server.set_app(wsgi_app) browser = web_fixture.driver_browser browser.open('/') # Clicking on 3 (a normal tab), changes the current tab assert browser.wait_for_not(tabbed_panel_ajax_fixture.tab_is_active, 'tab 3 name') assert browser.wait_for_not(tabbed_panel_ajax_fixture.tab_contents_equals, '<p>tab 3 content</p>') browser.click(XPath.link().with_text('tab 3 name')) assert browser.wait_for(tabbed_panel_ajax_fixture.tab_is_active, 'tab 3 name') assert browser.wait_for(tabbed_panel_ajax_fixture.tab_contents_equals, '<p>tab 3 content</p>') browser.click(XPath.link().with_text('tab 4 name')) assert browser.wait_for_not(tabbed_panel_ajax_fixture.tab_is_active, 'tab 3 name') assert browser.wait_for(tabbed_panel_ajax_fixture.tab_is_active, 'tab 4 name') assert browser.wait_for(tabbed_panel_ajax_fixture.tab_contents_equals, '<p>tab 4 content</p>')
def test_marshalling_of_radio_button_select_input(web_fixture, input_fixture, field_scenario): """When a form is submitted, the value of the selected radio button is persisted.""" fixture = input_fixture choices = [Choice(1, IntegerField(label='One')), Choice(2, IntegerField(label='Two'))] fixture.field = field_scenario.field_class(choices) model_object = fixture.model_object model_object.an_attribute = field_scenario.default_domain_value wsgi_app = web_fixture.new_wsgi_app(child_factory=fixture.new_Form(input_widget_class=RadioButtonSelectInput).factory('myform')) web_fixture.reahl_server.set_app(wsgi_app) web_fixture.driver_browser.open('/') radio_one = XPath.input_labelled('One') radio_two = XPath.input_labelled('Two') # One is pre selected assert web_fixture.driver_browser.is_selected(radio_one) assert not web_fixture.driver_browser.is_selected(radio_two) # click on Two web_fixture.driver_browser.click(radio_two) assert not web_fixture.driver_browser.is_selected(radio_one) web_fixture.driver_browser.click(XPath.button_labelled('click me')) assert model_object.an_attribute == field_scenario.selected_domain_value
def test_take_and_release_task(web_fixture, task_queue_fixture, workflow_web_fixture): fixture = workflow_web_fixture browser = Browser(fixture.wsgi_app) task = task_queue_fixture.task take_task_button = XPath.button_labelled('Take') defer_task_button = XPath.button_labelled('Defer') release_task_button = XPath.button_labelled('Release') go_to_task_button = XPath.button_labelled('Go to') web_fixture.log_in(browser=browser) browser.open('/inbox/') browser.click(take_task_button) assert browser.current_url.path == '/inbox/task/%s' % task.id browser.click(defer_task_button) assert browser.current_url.path == '/inbox/' browser.click(go_to_task_button) assert browser.current_url.path == '/inbox/task/%s' % task.id browser.click(release_task_button) assert browser.current_url.path == '/inbox/'
def test_detour_is_non_reentrant(web_fixture): """Once detoured to a View marked as the start of a Detour, a Bookmark to that View itself will not re-enter the detour. """ class MainUI(UserInterface): def assemble(self): self.define_page(HTML5Page).use_layout(BasicPageLayout()) step1 = self.define_view('/firstStepOfDetour', title='Step 1', detour=True) step1.set_slot('main', A.factory_from_bookmark(step1.as_bookmark(self))) home = self.define_view('/initial', title='View a') home.set_slot('main', A.factory_from_bookmark(step1.as_bookmark(self))) wsgi_app = web_fixture.new_wsgi_app(site_root=MainUI) browser = Browser(wsgi_app) def locationIsSetToReturnTo(url_path): return urllib.parse.parse_qs(browser.current_url.query)['returnTo'] == [url_path] browser.open('/initial') browser.click(XPath.link().with_text('Step 1')) assert browser.current_url.path == '/firstStepOfDetour' assert locationIsSetToReturnTo('http://localhost/initial') browser.click(XPath.link().with_text('Step 1')) assert browser.current_url.path == '/firstStepOfDetour' assert locationIsSetToReturnTo('http://localhost/initial')
def test_file_upload_input_list_files(web_fixture, file_upload_input_fixture): """The FileUploadInput displays a list of files that were uploaded so far.""" fixture = file_upload_input_fixture web_fixture.reahl_server.set_app(fixture.wsgi_app) browser = web_fixture.driver_browser browser.open('/') # Upload one file browser.type(XPath.input_labelled('Choose file(s)'), fixture.file_to_upload1.name) browser.click(XPath.button_labelled('Upload')) assert fixture.uploaded_file_is_listed(fixture.file_to_upload1.name) assert not fixture.uploaded_file_is_listed(fixture.file_to_upload2.name) # Upload a second file browser.type(XPath.input_labelled('Choose file(s)'), fixture.file_to_upload2.name) browser.click(XPath.button_labelled('Upload')) assert fixture.uploaded_file_is_listed(fixture.file_to_upload1.name) assert fixture.uploaded_file_is_listed(fixture.file_to_upload2.name)
def test_submit_form_with_expired_csrf_token(web_fixture, csrf_fixture): """A form submitted with a valid expired token, shows a validation exception. After refresh, a new token is received and submit works.""" fixture = csrf_fixture wsgi_app = web_fixture.new_wsgi_app(child_factory=fixture.MyForm.factory(), enable_js=True) web_fixture.reahl_server.set_app(wsgi_app) browser = web_fixture.driver_browser browser.open('/') select_widget_path = XPath.select_labelled('choice') assert browser.get_value(select_widget_path) == '1' browser.select(select_widget_path, '2') wsgi_app.config.web.csrf_timeout_seconds = 0.5 time.sleep(wsgi_app.config.web.csrf_timeout_seconds + 0.1) browser.click(XPath.button_labelled('Submit')) wsgi_app.config.web.csrf_timeout_seconds = 300 error_message = XPath.paragraph().including_text( 'This page has expired. For security reasons, please review your input and retry.' ) assert browser.is_element_present(error_message) #case: submit again should work now - new csrftoken received in GET browser.click(XPath.button_labelled('Submit')) assert not browser.is_element_present(error_message)
def test_async_in_progress(web_fixture, large_file_upload_input_fixture): """While a large file is being uploaded, a progress bar and a Cancel button are displayed. Clicking on the Cancel button stops the upload and clears the file name from the list of uploaded files. """ fixture = large_file_upload_input_fixture fixture.run_hook_before = True web_fixture.reahl_server.set_app(fixture.new_wsgi_app(enable_js=True)) browser = web_fixture.driver_browser browser.open('/') assert not fixture.file_was_uploaded(fixture.file_to_upload1.name) assert not fixture.uploaded_file_is_listed(fixture.file_to_upload1.name) with web_fixture.reahl_server.in_background(wait_till_done_serving=False): browser.type(XPath.input_labelled('Choose file(s)'), fixture.file_to_upload1.name, wait_for_ajax=False) # Upload will block, see fixture assert browser.is_element_present('//ul/li/progress') progress = browser.get_attribute('//ul/li/progress', 'value') assert progress == '100' browser.click(XPath.button_labelled('Cancel'), wait_for_ajax=False) assert not fixture.uploaded_file_is_listed(fixture.file_to_upload1.name) assert not fixture.file_was_uploaded(fixture.file_to_upload1.name)
def test_refresh_widget_with_expired_csrf_token(web_fixture, csrf_fixture): """An Ajax request done with a valid expired token, shows a validation exception. After refresh, a new token is received and submit works.""" fixture = csrf_fixture wsgi_app = web_fixture.new_wsgi_app(child_factory=fixture.MyForm.factory(), enable_js=True) web_fixture.reahl_server.set_app(wsgi_app) browser = web_fixture.driver_browser wsgi_app.config.reahlsystem.debug = False #causes redirect to the error page and not stacktrace browser.open('/') select_widget_path = XPath.select_labelled('choice') assert browser.get_value(select_widget_path) == '1' wsgi_app.config.web.csrf_timeout_seconds = 0.5 time.sleep(wsgi_app.config.web.csrf_timeout_seconds + 0.1) browser.select(select_widget_path, '2', wait_for_ajax=False) wsgi_app.config.web.csrf_timeout_seconds = 300 with browser.alert_expected() as js_alert: assert js_alert.text == 'This page has expired. For security reasons, please review your input and retry.' js_alert.accept() #case: new csrftoken received in GET assert browser.get_value(select_widget_path) == '1' assert browser.wait_for_element_not_present( XPath.paragraph().with_text('Chosen: 2')) browser.select(select_widget_path, '2') assert browser.wait_for_element_present( XPath.paragraph().with_text('Chosen: 2')) assert browser.wait_for_not(browser.is_alert_present)
def test_simple_file_input_exceptions(web_fixture): """Usually, when a DomainException happens during a form submit Inputs save the input they received so that such input can be pre-populated on the screen rendered by a subsequent GET for a user to correct possible mistakes and resubmit. In the case of a SimpleFileInput however, the UploadedFile objects that serve as user input to a SimpleFileInput are discarded when a DomainException happens. This is because it is not possible to prepopulate its value using HTML or JS as this would be a security risk. """ file_to_upload = temp_file_with('some content') failing_constraint = FailingConstraint('I am breaking') class DomainObject: def __init__(self): self.file = None @exposed def fields(self, fields): fields.file = FileField(allow_multiple=False, label='Attached files') # FailingConstraint is declared in module level scope for it to be pickleable fields.file.add_validation_constraint(failing_constraint) @exposed def events(self, events): events.upload = Event(label='Upload') domain_object = DomainObject() class FileUploadForm(Form): def __init__(self, view): super().__init__(view, 'test') file_input = self.add_child( SimpleFileInput(self, domain_object.fields.file)) if file_input.validation_error: self.add_child(self.create_error_label(file_input)) self.define_event_handler(domain_object.events.upload) self.add_child(ButtonInput(self, domain_object.events.upload)) wsgi_app = web_fixture.new_wsgi_app(child_factory=FileUploadForm.factory(), enable_js=False) web_fixture.reahl_server.set_app(wsgi_app) browser = web_fixture.driver_browser browser.open('/') browser.type(XPath.input_of_type('file'), file_to_upload.name) browser.click(XPath.button_labelled('Upload')) assert browser.is_element_present('//label[text()="I am breaking"]') # Message is cleared on second attempt failing_constraint.fail = False browser.type(XPath.input_of_type('file'), file_to_upload.name) browser.click(XPath.button_labelled('Upload')) assert not browser.is_element_present('//label[text()="I am breaking"]')
def test_addressbook2bootstrap(web_fixture, addressbook2bootstrap_scenario): fixture = addressbook2bootstrap_scenario fixture.start_example_app() web_fixture.driver_browser.open('/') web_fixture.driver_browser.set_window_size('xs') web_fixture.driver_browser.type(XPath.input_labelled('Name'), 'John') web_fixture.driver_browser.type(XPath.input_labelled('Email'), '*****@*****.**') web_fixture.driver_browser.capture_cropped_screenshot(fixture.new_screenshot_path('bootstrapform.png'))
def without_layout(self): """The error is inserted as the very first element on the page""" class CustomPage(HTML5Page): def __init__(self, view): super().__init__(view) self.body.add_child(P(view, 'the first paragraph on the page')) self.CustomPage = CustomPage self.expected_alert = XPath('div')[1].including_class( 'alert').inside_of(XPath.any('body'))