|
5 | 5 | import os |
6 | 6 | import json |
7 | 7 | import time |
| 8 | +from urllib import parse |
8 | 9 |
|
9 | 10 | import pytest |
10 | 11 | import sphinx |
@@ -571,9 +572,8 @@ def test_enter_button_on_input_field_when_no_result_active(selenium, app, status |
571 | 572 | ) |
572 | 573 | search_outer_input.send_keys('i am searching') |
573 | 574 | search_outer_input.send_keys(Keys.ENTER) |
574 | | - |
575 | 575 | WebDriverWait(selenium, 10).until( |
576 | | - EC.url_matches(app.outdir / 'search.html') |
| 576 | + EC.url_contains(app.outdir / 'search.html') |
577 | 577 | ) |
578 | 578 |
|
579 | 579 | # enter button should redirect the user to search page |
@@ -635,3 +635,106 @@ def test_position_search_modal(selenium, app, status, warning): |
635 | 635 | assert ( |
636 | 636 | abs(actual_y - calculated_y) < 10 |
637 | 637 | ), 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