Skip to content

Commit

Permalink
Refer to Capybara.current_session by document
Browse files Browse the repository at this point in the history
The Capybara DSL provides a `page` helper method to refer to the
Capybara.current_session for assertions about the current document.
Unfortunately as I have chosen a key application model to be `Page`, I
am often shadowing the Capybara.current_session helper in system specs
with the local variable `page` to refer to the domain concept. Using
them interchangeably doesn’t work.

In this changeset, I have added a separate alias to
Capybara.current_sesison for system specs called `document`. I’m not
enforcing its usage as Capybara appears to use the `page` DSL method
internally.
  • Loading branch information
rossta committed Dec 8, 2024
1 parent e10809e commit 71758cf
Show file tree
Hide file tree
Showing 42 changed files with 209 additions and 213 deletions.
2 changes: 1 addition & 1 deletion lib/generators/article/templates/article_spec.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ RSpec.describe "<%= title %>", type: :system do
it "displays the article content" do
visit "/articles/<%= article_file_name %>"

expect(page).to have_content <%= title %>
expect(document).to have_content <%= title %>
end
end
12 changes: 6 additions & 6 deletions spec/requests/feed_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
get "/feed"

expect(response.status).to eq(200)
page = Capybara.string(response.body)
Capybara.string(response.body)

expect(page).to have_content("Introducing Joy of Rails")
expect(page).to have_content("/introducing-joy-of-rails")
expect(page).to have_content("How it started, How it’s going</h2>")
expect(page).to have_content(%(<div class="code-wrapper highlight language-ruby"><pre><code><span class="k">class</span>))
expect(page).not_to have_content(%(<turbo-frame))
expect(document).to have_content("Introducing Joy of Rails")
expect(document).to have_content("/introducing-joy-of-rails")
expect(document).to have_content("How it started, How it’s going</h2>")
expect(document).to have_content(%(<div class="code-wrapper highlight language-ruby"><pre><code><span class="k">class</span>))
expect(document).not_to have_content(%(<turbo-frame))
end

it "render valid feed" do
Expand Down
16 changes: 8 additions & 8 deletions spec/requests/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@

expect(response).to have_http_status(:success)

expect(page).not_to have_content("No results")
expect(document).not_to have_content("No results")
end

it "renders empty without search query as POST" do
post search_path

expect(response).to have_http_status(:success)

expect(page).not_to have_content("No results")
expect(document).not_to have_content("No results")
end

it "renders empty without search query as turbo stream" do
get search_path(format: :turbo_stream)

expect(response).to have_http_status(:success)

expect(page).not_to have_content("No results")
expect(document).not_to have_content("No results")
end

it "renders No results feedback when query is long enough" do
get search_path, params: {query: "Pro"}

expect(response).to have_http_status(:success)

expect(page).to have_content("No results")
expect(document).to have_content("No results")
end

it "renders the search results without query" do
Expand All @@ -42,7 +42,7 @@

expect(response).to have_http_status(:success)

expect(page).not_to have_content("No results")
expect(document).not_to have_content("No results")
end

it "renders the search results with query as turbo stream" do
Expand Down Expand Up @@ -73,15 +73,15 @@

expect(response).to have_http_status(:success)

expect(page).to have_content("Progressive Web Apps on Rails Showcase")
expect(page).not_to have_content("Introducing Joy of Rails")
expect(document).to have_content("Progressive Web Apps on Rails Showcase")
expect(document).not_to have_content("Introducing Joy of Rails")
end

it "doesn’t blow up with invalid query" do
get search_path, params: {query: "(((("}

expect(response).to have_http_status(:success)
expect(page).to have_content("No results")
expect(document).to have_content("No results")
end

it "doesn’t blow up with invalid query as turbo stream" do
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/share/polls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
it "does not render the New Poll button when not allowed" do
get share_polls_url

expect(page).to_not have_content("New Poll")
expect(document).not_to have_content("New Poll")
end

it "renders the New Poll button when allowed" do
Flipper.enable(:polls, login_as_user)
get share_polls_url

expect(page).to have_content("New Poll")
expect(document).to have_content("New Poll")
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/requests/share/snippets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
it "does not render the New Snippet button when not allowed" do
get share_snippets_url

expect(page).to_not have_content("New Snippet")
expect(document).not_to have_content("New Snippet")
end

it "renders the New Snippet button when allowed" do
Flipper.enable(:snippets, login_as_user)
get share_snippets_url

expect(page).to have_content("New Snippet")
expect(document).to have_content("New Snippet")
end
end

Expand Down Expand Up @@ -58,7 +58,7 @@
snippet = FactoryBot.create(:snippet)
get share_snippet_url(snippet)

expect(page).to_not have_content("Edit this snippet")
expect(document).not_to have_content("Edit this snippet")
end

it "renders the Edit Snippet button when allowed" do
Expand All @@ -67,7 +67,7 @@
snippet = FactoryBot.create(:snippet, author: user)
get share_snippet_url(snippet)

expect(page).to have_content("Edit this snippet")
expect(document).to have_content("Edit this snippet")
end
end
end
4 changes: 2 additions & 2 deletions spec/requests/topics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

expect(response).to have_http_status(:success)

expect(page).to have_content(topic_1.name)
expect(page).to have_content(topic_2.name)
expect(document).to have_content(topic_1.name)
expect(document).to have_content(topic_2.name)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/support/requests.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module RequestSpecHelpers
def page
@page ||= Capybara.string(response.body)
def document
@document ||= Capybara.string(response.body)
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/support/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,18 @@
Capybara.default_max_wait_time = 5
Capybara.disable_animation = true

module CapybaraHelper
extend ActiveSupport::Concern

# Use the current session as an alternative to `page`
def document = Capybara.current_session
end

RSpec.configure do |config|
config.include Warden::Test::Helpers, type: :system
config.include Rails.application.routes.url_helpers, type: :system
config.include ActiveJob::TestHelper
config.include CapybaraHelper, type: :system

config.before(:each, type: :system) do
driven_by(:cuprite,
Expand Down
14 changes: 7 additions & 7 deletions spec/system/admin/newsletter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

click_button "Create Newsletter"

expect(page).to have_content("Newsletter was successfully created.")
expect(document).to have_content("Newsletter was successfully created.")

expect(page).to have_content("Welcome to Joy of Rails!")
expect(document).to have_content("Welcome to Joy of Rails!")

click_link "Back to newsletters"

expect(page).to have_content("Welcome to Joy of Rails!")
expect(document).to have_content("Welcome to Joy of Rails!")

within "#newsletters" do
click_link "Edit"
Expand All @@ -38,9 +38,9 @@

click_button "Update Newsletter"

expect(page).to have_content("Newsletter was successfully updated.")
expect(document).to have_content("Newsletter was successfully updated.")

expect(page).to have_content("OMG! This is the first newsletter.")
expect(document).to have_content("OMG! This is the first newsletter.")
end

it "send a newsletter test", :vcr do
Expand All @@ -58,7 +58,7 @@

click_button "Send Test"

expect(page).to have_content("[TEST] Newsletter was successfully delivered.")
expect(document).to have_content("[TEST] Newsletter was successfully delivered.")

perform_enqueued_jobs_and_subsequently_enqueued_jobs

Expand Down Expand Up @@ -88,7 +88,7 @@

click_button "Send Live"

expect(page).to have_content("[LIVE] Newsletter was successfully delivered.")
expect(document).to have_content("[LIVE] Newsletter was successfully delivered.")

perform_enqueued_jobs_and_subsequently_enqueued_jobs

Expand Down
16 changes: 8 additions & 8 deletions spec/system/admin_users/sessions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
admin_user = FactoryBot.create(:admin_user)

visit new_admin_users_session_path
expect(page).to have_text("Sign in to your admin account")
expect(document).to have_text("Sign in to your admin account")

fill_in "Email", with: admin_user.email
fill_in "Password", with: "password"

click_button "Sign in"

expect(page).not_to have_text("Sign in to your admin account")
expect(document).not_to have_text("Sign in to your admin account")

expect(page).to have_text("Signed in successfully.")
expect(document).to have_text("Signed in successfully.")

expect(page).to have_text(admin_user.email)
expect(document).to have_text(admin_user.email)
end

it "fails sign in" do
Expand All @@ -30,7 +30,7 @@
click_button "Sign in"

expect(current_path).to eq(admin_users_sessions_path)
expect(page).to have_text("Incorrect email or password.")
expect(document).to have_text("Incorrect email or password.")
end

it "signs out admin_user" do
Expand All @@ -40,11 +40,11 @@

visit root_path

expect(page).to have_text(admin_user.email)
expect(document).to have_text(admin_user.email)

click_button "Sign out"

expect(page).to have_text("Signed out successfully.")
expect(page).not_to have_text(admin_user.email)
expect(document).to have_text("Signed out successfully.")
expect(document).not_to have_text(admin_user.email)
end
end
20 changes: 10 additions & 10 deletions spec/system/articles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
it "displays articles" do
visit "/articles"

expect(page).to have_content("Articles")
expect(document).to have_content("Articles")
end

it "displays a single article" do
Expand All @@ -15,7 +15,7 @@
click_on article.title

within("header.page-header") do
expect(page).to have_content(article.title)
expect(document).to have_content(article.title)
end
end

Expand All @@ -30,27 +30,27 @@
visit article.request_path

within("header.page-header") do
expect(page).to have_content(article.title)
expect(document).to have_content(article.title)
end

expect(article.topics.count).to eq(4)
expect(article.topics.approved.count).to eq(2)

article.topics.approved.each do |topic|
expect(page).to have_link(topic.slug, href: topic_path(topic))
expect(document).to have_link(topic.slug, href: topic_path(topic))
end
(article.topics.rejected + article.topics.pending).each do |topic|
expect(page).not_to have_link(topic.slug, href: topic_path(topic))
expect(document).not_to have_link(topic.slug, href: topic_path(topic))
end

first_topic = article.topics.first
click_link first_topic.slug

within("header.page-header") do
expect(page).to have_content("Topics")
expect(document).to have_content("Topics")
end
expect(page).to have_content(first_topic.name)
expect(page).to have_content(article.title)
expect(document).to have_content(first_topic.name)
expect(document).to have_content(article.title)
end

it "displays similar articles" do
Expand All @@ -65,7 +65,7 @@

visit article.request_path

expect(page).to have_content("More articles to enjoy")
expect(page).to have_link(similar.title, href: similar.request_path)
expect(document).to have_content("More articles to enjoy")
expect(document).to have_link(similar.title, href: similar.request_path)
end
end
14 changes: 7 additions & 7 deletions spec/system/author/polls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

visit author_poll_path(poll)

expect(page).to have_content("Color poll")
expect(page).to have_content("What is your favorite color?")
expect(page).to have_content("Red")
expect(page).to have_content("Blue")
expect(page).to have_content("Green")
expect(document).to have_content("Color poll")
expect(document).to have_content("What is your favorite color?")
expect(document).to have_content("Red")
expect(document).to have_content("Blue")
expect(document).to have_content("Green")

click_link "+ New question"
fill_in "Body", with: "What is your favorite animal?"
click_button "Save question"
expect(page).to have_content("What is your favorite animal?")
expect(document).to have_content("What is your favorite animal?")

last_question = Polls::Question.last
expect(last_question.body).to eq("What is your favorite animal?")
Expand All @@ -34,7 +34,7 @@
fill_in "Body", with: "Dog"
click_button "Save answer"

expect(page).to have_content("Dog")
expect(document).to have_content("Dog")
end

last_answer = Polls::Answer.last
Expand Down
12 changes: 6 additions & 6 deletions spec/system/author/snippets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

click_button "Save"

expect(page).to have_content("Your snippet has been saved")
expect(document).to have_content("Your snippet has been saved")

snippet = Snippet.last
expect(snippet.source).to eq("class Blog\nend")
Expand All @@ -39,13 +39,13 @@

click_button "Save"

expect(page).to have_content("Your snippet has been saved")
expect(document).to have_content("Your snippet has been saved")

click_link "Author snippets"

expect(page).to have_content("lib/models/blog.rb")
expect(page).to have_content("class Blog\n has_many :posts\nend")
expect(page).to have_content("A Ruby class")
expect(page).to have_content("This is how you do it")
expect(document).to have_content("lib/models/blog.rb")
expect(document).to have_content("class Blog\n has_many :posts\nend")
expect(document).to have_content("A Ruby class")
expect(document).to have_content("This is how you do it")
end
end
Loading

0 comments on commit 71758cf

Please sign in to comment.