comparison tests/e2e/libervia-web/conftest.py @ 3498:d78b5eae912a

tests: update following names change
author Goffi <goffi@goffi.org>
date Fri, 16 Apr 2021 18:32:34 +0200
parents tests/e2e/libervia/conftest.py@7550ae9cfbac
children dfccc90cacc6
comparison
equal deleted inserted replaced
3497:73e04040d577 3498:d78b5eae912a
1 #!/usr/bin/env python3
2
3 # Libervia: an XMPP client
4 # Copyright (C) 2009-2021 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 WEB_HOST = "libervia-web.test"
30 WEB_PORT_HTTPS = 8443
31 BASE_URL = f"https://{WEB_HOST}:{WEB_PORT_HTTPS}"
32 SIZE_DESKTOP = (1024, 728)
33 SIZE_MOBILE = (380, 640)
34 accounts_cookies = {}
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("LIBERVIA_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("LIBERVIA_TEST_E2E_WEB_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(WEB_HOST, WEB_PORT_HTTPS)
86 yield helium
87 if os.getenv("LIBERVIA_TEST_E2E_WEB_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 def log_in(browser, account):
96 try:
97 account_cookies = accounts_cookies[account]
98 except KeyError:
99 browser.get_driver().delete_all_cookies()
100 browser.go_to("https://libervia-web.test:8443/login")
101 browser.write(account, into="login")
102 browser.write("test", into="password")
103 browser.click("log in")
104 accounts_cookies[account] = browser.get_driver().get_cookies()[0]
105 else:
106 browser.get_driver().add_cookie(account_cookies)
107
108 @pytest.fixture
109 def log_in_account1(browser):
110 log_in(browser, "account1")
111
112 @pytest.fixture
113 def log_in_account1_s2(browser):
114 log_in(browser, "account1_s2")
115
116 @pytest.fixture
117 def mobile_screen(browser):
118 browser.get_driver().set_window_size(*SIZE_MOBILE)
119
120
121 @pytest.fixture
122 def desktop_screen(browser):
123 browser.get_driver().set_window_size(*SIZE_DESKTOP)