#!/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)