test 1.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#!/usr/bin/env python3

import time
import socket
import unittest
import subprocess
import contextlib

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

PORT = 4000
JEKYLL = None


def setUpModule():
    global JEKYLL

    assert not port_open()

    subprocess.check_call(('jekyll', 'build', '--verbose'))
    JEKYLL = subprocess.Popen((
        'jekyll',
        'serve',
        '--skip-initial-build',
        '--port', str(PORT),
    ))

    for x in range(10):
        time.sleep(1)
        if port_open():
            break
    else:
        assert False, "Could not find site"


def tearDownModule():
    global JEKYLL

    JEKYLL.kill()


def port_open():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    with contextlib.closing(s) as s:
        return s.connect_ex(('127.0.0.1', PORT)) == 0


class TestSite(unittest.TestCase):
    def setUp(self):
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        self.driver = webdriver.Chrome(chrome_options=chrome_options)
        self.driver.implicitly_wait(10)

    def tearDown(self):
        self.driver.quit()

    def test_smoke(self):
        try:
            self.driver.get('http://127.0.0.1:{}'.format(PORT))
            self.driver.find_element_by_class_name('site-header')
        except NoSuchElementException as exc:
            self.fail(exc.msg)


if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestSite)
    unittest.TextTestRunner(verbosity=2).run(suite)