def test_slot_defaults(web_fixture): """If a View does not specify contents for a Slot, the Slot will be populated by the window's default widget for that slot if specified, else it will be left empty. """ class MainUI(UserInterface): def assemble(self): main = self.define_page(HTML5Page).use_layout(BasicPageLayout()) main.add_default_slot('main', P.factory(text='defaulted slot contents')) self.define_view('/', title='Hello') fixture = web_fixture wsgi_app = fixture.new_wsgi_app(site_root=MainUI) browser = Browser(wsgi_app) browser.open('/') # The default widget for the main slot is used [p] = browser.xpath('//p') assert p.text == 'defaulted slot contents' # The header slot has no default, and is thus left empty header_contents = browser.xpath('//header/*') assert not header_contents
def basic_assembly(fixture): """An application is built by extending UserInterface, and defining this UserInterface in an .assemble() method. To define the UserInterface, several Views are defined. Views are mapped to URLs. When a user GETs the URL of a View, a page is rendered back to the user. How that page is created can happen in different ways, as illustrated by each scenario of this test. """ wsgi_app = fixture.new_wsgi_app(site_root=fixture.MainUI) browser = Browser(wsgi_app) # GETting the URL results in the HTML for that View with warnings.catch_warnings(record=True) as caught_warnings: warnings.simplefilter('always') browser.open('/') vassert(browser.title == 'Hello') warning_messages = [six.text_type(i.message) for i in caught_warnings] vassert(len(warning_messages) == len(fixture.expected_warnings)) for caught, expected_message in zip_longest(warning_messages, fixture.expected_warnings): vassert(expected_message in caught) if fixture.content_includes_p: [message] = browser.xpath('//p') vassert(message.text == 'Hello world!') # The headers are set correctly response = browser.last_response vassert(response.content_length == fixture.expected_content_length) vassert(response.content_type == 'text/html') vassert(response.charset == 'utf-8') # Invalid URLs do not exist with warnings.catch_warnings(record=True): browser.open('/nonexistantview/', status=404)
def slots(fixture): """A View modifies the page by populating named Slots in the page with Widgets.""" wsgi_app = fixture.new_wsgi_app(site_root=fixture.MainUI) browser = Browser(wsgi_app) browser.open('/') vassert(browser.title == 'Hello') [main_p, footer_p] = browser.xpath('//p') vassert(main_p.text == 'Hello world') vassert(footer_p.text == 'I am the footer')
def test_slots(web_fixture, slot_scenarios): """A View modifies the page by populating named Slots in the page with Widgets.""" fixture = slot_scenarios wsgi_app = web_fixture.new_wsgi_app(site_root=fixture.MainUI) browser = Browser(wsgi_app) browser.open('/') assert browser.title == 'Hello' [main_p, footer_p] = browser.xpath('//p') assert main_p.text == 'Hello world' assert footer_p.text == 'I am the footer'