discourse/spec/system/page_objects/pages/search.rb
Alan Guo Xiang Tan e323628d8a
DEV: Speed up core system tests (#21394)
What is the problem?

We are relying on RSpec custom matchers in system tests by defining
predicates in page objects. The problem is that this can result in a
system test unnecessarily waiting up till the full duration of
Capybara's default wait time when the RSpec custom matcher is used with
`not_to`. Considering this topic page object where we have a `has_post?`
predicate defined.

```
class Topic < PageObject
  def has_post?
    has_css?('something')
  end
end
```

The assertion `expect(Topic.new).not_to have_post` will end up waiting
the full Capybara's default wait time since the RSpec custom matcher is
calling Capybara's `has_css?` method which will wait until the selector
appear. If the selector has already disappeared by the time the
assertion is called, we end up waiting for something that will never
exists.

This commit fixes such cases by introducing new predicates that uses
the `has_no_*` versions of Capybara's node matchers.

For future reference, `to have_css` and `not_to have_css` is safe to sue
because the RSpec matcher defined by Capbyara is smart enough to call
`has_css?` or `has_no_css?` based on the expectation of the assertion.
2023-05-05 07:45:53 +08:00

58 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module PageObjects
module Pages
class Search < PageObjects::Pages::Base
def type_in_search(input)
find("input.full-page-search").send_keys(input)
self
end
def clear_search_input
find("input.full-page-search").set("")
self
end
def heading_text
find("h1.search-page-heading").text
end
def click_search_button
find(".search-cta").click
end
def click_home_logo
find(".d-header .logo-mobile").click
end
def click_search_icon
find(".d-header #search-button").click
end
SEARCH_RESULT_SELECTOR = ".search-results .fps-result"
def has_search_result?
page.has_selector?(SEARCH_RESULT_SELECTOR)
end
def has_no_search_result?
page.has_no_selector?(SEARCH_RESULT_SELECTOR)
end
def has_warning_message?
page.has_selector?(".search-results .warning")
end
SEARCH_PAGE_SELECTOR = "body.search-page"
def active?
has_css?(SEARCH_PAGE_SELECTOR)
end
def not_active?
has_no_css?(SEARCH_PAGE_SELECTOR)
end
end
end
end