Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 29cdcbb

Browse files
committed
refactor tests
1 parent 75d541a commit 29cdcbb

File tree

2 files changed

+107
-117
lines changed

2 files changed

+107
-117
lines changed

tests/test_ui.py

Lines changed: 9 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@
1515
from selenium.webdriver.support import expected_conditions as EC
1616
from selenium.webdriver.support.ui import WebDriverWait
1717

18-
from tests.utils import InjectJsManager, set_viewport_size
18+
from tests.utils import InjectJsManager, set_viewport_size, get_ajax_overwrite_func
1919
from tests import TEST_DOCS_SRC
2020

2121

22-
DUMMY_RESULTS = os.path.join(
23-
os.path.abspath(os.path.dirname(__file__)),
24-
'dummy_results.json'
25-
)
26-
2722
READTHEDOCS_DATA = {
2823
'project': 'docs',
2924
'version': 'latest',
@@ -244,21 +239,7 @@ def test_no_results_msg(selenium, app, status, warning):
244239
path = app.outdir / 'index.html'
245240

246241
# to test this, we need to override the $.ajax function
247-
ajax_func = '''
248-
<script>
249-
$.ajax = function(params) {
250-
return params.complete(
251-
{
252-
responseJSON: {
253-
results: []
254-
}
255-
},
256-
'success'
257-
)
258-
}
259-
</script>
260-
'''
261-
242+
ajax_func = get_ajax_overwrite_func('zero_results')
262243
injected_script = SCRIPT_TAG + ajax_func
263244

264245
with InjectJsManager(path, injected_script) as _:
@@ -295,18 +276,7 @@ def test_error_msg(selenium, app, status, warning):
295276
path = app.outdir / 'index.html'
296277

297278
# to test this, we need to override the $.ajax function
298-
ajax_func = '''
299-
<script>
300-
$.ajax = function(params) {
301-
return params.error(
302-
{ },
303-
'error',
304-
'Dummy Error.'
305-
)
306-
}
307-
</script>
308-
'''
309-
279+
ajax_func = get_ajax_overwrite_func('error')
310280
injected_script = SCRIPT_TAG + ajax_func
311281

312282
with InjectJsManager(path, injected_script) as _:
@@ -343,24 +313,7 @@ def test_searching_msg(selenium, app, status, warning):
343313
path = app.outdir / 'index.html'
344314

345315
# to test this, we need to override the $.ajax function
346-
# setTimeout is used here to give a real feel of the API call
347-
ajax_func = '''
348-
<script>
349-
$.ajax = function(params) {
350-
return setTimeout(function(params){
351-
return params.complete(
352-
{
353-
responseJSON: {
354-
results: []
355-
}
356-
},
357-
'success'
358-
)
359-
}, 2000, params);
360-
}
361-
</script>
362-
'''
363-
316+
ajax_func = get_ajax_overwrite_func('timeout__zero_results')
364317
injected_script = SCRIPT_TAG + ajax_func
365318

366319
with InjectJsManager(path, injected_script) as _:
@@ -407,22 +360,8 @@ def test_results_displayed_to_user(selenium, app, status, warning):
407360
app.build()
408361
path = app.outdir / 'index.html'
409362

410-
with open(DUMMY_RESULTS, 'r') as f:
411-
dummy_res = f.read()
412-
413363
# to test this, we need to override the $.ajax function
414-
ajax_func = f'''
415-
<script>
416-
$.ajax = function(params) {{
417-
return params.complete(
418-
{{
419-
responseJSON: { dummy_res }
420-
}}
421-
)
422-
}}
423-
</script>
424-
'''
425-
364+
ajax_func = get_ajax_overwrite_func('dummy_results')
426365
injected_script = SCRIPT_TAG + ajax_func
427366

428367
with InjectJsManager(path, injected_script) as _:
@@ -471,22 +410,8 @@ def test_navigate_results_with_arrow_up_and_down(selenium, app, status, warning)
471410
app.build()
472411
path = app.outdir / 'index.html'
473412

474-
with open(DUMMY_RESULTS, 'r') as f:
475-
dummy_res = f.read()
476-
477413
# to test this, we need to override the $.ajax function
478-
ajax_func = f'''
479-
<script>
480-
$.ajax = function(params) {{
481-
return params.complete(
482-
{{
483-
responseJSON: { dummy_res }
484-
}}
485-
)
486-
}}
487-
</script>
488-
'''
489-
414+
ajax_func = get_ajax_overwrite_func('dummy_results')
490415
injected_script = SCRIPT_TAG + ajax_func
491416

492417
with InjectJsManager(path, injected_script) as _:
@@ -549,18 +474,7 @@ def test_enter_button_on_input_field_when_no_result_active(selenium, app, status
549474
path = app.outdir / 'index.html'
550475

551476
# to test this, we need to override the $.ajax function
552-
ajax_func = '''
553-
<script>
554-
$.ajax = function(params) {
555-
return params.error(
556-
{ },
557-
'error',
558-
'Dummy Error.'
559-
)
560-
}
561-
</script>
562-
'''
563-
477+
ajax_func = get_ajax_overwrite_func('error')
564478
injected_script = SCRIPT_TAG + ajax_func
565479

566480
with InjectJsManager(path, injected_script) as _:
@@ -644,18 +558,7 @@ def test_writing_query_adds_rtd_search_as_url_param(selenium, app, status, warni
644558
path = app.outdir / 'index.html'
645559

646560
# to test this, we need to override the $.ajax function
647-
ajax_func = '''
648-
<script>
649-
$.ajax = function(params) {
650-
return params.error(
651-
{ },
652-
'error',
653-
'Dummy Error.'
654-
)
655-
}
656-
</script>
657-
'''
658-
561+
ajax_func = get_ajax_overwrite_func('error')
659562
injected_script = SCRIPT_TAG + ajax_func
660563

661564
with InjectJsManager(path, injected_script) as _:
@@ -704,18 +607,7 @@ def test_modal_open_if_rtd_search_is_present(selenium, app, status, warning):
704607
path = app.outdir / 'index.html'
705608

706609
# to test this, we need to override the $.ajax function
707-
ajax_func = '''
708-
<script>
709-
$.ajax = function(params) {
710-
return params.error(
711-
{ },
712-
'error',
713-
'Dummy Error.'
714-
)
715-
}
716-
</script>
717-
'''
718-
610+
ajax_func = get_ajax_overwrite_func('error')
719611
injected_script = SCRIPT_TAG + ajax_func
720612

721613
with InjectJsManager(path, injected_script) as _:

tests/utils.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
import os
66

7+
DUMMY_RESULTS = os.path.join(
8+
os.path.abspath(os.path.dirname(__file__)),
9+
'dummy_results.json'
10+
)
11+
712

813
class InjectJsManager:
914
"""
@@ -47,3 +52,96 @@ def set_viewport_size(driver, width, height):
4752
height
4853
)
4954
driver.set_window_size(*window_size)
55+
56+
57+
def get_ajax_overwrite_func(type_, **kwargs):
58+
possible_types = [
59+
60+
# return ajax func which results in zero results
61+
'zero_results',
62+
63+
# return ajax func which results in error while searching
64+
'error',
65+
66+
# return ajax func with a setTimeout of 2000ms (default) and
67+
# results in zero results.
68+
# A possible `timeout` argument can be passed to change the
69+
# waiting time of setTimeout
70+
'timeout__zero_results',
71+
72+
# return ajax func with dummy results
73+
'dummy_results',
74+
]
75+
76+
# check if current type_ is passed
77+
assert (
78+
type_ in possible_types
79+
), 'wrong type is specified'
80+
81+
if type_ == 'zero_results':
82+
ajax_func = '''
83+
<script>
84+
$.ajax = function(params) {
85+
return params.complete(
86+
{
87+
responseJSON: {
88+
results: []
89+
}
90+
},
91+
'success'
92+
)
93+
}
94+
</script>
95+
'''
96+
97+
elif type_ == 'error':
98+
ajax_func = '''
99+
<script>
100+
$.ajax = function(params) {
101+
return params.error(
102+
{ },
103+
'error',
104+
'Dummy Error.'
105+
)
106+
}
107+
</script>
108+
'''
109+
110+
elif type_ == 'timeout__zero_results':
111+
timeout = kwargs.get('timeout') or 2000
112+
113+
# setTimeout is used here to give a real feel of the API call
114+
ajax_func = f'''
115+
<script>
116+
$.ajax = function(params) {{
117+
return setTimeout(function(params) {{
118+
return params.complete(
119+
{{
120+
responseJSON: {{
121+
results: []
122+
}}
123+
}},
124+
'success'
125+
)
126+
}}, { timeout }, params);
127+
}}
128+
</script>
129+
'''
130+
131+
elif type_ == 'dummy_results':
132+
with open(DUMMY_RESULTS, 'r') as f:
133+
dummy_res = f.read()
134+
135+
ajax_func = f'''
136+
<script>
137+
$.ajax = function(params) {{
138+
return params.complete(
139+
{{
140+
responseJSON: { dummy_res }
141+
}}
142+
)
143+
}}
144+
</script>
145+
'''
146+
147+
return ajax_func

0 commit comments

Comments
 (0)