Skip to content

Commit

Permalink
Improve tests result consistency (#581)
Browse files Browse the repository at this point in the history
* Increase wait time for capybara
  • Loading branch information
ahangarha authored Jan 5, 2024
1 parent 0d87741 commit 0c254ee
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 52 deletions.
40 changes: 2 additions & 38 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,45 +66,9 @@
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!

# Capybara config
#
# selenium_firefox webdriver only works for Travis-CI builds.
default_driver = :selenium_chrome_headless

supported_drivers = %i[selenium_chrome_headless selenium_chrome selenium_firefox selenium]
driver = ENV["DRIVER"].try(:to_sym) || default_driver
Capybara.default_driver = driver

raise "Unsupported driver: #{driver} (supported = #{supported_drivers})" unless supported_drivers.include?(driver)

case driver
when :selenium_chrome
DriverRegistration.register_selenium_chrome

when :selenium_chrome_headless
DriverRegistration.register_selenium_chrome_headless

when :selenium_firefox, :selenium
DriverRegistration.register_selenium_firefox
driver = :selenium_firefox
end

Capybara.javascript_driver = driver
Capybara.default_driver = driver

Capybara.register_server(Capybara.javascript_driver) do |app, port|
require "rack/handler/puma"
Rack::Handler::Puma.run(app, Port: port)
end
Capybara.server = :puma

config.before(:each, type: :system, js: true) do
driven_by driver
driven_by :selenium, using: :chrome,
options: { args: %w[headless disable-gpu no-sandbox disable-dev-shm-usage] }
end
Capybara.default_driver = :selenium_chrome_headless
Capybara.javascript_driver = :selenium_chrome_headless

# Capybara.default_max_wait_time = 15
puts "=" * 80
puts "Capybara using driver: #{Capybara.javascript_driver}"
puts "=" * 80
Expand Down
9 changes: 9 additions & 0 deletions spec/rescript/rescript_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,35 @@
fill_in author_field, with: comment.author
fill_in text_field, with: comment.text
click_button("Post")

page.driver.browser.manage.timeouts.implicit_wait = 1

expect(Comment.all.count).to equal(new_comment_count)
end

it "comment count remains the same when author field is empty" do
initial_comment_count = Comment.all.count
fill_in text_field, with: comment.text
click_button("Post")

expect(page).to have_text(/Can't save the comment!/)
expect(Comment.all.count).to equal(initial_comment_count)
end

it "comment count remains the same when text field is empty" do
initial_comment_count = Comment.all.count
fill_in author_field, with: comment.author
click_button("Post")

expect(page).to have_text(/Can't save the comment!/)
expect(Comment.all.count).to equal(initial_comment_count)
end

it "comment count remains the same when both form fields are empty" do
initial_comment_count = Comment.all.count
click_button("Post")

expect(page).to have_text(/Can't save the comment!/)
expect(Comment.all.count).to equal(initial_comment_count)
end
end
Expand Down
21 changes: 11 additions & 10 deletions spec/stimulus/turbo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
end

describe "form submission functions" do
let(:comment) { Comment.new(author: "Author", text: "This is a comment") }
let(:comment) { Comment.new(author: "Author", text: "This is a comment #{Time.zone.now}") }
let(:author_field) { "comment_author" }
let(:author_error) { "Author: can't be blank" }
let(:text_field) { "comment_text" }
Expand All @@ -39,40 +39,41 @@
visit "/stimulus"
end

it "adds a new comment to the page" do
fill_in author_field, with: comment.author
fill_in text_field, with: comment.text
click_button("Post")
expect(page).to have_selector "h2", text: comment.author
end

it "comment count increases with successful form submission" do
it "adds a new comment to the page and database" do
initital_comment_count = Comment.all.count
new_comment_count = initital_comment_count + 1
fill_in author_field, with: comment.author
fill_in text_field, with: comment.text
click_button("Post")
sleep(1)

expect(page).to have_css("h2", text: comment.author)
expect(page).to have_css("p", text: comment.text)
expect(Comment.all.count).to equal(new_comment_count)
end

it "comment count remains the same when author field is empty" do
initial_comment_count = Comment.all.count
fill_in text_field, with: comment.text
click_button("Post")

expect(page).to have_text("Author: can't be blank")
expect(Comment.all.count).to equal(initial_comment_count)
end

it "comment count remains the same when text field is empty" do
initial_comment_count = Comment.all.count
fill_in author_field, with: comment.author
click_button("Post")

expect(page).to have_text("Text: can't be blank")
expect(Comment.all.count).to equal(initial_comment_count)
end

it "comment count remains the same when both form fields are empty" do
initial_comment_count = Comment.all.count
click_button("Post")

expect(page).to have_text("Author: can't be blank")
expect(Comment.all.count).to equal(initial_comment_count)
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/system/add_new_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "system/shared/contexts"

describe "Add new comment" do
context "when React Router", page: :main, js: true, type: :system do
context "when React Router", page: :main do
describe "with Horizontal Form" do
before do
visit root_path
Expand Down Expand Up @@ -61,7 +61,7 @@
end
end

context "when React/Redux", page: :react_demo, js: true, type: :system do
context "when React/Redux", page: :react_demo do
describe "with Horizontal Form" do
before do
visit root_path
Expand Down Expand Up @@ -118,7 +118,7 @@
end
end

context "when simple page", page: :simple, js: true, type: :system do
context "when simple page", page: :simple do
describe "with Horizontal Form" do
before do
visit root_path
Expand Down
2 changes: 1 addition & 1 deletion spec/system/react_router_demo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "rails_helper"
require "system/shared/contexts"

describe "React Router Routes", js: true, type: :system do
describe "React Router Routes" do
context "when Root URL", page: :main do
it "shows comments section" do
visit root_path
Expand Down

0 comments on commit 0c254ee

Please sign in to comment.