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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
#
# Copyright (C) 2018 FreeIPA Contributors see COPYING for license
#
"""
Test translations
"""
from ipatests.test_webui.ui_driver import UI_driver
from ipatests.test_webui.ui_driver import screenshot
try:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
except ImportError:
pass
import pytest
from re import sub
from lxml import html
from ipalib import api, util
class ConfigPageBase(UI_driver):
"""
Base class to test translation of pages which are located at /ipa/config/
"""
page_name = ''
def init_app(self):
"""
Load a web page
"""
self.url = '/'.join((self.get_base_url(), self.page_name))
self.load()
def get_base_url(self):
"""
Get FreeIPA Web UI config url
"""
host = self.config.get('ipa_server')
if not host:
self.skip('FreeIPA server hostname not configured')
return 'https://%s/ipa/config' % host
def files_loaded(self):
"""
Test if dependencies were loaded. (Checks if page has been rendered)
"""
indicator = self.find(".info-page", By.CSS_SELECTOR)
return indicator is not None
def load(self):
"""
Navigate to Web page and wait for loading of all dependencies.
"""
self.driver.get(self.url)
runner = self
WebDriverWait(self.driver, 10).until(
lambda d: runner.files_loaded()
)
def page_raw_source(self):
"""
Retrieve a raw source of the web page
"""
host = api.env.host
cacert = api.env.tls_ca_cert
conn = util.create_https_connection(host, cafile=cacert)
conn.request('GET', self.url)
response = conn.getresponse()
# check successful response from a server
assert response.status == 200
return response.read().decode('utf-8')
def has_no_child(self, tag, child_tag):
"""
Check if element with the given tag has no child with the given one
"""
parent = self.find("#{}".format(tag), By.CSS_SELECTOR)
if parent is None:
return True
child_element = self.find(".//{}".format(child_tag), By.XPATH, parent)
return child_element is None
def innerhtml(self, id):
"""
Extract html text from the current opened page by the given id
"""
dom_element = self.find("#{}".format(id), By.CSS_SELECTOR)
return dom_element.get_attribute('innerHTML').split('\n')
def innerhtml_noscript(self, id, raw_page):
"""
Extract html text from the given raw source of the page under the
'noscript' html tag with the given id
"""
html_tree = html.fromstring(raw_page)
noscript_tree = html_tree.xpath(
"//div[@id='{}']/noscript/*".format(id)
)
noscript_html_text = ''.join([html.tostring(elem, encoding="unicode")
for elem in noscript_tree])
noscript_html = []
# remove trailing whitespaces between close and open tags
for html_row in noscript_html_text.split('\n'):
noscript_html.append(sub('^[ ]+(?=(<|[ ]*$))', '', html_row))
return noscript_html
def check_noscript_innerhtml(self, html_id):
"""
Compare inner html under enabled javascript and disabled one
"""
# check if js is enabled in browser
assert self.has_no_child(html_id, 'noscript')
html_js_enabled = self.innerhtml(html_id)
raw_page = self.page_raw_source()
html_js_disabled = self.innerhtml_noscript(html_id, raw_page)
assert html_js_enabled == html_js_disabled
@pytest.mark.tier1
class TestSsbrowserPage(ConfigPageBase):
"""
Test translation of ssbrowser.html page
"""
page_name = 'ssbrowser.html'
@screenshot
def test_long_text_of_ssbrowser_page(self):
"""
Tests whether the text from '@i18n:ssbrowser-page' is synced
against '<noscript>' tag to ensure a similarity of the behavior.
"""
self.init_app()
self.check_noscript_innerhtml('ssbrowser-msg')
@pytest.mark.tier1
class TestUnauthorizedPage(ConfigPageBase):
"""
Test translation of unauthorized.html page
"""
page_name = 'unauthorized.html'
@screenshot
def test_long_text_of_unauthorized_page(self):
"""
Tests whether the text from '@i18n:unauthorized-page' is synced
against '<noscript>' tag to ensure a similarity of the behavior.
"""
self.init_app()
self.check_noscript_innerhtml('unauthorized-msg')
|