2016-09-30 23:13:41 +01:00
|
|
|
import unittest
|
|
|
|
import os.path
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
|
|
|
class TestClient:
|
|
|
|
output_path = os.path.realpath('./output')
|
|
|
|
|
2016-10-08 22:44:45 +01:00
|
|
|
def get(self, path, JS=True):
|
2016-09-30 23:13:41 +01:00
|
|
|
file_path = self.build_path(path)
|
|
|
|
content = "".join(open(file_path).readlines())
|
|
|
|
if path.endswith('html'):
|
|
|
|
content = BeautifulSoup(content, 'html.parser')
|
2016-10-08 22:44:45 +01:00
|
|
|
if JS:
|
|
|
|
for script in content(["noscript"]): # Remove noscript tags
|
|
|
|
script.extract()
|
2016-09-30 23:13:41 +01:00
|
|
|
return content
|
|
|
|
|
|
|
|
def build_path(self, path):
|
|
|
|
if path.startswith('/'):
|
|
|
|
path = path[1:]
|
|
|
|
return os.path.join(self.output_path, path)
|
|
|
|
|
|
|
|
def exists(self, path):
|
2016-10-08 22:44:45 +01:00
|
|
|
return os.path.exists(self.build_path(path))
|
2016-09-30 23:13:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestCase(unittest.TestCase):
|
|
|
|
client = TestClient()
|
|
|
|
|
2016-10-02 11:36:21 +01:00
|
|
|
def get_children(self, content):
|
2017-01-14 23:53:18 +00:00
|
|
|
return str(list(content.children)[0]).strip()
|
2016-10-02 11:36:21 +01:00
|
|
|
|
2016-09-30 23:13:41 +01:00
|
|
|
def assertTitle(self, content, title):
|
|
|
|
self.assertIn(title, content.title.string)
|
2016-10-02 11:36:21 +01:00
|
|
|
|
|
|
|
def assertHeaderTitle(self, content, title):
|
2017-01-17 22:14:01 +00:00
|
|
|
header_title = content.find('div', class_="header-content").find('h1')
|
2016-10-02 11:36:21 +01:00
|
|
|
self.assertIn(title, self.get_children(header_title))
|
2017-01-14 23:53:18 +00:00
|
|
|
|
|
|
|
def assertSamePath(self, p1, p2):
|
|
|
|
self.assertEqual(self.client.build_path(p1), self.client.build_path(p2))
|