|
15 | 15 | from selenium.webdriver.support import expected_conditions as EC |
16 | 16 | from selenium.webdriver.support.ui import WebDriverWait |
17 | 17 |
|
18 | | -from tests.utils import InjectJsManager, set_viewport_size |
| 18 | +from tests.utils import InjectJsManager, set_viewport_size, get_ajax_overwrite_func |
19 | 19 | from tests import TEST_DOCS_SRC |
20 | 20 |
|
21 | 21 |
|
22 | | -DUMMY_RESULTS = os.path.join( |
23 | | - os.path.abspath(os.path.dirname(__file__)), |
24 | | - 'dummy_results.json' |
25 | | -) |
26 | | - |
27 | 22 | READTHEDOCS_DATA = { |
28 | 23 | 'project': 'docs', |
29 | 24 | 'version': 'latest', |
@@ -244,21 +239,7 @@ def test_no_results_msg(selenium, app, status, warning): |
244 | 239 | path = app.outdir / 'index.html' |
245 | 240 |
|
246 | 241 | # 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') |
262 | 243 | injected_script = SCRIPT_TAG + ajax_func |
263 | 244 |
|
264 | 245 | with InjectJsManager(path, injected_script) as _: |
@@ -295,18 +276,7 @@ def test_error_msg(selenium, app, status, warning): |
295 | 276 | path = app.outdir / 'index.html' |
296 | 277 |
|
297 | 278 | # 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') |
310 | 280 | injected_script = SCRIPT_TAG + ajax_func |
311 | 281 |
|
312 | 282 | with InjectJsManager(path, injected_script) as _: |
@@ -343,24 +313,7 @@ def test_searching_msg(selenium, app, status, warning): |
343 | 313 | path = app.outdir / 'index.html' |
344 | 314 |
|
345 | 315 | # 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') |
364 | 317 | injected_script = SCRIPT_TAG + ajax_func |
365 | 318 |
|
366 | 319 | with InjectJsManager(path, injected_script) as _: |
@@ -407,22 +360,8 @@ def test_results_displayed_to_user(selenium, app, status, warning): |
407 | 360 | app.build() |
408 | 361 | path = app.outdir / 'index.html' |
409 | 362 |
|
410 | | - with open(DUMMY_RESULTS, 'r') as f: |
411 | | - dummy_res = f.read() |
412 | | - |
413 | 363 | # 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') |
426 | 365 | injected_script = SCRIPT_TAG + ajax_func |
427 | 366 |
|
428 | 367 | with InjectJsManager(path, injected_script) as _: |
@@ -471,22 +410,8 @@ def test_navigate_results_with_arrow_up_and_down(selenium, app, status, warning) |
471 | 410 | app.build() |
472 | 411 | path = app.outdir / 'index.html' |
473 | 412 |
|
474 | | - with open(DUMMY_RESULTS, 'r') as f: |
475 | | - dummy_res = f.read() |
476 | | - |
477 | 413 | # 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') |
490 | 415 | injected_script = SCRIPT_TAG + ajax_func |
491 | 416 |
|
492 | 417 | with InjectJsManager(path, injected_script) as _: |
@@ -549,18 +474,7 @@ def test_enter_button_on_input_field_when_no_result_active(selenium, app, status |
549 | 474 | path = app.outdir / 'index.html' |
550 | 475 |
|
551 | 476 | # 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') |
564 | 478 | injected_script = SCRIPT_TAG + ajax_func |
565 | 479 |
|
566 | 480 | with InjectJsManager(path, injected_script) as _: |
@@ -644,18 +558,7 @@ def test_writing_query_adds_rtd_search_as_url_param(selenium, app, status, warni |
644 | 558 | path = app.outdir / 'index.html' |
645 | 559 |
|
646 | 560 | # 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') |
659 | 562 | injected_script = SCRIPT_TAG + ajax_func |
660 | 563 |
|
661 | 564 | with InjectJsManager(path, injected_script) as _: |
@@ -704,18 +607,7 @@ def test_modal_open_if_rtd_search_is_present(selenium, app, status, warning): |
704 | 607 | path = app.outdir / 'index.html' |
705 | 608 |
|
706 | 609 | # 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') |
719 | 611 | injected_script = SCRIPT_TAG + ajax_func |
720 | 612 |
|
721 | 613 | with InjectJsManager(path, injected_script) as _: |
|
0 commit comments