| 1 |
// This tests basic search behavior. |
| 2 |
|
| 3 |
fail-on-js-error: true |
| 4 |
go-to: |DOC_PATH| + "search/index.html" |
| 5 |
|
| 6 |
define-function: ( |
| 7 |
"open-search", |
| 8 |
[], |
| 9 |
block { |
| 10 |
assert-css: ("#mdbook-search-wrapper", {"display": "none"}) |
| 11 |
press-key: 's' |
| 12 |
wait-for-css-false: ("#mdbook-search-wrapper", {"display": "none"}) |
| 13 |
} |
| 14 |
) |
| 15 |
|
| 16 |
call-function: ("open-search", {}) |
| 17 |
assert-text: ("#mdbook-searchresults-header", "") |
| 18 |
write: "extraordinary" |
| 19 |
wait-for-text: ("#mdbook-searchresults-header", "2 search results for 'extraordinary':") |
| 20 |
// Close the search display |
| 21 |
press-key: 'Escape' |
| 22 |
wait-for-css: ("#mdbook-search-wrapper", {"display": "none"}) |
| 23 |
// Reopening the search should show the last value |
| 24 |
call-function: ("open-search", {}) |
| 25 |
assert-text: ("#mdbook-searchresults-header", "2 search results for 'extraordinary':") |
| 26 |
// Navigate to a sub-chapter |
| 27 |
go-to: "./inner/chapter_2.html" |
| 28 |
assert-text: ("#mdbook-searchresults-header", "") |
| 29 |
call-function: ("open-search", {}) |
| 30 |
write: "kaleidoscope" |
| 31 |
wait-for-text: ("#mdbook-searchresults-header", "2 search results for 'kaleidoscope':") |
| 32 |
|
| 33 |
// Now we test search shortcuts and more page changes. |
| 34 |
go-to: |DOC_PATH| + "search/index.html" |
| 35 |
|
| 36 |
// This check is to ensure that the search bar is inside the search wrapper. |
| 37 |
assert: "#mdbook-search-wrapper #mdbook-searchbar" |
| 38 |
assert-css: ("#mdbook-search-wrapper", {"display": "none"}) |
| 39 |
|
| 40 |
// Now we make sure the search input appear with the `S` shortcut. |
| 41 |
press-key: 's' |
| 42 |
wait-for-css-false: ("#mdbook-search-wrapper", {"display": "none"}) |
| 43 |
// We ensure the search bar has the focus. |
| 44 |
wait-for: "#mdbook-searchbar:focus" |
| 45 |
// Pressing a key will therefore update the search input. |
| 46 |
press-key: 't' |
| 47 |
assert-text: ("#mdbook-searchbar", "t") |
| 48 |
|
| 49 |
// Now we press `Escape` to ensure that the search input disappears again. |
| 50 |
press-key: 'Escape' |
| 51 |
wait-for-css: ("#mdbook-search-wrapper", {"display": "none"}) |
| 52 |
|
| 53 |
// Making it appear by clicking on the search button. |
| 54 |
click: "#mdbook-search-toggle" |
| 55 |
wait-for-css: ("#mdbook-search-wrapper", {"display": "block"}) |
| 56 |
// We ensure the search bar has the focus. |
| 57 |
assert: "#mdbook-searchbar:focus" |
| 58 |
|
| 59 |
// We input "thunder". |
| 60 |
write: "thunder" |
| 61 |
// The results should now appear. |
| 62 |
wait-for-text: ("#mdbook-searchresults-header", "1 search result for 'thunder':") |
| 63 |
assert: "#mdbook-searchresults" |
| 64 |
// Ensure that the URL was updated as well. |
| 65 |
assert-document-property: ({"URL": "?search=thunder"}, ENDS_WITH) |
| 66 |
|
| 67 |
// Now we ensure that when we land on the page with a "search in progress", the search results are |
| 68 |
// loaded and that the search input has focus. |
| 69 |
go-to: |DOC_PATH| + "search/index.html?search=thunder" |
| 70 |
wait-for-text: ("#mdbook-searchresults-header", "1 search result for 'thunder':") |
| 71 |
assert: "#mdbook-searchbar:focus" |
| 72 |
assert: "#mdbook-searchresults" |
| 73 |
|
| 74 |
// And now we press `Escape` to close everything. |
| 75 |
press-key: 'Escape' |
| 76 |
wait-for-css: ("#mdbook-search-wrapper", {"display": "none"}) |
| 77 |
|
| 78 |
// Search result links should have a `?highlight=` query param, which will highlight |
| 79 |
// the search term when we navigate to the destination page. |
| 80 |
go-to: |DOC_PATH| + "search/index.html?search=kale" |
| 81 |
assert: '[href*="highlight=kale"]' |
| 82 |
|
| 83 |
// On navigation, text on the destination page that matches the search term should be highlighted. |
| 84 |
go-to: |DOC_PATH| + "search/index.html?highlight=kale" |
| 85 |
wait-for: '[data-markjs="true"]' |
| 86 |
assert-text: ('[data-markjs="true"]', "kale", ALL) |
| 87 |
|
| 88 |
// Clicking on any of the highlights should dismiss them. |
| 89 |
click: '[data-markjs="true"]' |
| 90 |
assert-false: '[data-markjs="true"]' |
| 91 |
// The `?highlight=` URL query param should also have been removed |
| 92 |
// (so that highlights don't unexpectedly appear again.) |
| 93 |
assert-window-property-false: ({"location": "highlight=kale"}, CONTAINS) |
| 94 |
// Verify this by clicking on a heading to navigate to its anchor. This would otherwise preserve |
| 95 |
// search params, and if the query param were not removed, highlights would appear again. |
| 96 |
click: 'main h1 a' |
| 97 |
assert-false: '[data-markjs="true"]' |