class AboutPage(PageObject):
    """
    Testing representation of the About page
    """
    url = 'http://localhost:{}/pages/about.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('About') class EventPage(PageObject):
    """
    Testing representation of a sample event page
    """
    url = 'http://localhost:{}/events/pydata.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('PyData') class EventsPage(PageObject):
    """
    Testing representation of the events listing page
    """
    url = 'http://localhost:{}/events.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('Events') class MP4Page(PageObject):
    """
    Testing representation of a sample MP4 video page
    """
    url = 'http://localhost:{}/pyohio-2011/pyohio-2011--saturday-lightning-talks.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('PyOhio 2011: Saturday Lightning Talks') class IndexPage(PageObject):
    """
    Testing representation of the main page of the site
    """
    url = 'http://localhost:{}/index.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == 'PyTube.org'

    def search(self, phrase):
        """
        Enter a search phrase and display the results
        """
        selector = 'input.gsc-input'
        self.wait_for_element_visibility(selector, 'Search input available')
        self.q(css=selector).fill(phrase + Keys.ENTER)
        self.wait_for_element_visibility('.gsc-results', 'Search results shown') class OGVPage(PageObject):
    """
    Testing representation of a sample OGV video page
    """
    url = 'http://localhost:{}/pycon-za-2013/python-in-debian-and-ubuntu.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('Python in Debian and Ubuntu') PORT = 8081 class SpeakerPage(PageObject):
    """
    Testing representation of a sample speaker page
    """
    url = 'http://localhost:{}/speaker/glyph.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('Glyph') class SpeakersPage(PageObject):
    """
    Testing representation of the speakers listing page
    """
    url = 'http://localhost:{}/speakers.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('Speakers') class TagPage(PageObject):
    """
    Testing representation of a sample tag page
    """
    url = 'http://localhost:{}/tag/subtitles/'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('subtitles') class TagsPage(PageObject):
    """
    Testing representation of the tags listing page
    """
    url = 'http://localhost:{}/tags.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('Tags') class VimeoPage(PageObject):
    """
    Testing representation of a sample Vimeo video page
    """
    url = 'http://localhost:{}/boston-python-meetup/boston-python-meetup--testing--where-do-i-start.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('Boston Python Meetup: Testing: Where do I start?') class YouTubePage(PageObject):
    """
    Testing representation of a sample YouTube video page
    """
    url = 'http://localhost:{}/europython-2011/browse-and-print-problems-and-solutions.html'.format(PORT)

    def is_browser_on_page(self):
        return self.browser.title == TITLE_TEMPLATE.format('Browse and print problems and solutions')
from multiprocessing import Process
from socketserver import TCPServer
from bok_choy.web_app_test import WebAppTest
from pelican.server import ComplexHTTPRequestHandler
from .pages import (
    AboutPage, EventPage, EventsPage, MP4Page, IndexPage, OGVPage, PORT,
    SpeakerPage, SpeakersPage, TagPage, TagsPage, VimeoPage, YouTubePage
)
import os
import time
import unittest
import requests



def start_pelican():
    os.chdir('output')
    TCPServer.allow_reuse_address = True
    httpd = TCPServer(('', PORT), ComplexHTTPRequestHandler)
    httpd.serve_forever()

def wait_for_pelican(seconds):
    end = time.time() + seconds
    url = 'http://localhost:{}/pages/about.html'.format(PORT)
    while time.time() < end:
        try:
            response = requests.get(url, timeout=(end - time.time()))
            response.raise_for_status()
            return
        except requests.ConnectionError:
            continue
    raise Exception('Timed out connecting to pelican server')

class TestAccessibility(WebAppTest):
    """
    Tests for the accessibility of various pages
    """

    @classmethod
    def setUpClass(cls):
        cls.server = Process(target=start_pelican)
        cls.server.start()
        wait_for_pelican(seconds=30)

    @classmethod
    def tearDownClass(cls):
        cls.server.terminate()

    def test_about(self):
        """
        The About page should have good accessibility
        """
        self._check_a11y(AboutPage)

    def test_event(self):
        """
        The page for a single event should have good accessibility
        """
        self._check_a11y(EventPage)

    def test_events(self):
        """
        The events listing page should have good accessibility
        """
        self._check_a11y(EventsPage)

    def test_index(self):
        """
        The main page should have good accessibility
        """
        self._check_a11y(IndexPage)

    def test_search_results(self):
        """
        The search results should have good accessibility
        """
        page = IndexPage(self.browser)
        page.visit()
        page.search('accessibility')
        page.a11y_audit.check_for_accessibility_errors()

    def test_speaker(self):
        """
        The page for a single speaker should have good accessibility
        """

        self._check_a11y(SpeakerPage)