frontend_tests.py
2.84 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import urllib.request
from flask_testing import LiveServerTestCase
from selenium import webdriver
from app import create_app
from pdc_config import TestConfig
class BaseFrontTestCase(LiveServerTestCase):
def create_app(self):
# # remove logging lines on test output
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.INFO)
log.disabled = True
os.environ['WERKZEUG_RUN_MAIN'] = 'true'
# pass in test configurations
app = create_app(TestConfig)
self.db_path = os.path.join(app.instance_path, 'test-app.db')
app.config.update(
# Specify the test database as the default in_memory wont work for web tests
SQLALCHEMY_DATABASE_URI='sqlite:///' + self.db_path,
# Change the port that the liveserver listens on as we dont want to conflict with running:5000
LIVESERVER_PORT=8943
)
self.app_context = app.app_context()
self.app_context.push()
return app
def setUp(self):
self.driver = self.create_chrome_driver()
# db.create_all()
def tearDown(self):
# fs_utils.clean_dirs()
# db.session.remove()
# db.drop_all()
self.app_context.pop()
if os.path.exists(self.db_path):
os.remove(self.db_path)
self.driver.close()
self.driver.quit()
# noinspection PyMethodMayBeStatic
def is_in_docker(self):
"""
Test if in docker.
:return: True if running in docker, False otherwise.
"""
if os.path.exists('/proc/self/cgroup'):
with open('/proc/self/cgroup') as f:
content = f.read()
return '/docker/' in content
return False
def create_chrome_driver(self):
"""
Create then return the chrome driver.
:return: the chrome driver.
"""
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
# options.add_argument('--log-level=3')
# options.add_experimental_option('excludeSwitches', ['enable-logging'])
if self.is_in_docker():
options.add_argument("--no-sandbox")
return webdriver.Chrome(options=options)
class AccessTestCase(BaseFrontTestCase):
def test_ping(self):
self.assertEqual("http://localhost:8943", self.get_server_url())
def test_selenium_access(self):
# On ouvre le navigateur avec l'adresse du serveur.
self.driver.get(self.get_server_url())
# L'adresse dans l'url doit être celle que l'on attend.
self.assertEqual(self.driver.current_url, 'http://localhost:8943/')
def test_server_is_up_and_running(self):
response = urllib.request.urlopen(self.get_server_url())
self.assertEqual(response.code, 200)