Skip to content

Commit 0c254ee

Browse files
authored
Improve tests result consistency (#581)
* Increase wait time for capybara
1 parent 0d87741 commit 0c254ee

File tree

5 files changed

+26
-52
lines changed

5 files changed

+26
-52
lines changed

spec/rails_helper.rb

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -66,45 +66,9 @@
6666
# https://relishapp.com/rspec/rspec-rails/docs
6767
config.infer_spec_type_from_file_location!
6868

69-
# Capybara config
70-
#
71-
# selenium_firefox webdriver only works for Travis-CI builds.
72-
default_driver = :selenium_chrome_headless
73-
74-
supported_drivers = %i[selenium_chrome_headless selenium_chrome selenium_firefox selenium]
75-
driver = ENV["DRIVER"].try(:to_sym) || default_driver
76-
Capybara.default_driver = driver
77-
78-
raise "Unsupported driver: #{driver} (supported = #{supported_drivers})" unless supported_drivers.include?(driver)
79-
80-
case driver
81-
when :selenium_chrome
82-
DriverRegistration.register_selenium_chrome
83-
84-
when :selenium_chrome_headless
85-
DriverRegistration.register_selenium_chrome_headless
86-
87-
when :selenium_firefox, :selenium
88-
DriverRegistration.register_selenium_firefox
89-
driver = :selenium_firefox
90-
end
91-
92-
Capybara.javascript_driver = driver
93-
Capybara.default_driver = driver
94-
95-
Capybara.register_server(Capybara.javascript_driver) do |app, port|
96-
require "rack/handler/puma"
97-
Rack::Handler::Puma.run(app, Port: port)
98-
end
99-
Capybara.server = :puma
100-
101-
config.before(:each, type: :system, js: true) do
102-
driven_by driver
103-
driven_by :selenium, using: :chrome,
104-
options: { args: %w[headless disable-gpu no-sandbox disable-dev-shm-usage] }
105-
end
69+
Capybara.default_driver = :selenium_chrome_headless
70+
Capybara.javascript_driver = :selenium_chrome_headless
10671

107-
# Capybara.default_max_wait_time = 15
10872
puts "=" * 80
10973
puts "Capybara using driver: #{Capybara.javascript_driver}"
11074
puts "=" * 80

spec/rescript/rescript_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,35 @@
5050
fill_in author_field, with: comment.author
5151
fill_in text_field, with: comment.text
5252
click_button("Post")
53+
54+
page.driver.browser.manage.timeouts.implicit_wait = 1
55+
5356
expect(Comment.all.count).to equal(new_comment_count)
5457
end
5558

5659
it "comment count remains the same when author field is empty" do
5760
initial_comment_count = Comment.all.count
5861
fill_in text_field, with: comment.text
5962
click_button("Post")
63+
64+
expect(page).to have_text(/Can't save the comment!/)
6065
expect(Comment.all.count).to equal(initial_comment_count)
6166
end
6267

6368
it "comment count remains the same when text field is empty" do
6469
initial_comment_count = Comment.all.count
6570
fill_in author_field, with: comment.author
6671
click_button("Post")
72+
73+
expect(page).to have_text(/Can't save the comment!/)
6774
expect(Comment.all.count).to equal(initial_comment_count)
6875
end
6976

7077
it "comment count remains the same when both form fields are empty" do
7178
initial_comment_count = Comment.all.count
7279
click_button("Post")
80+
81+
expect(page).to have_text(/Can't save the comment!/)
7382
expect(Comment.all.count).to equal(initial_comment_count)
7483
end
7584
end

spec/stimulus/turbo_spec.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
end
3030

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

42-
it "adds a new comment to the page" do
43-
fill_in author_field, with: comment.author
44-
fill_in text_field, with: comment.text
45-
click_button("Post")
46-
expect(page).to have_selector "h2", text: comment.author
47-
end
48-
49-
it "comment count increases with successful form submission" do
42+
it "adds a new comment to the page and database" do
5043
initital_comment_count = Comment.all.count
5144
new_comment_count = initital_comment_count + 1
5245
fill_in author_field, with: comment.author
5346
fill_in text_field, with: comment.text
5447
click_button("Post")
55-
sleep(1)
48+
49+
expect(page).to have_css("h2", text: comment.author)
50+
expect(page).to have_css("p", text: comment.text)
5651
expect(Comment.all.count).to equal(new_comment_count)
5752
end
5853

5954
it "comment count remains the same when author field is empty" do
6055
initial_comment_count = Comment.all.count
6156
fill_in text_field, with: comment.text
6257
click_button("Post")
58+
59+
expect(page).to have_text("Author: can't be blank")
6360
expect(Comment.all.count).to equal(initial_comment_count)
6461
end
6562

6663
it "comment count remains the same when text field is empty" do
6764
initial_comment_count = Comment.all.count
6865
fill_in author_field, with: comment.author
6966
click_button("Post")
67+
68+
expect(page).to have_text("Text: can't be blank")
7069
expect(Comment.all.count).to equal(initial_comment_count)
7170
end
7271

7372
it "comment count remains the same when both form fields are empty" do
7473
initial_comment_count = Comment.all.count
7574
click_button("Post")
75+
76+
expect(page).to have_text("Author: can't be blank")
7677
expect(Comment.all.count).to equal(initial_comment_count)
7778
end
7879
end

spec/system/add_new_comment_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require "system/shared/contexts"
55

66
describe "Add new comment" do
7-
context "when React Router", page: :main, js: true, type: :system do
7+
context "when React Router", page: :main do
88
describe "with Horizontal Form" do
99
before do
1010
visit root_path
@@ -61,7 +61,7 @@
6161
end
6262
end
6363

64-
context "when React/Redux", page: :react_demo, js: true, type: :system do
64+
context "when React/Redux", page: :react_demo do
6565
describe "with Horizontal Form" do
6666
before do
6767
visit root_path
@@ -118,7 +118,7 @@
118118
end
119119
end
120120

121-
context "when simple page", page: :simple, js: true, type: :system do
121+
context "when simple page", page: :simple do
122122
describe "with Horizontal Form" do
123123
before do
124124
visit root_path

spec/system/react_router_demo_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require "rails_helper"
44
require "system/shared/contexts"
55

6-
describe "React Router Routes", js: true, type: :system do
6+
describe "React Router Routes" do
77
context "when Root URL", page: :main do
88
it "shows comments section" do
99
visit root_path

0 commit comments

Comments
 (0)