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

Commit 75d541a

Browse files
committed
add tests
1 parent 60d04fe commit 75d541a

File tree

1 file changed

+105
-2
lines changed

1 file changed

+105
-2
lines changed

tests/test_ui.py

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os
66
import json
77
import time
8+
from urllib import parse
89

910
import pytest
1011
import sphinx
@@ -571,9 +572,8 @@ def test_enter_button_on_input_field_when_no_result_active(selenium, app, status
571572
)
572573
search_outer_input.send_keys('i am searching')
573574
search_outer_input.send_keys(Keys.ENTER)
574-
575575
WebDriverWait(selenium, 10).until(
576-
EC.url_matches(app.outdir / 'search.html')
576+
EC.url_contains(app.outdir / 'search.html')
577577
)
578578

579579
# enter button should redirect the user to search page
@@ -635,3 +635,106 @@ def test_position_search_modal(selenium, app, status, warning):
635635
assert (
636636
abs(actual_y - calculated_y) < 10
637637
), f'difference between calculated and actual y coordinate should not be greater than 10 pixels for {"x".join(map(str, window_size))}'
638+
639+
640+
@pytest.mark.sphinx(srcdir=TEST_DOCS_SRC)
641+
def test_writing_query_adds_rtd_search_as_url_param(selenium, app, status, warning):
642+
"""Test if the `rtd_search` query param is added to the url when user is searching."""
643+
app.build()
644+
path = app.outdir / 'index.html'
645+
646+
# 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+
659+
injected_script = SCRIPT_TAG + ajax_func
660+
661+
with InjectJsManager(path, injected_script) as _:
662+
selenium.get(f'file://{path}')
663+
open_search_modal(selenium)
664+
query = 'i am searching'
665+
query_len = len(query)
666+
667+
assert (
668+
'rtd_search=' not in parse.unquote(selenium.current_url)
669+
), 'rtd_search param must not be present in the url when page loads'
670+
671+
search_outer_input = selenium.find_element_by_class_name(
672+
'search__outer__input'
673+
)
674+
search_outer_input.send_keys(query)
675+
query_param = f'rtd_search={query}'
676+
677+
assert (
678+
query_param in parse.unquote(selenium.current_url)
679+
), 'query param must be present in the url'
680+
681+
# deleting query from input field
682+
for i in range(query_len):
683+
search_outer_input.send_keys(Keys.BACK_SPACE)
684+
685+
if i != query_len -1:
686+
687+
current_query = query[:query_len - i - 1]
688+
current_url = parse.unquote(selenium.current_url)
689+
query_in_url = current_url[current_url.find('rtd_search'):]
690+
691+
assert (
692+
f'rtd_search={current_query}' == query_in_url
693+
)
694+
695+
assert (
696+
'rtd_search=' not in parse.unquote(selenium.current_url)
697+
), 'rtd_search param must not be present in the url if query is empty'
698+
699+
700+
@pytest.mark.sphinx(srcdir=TEST_DOCS_SRC)
701+
def test_modal_open_if_rtd_search_is_present(selenium, app, status, warning):
702+
"""Test if search modal opens if `rtd_search` query param is present in the URL."""
703+
app.build()
704+
path = app.outdir / 'index.html'
705+
706+
# 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+
719+
injected_script = SCRIPT_TAG + ajax_func
720+
721+
with InjectJsManager(path, injected_script) as _:
722+
selenium.get(f'file://{path}?rtd_search=i am searching')
723+
time.sleep(3) # give time to open modal and start searching
724+
725+
search_outer_wrapper = selenium.find_element_by_class_name(
726+
'search__outer__wrapper'
727+
)
728+
search_result_box = selenium.find_element_by_class_name(
729+
'search__result__box'
730+
)
731+
732+
assert (
733+
search_outer_wrapper.is_displayed() is True
734+
), 'search modal should displayed when the page loads'
735+
assert (
736+
search_result_box.text == 'Error Occurred. Please try again.'
737+
), 'user should be notified that there is error while searching'
738+
assert (
739+
len(search_result_box.find_elements_by_css_selector('*')) == 0
740+
), 'search result box should not have any child elements because there are no results'

0 commit comments

Comments
 (0)