comparison tests/e2e/libervia/conftest.py @ 3429:d4558f3cbf13

tests, docker(e2e): added e2e tests for Libervia: - moved jp tests to `e2e/jp` - new fixtures - adapted docker-compose - improved `run_e2e` with several flags + report on failure - doc to come
author Goffi <goffi@goffi.org>
date Fri, 27 Nov 2020 16:39:40 +0100
parents
children 84fb41b515a1
comparison
equal deleted inserted replaced
3428:a6ea53248c14 3429:d4558f3cbf13
1 #!/usr/bin/env python3
2
3 # SàT: an XMPP client
4 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import sys
20 import os
21 import socket
22 import pytest
23 import time
24 from datetime import datetime
25 from pathlib import Path
26 import helium
27
28
29 LIBERVIA_HOST = "libervia.test"
30 LIBERVIA_PORT_HTTPS = 8443
31 BASE_URL = f"https://{LIBERVIA_HOST}:{LIBERVIA_PORT_HTTPS}"
32 SIZE_DESKTOP = (1024, 728)
33 SIZE_MOBILE = (380, 640)
34 account_1_cookies = None
35
36
37 @pytest.hookimpl(tryfirst=True, hookwrapper=True)
38 def pytest_runtest_makereport(item, call):
39 # needed to get test results in request fixture
40 # cf. https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures
41 outcome = yield
42 rep = outcome.get_result()
43 setattr(item, "rep_" + rep.when, rep)
44
45
46 @pytest.fixture
47 def screenshot_on_failure(request):
48 yield
49 if request.node.rep_setup.passed:
50 if request.node.rep_call.failed:
51 report_dir = Path(os.getenv("SAT_TEST_REPORT_DIR", "/tmp/tests_report"))
52 dest_dir = report_dir/"screenshots"
53 dest_dir.mkdir(parents=True, exist_ok=True)
54 filename = f"{datetime.now().isoformat()}_{request.node.name}.png"
55 dest_path = dest_dir/filename
56 helium.get_driver().save_screenshot(str(dest_path))
57 print(f"screenshot saved to {dest_path}")
58
59
60 def wait_for_socket(host, port, retries=30):
61 sock = socket.socket()
62 while True:
63 try:
64 sock.connect((host, port))
65 except ConnectionRefusedError as e:
66 retries -= 1
67 if retries < 0:
68 print(f"Can't access server at {host}:{port}", file=sys.stderr)
69 raise e
70 else:
71 print(f"Can't connect to {host}:{port}, retrying ({retries})")
72 time.sleep(1)
73 else:
74 break
75
76
77 @pytest.fixture(scope="session")
78 def browser():
79 if os.getenv("SAT_TEST_E2E_LIBERVIA_NO_HEADLESS") is not None:
80 kwargs = {}
81 else:
82 kwargs = {"headless": True}
83 driver = helium.start_firefox(**kwargs)
84 driver.set_window_size(*SIZE_DESKTOP)
85 wait_for_socket(LIBERVIA_HOST, LIBERVIA_PORT_HTTPS)
86 yield helium
87 if os.getenv("SAT_TEST_E2E_LIBERVIA_KEEP_BROWSER") is None:
88 helium.kill_browser()
89
90
91 @pytest.fixture
92 def nobody_logged_in(browser):
93 browser.get_driver().delete_all_cookies()
94
95
96 @pytest.fixture
97 def log_in_account1(browser):
98 global account_1_cookies
99 if account_1_cookies is None:
100 browser.get_driver().delete_all_cookies()
101 browser.go_to("https://libervia.test:8443/login")
102 browser.write("account1", into="login")
103 browser.write("test", into="password")
104 browser.click("log in")
105 account_1_cookies = browser.get_driver().get_cookies()
106 else:
107 browser.get_driver().add_cookie(account_1_cookies)
108
109
110 @pytest.fixture
111 def mobile_screen(browser):
112 browser.get_driver().set_window_size(*SIZE_MOBILE)
113
114
115 @pytest.fixture
116 def desktop_screen(browser):
117 browser.get_driver().set_window_size(*SIZE_DESKTOP)