Skip to content

Commit ee99c89

Browse files
authored
Merge pull request rails#42513 from ghiculescu/ci-ujs-tests
Output UJs test failures in CI
2 parents 3a98e7d + 895a78d commit ee99c89

File tree

3 files changed

+88
-12
lines changed

3 files changed

+88
-12
lines changed

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ gem "aws-sdk-sns", require: false
9595
gem "webmock"
9696

9797
group :ujs do
98-
gem "qunit-selenium"
9998
gem "webdrivers"
10099
end
101100

Gemfile.lock

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,6 @@ GEM
349349
puma (5.3.2)
350350
nio4r (~> 2.0)
351351
que (0.14.3)
352-
qunit-selenium (0.0.4)
353-
selenium-webdriver
354-
thor
355352
raabro (1.4.0)
356353
racc (1.5.2)
357354
rack (2.2.3)
@@ -568,7 +565,6 @@ DEPENDENCIES
568565
puma
569566
que
570567
queue_classic!
571-
qunit-selenium
572568
racc (>= 1.4.6)
573569
rack-cache (~> 1.2)
574570
rails!

ci/qunit-selenium-runner.rb

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,103 @@
11
# frozen_string_literal: true
22

3-
require "qunit/selenium/test_runner"
3+
require "webdrivers"
44

5-
if ARGV[1]
6-
driver = ::Selenium::WebDriver.for(:remote, url: ARGV[1], desired_capabilities: :chrome)
7-
else
8-
require "webdrivers"
5+
# This class based on https://github.com/smontanari/qunit-selenium, with a few tweaks to make it easier to read output.
6+
# The license from https://github.com/smontanari/qunit-selenium is enclosed:
7+
#
8+
# The MIT License (MIT)
9+
#
10+
# Copyright (c) 2014 Silvio Montanari
11+
#
12+
# Permission is hereby granted, free of charge, to any person obtaining a copy
13+
# of this software and associated documentation files (the "Software"), to deal
14+
# in the Software without restriction, including without limitation the rights
15+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
# copies of the Software, and to permit persons to whom the Software is
17+
# furnished to do so, subject to the following conditions:
18+
#
19+
# The above copyright notice and this permission notice shall be included in all
20+
# copies or substantial portions of the Software.
21+
#
22+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
# SOFTWARE.
29+
30+
class TestRun
31+
TestResult = Struct.new(:tests, :assertions, :duration, :raw_output)
32+
33+
ID_TESTRESULT = "qunit-testresult"
34+
ID_TESTS = "qunit-tests"
35+
36+
def initialize(driver)
37+
@qunit_testresult = driver[ID_TESTRESULT]
38+
@qunit_tests = driver[ID_TESTS]
39+
end
40+
41+
def completed?
42+
@qunit_testresult.text =~ /Tests completed/
43+
end
44+
45+
def result
46+
assertions = { total: total_assertions, passed: passed_assertions, failed: failed_assertions }
47+
tests = { total: total_tests, passed: pass_tests, failed: fail_tests }
48+
TestResult.new(tests, assertions, duration, raw_output)
49+
end
50+
51+
private
52+
def raw_output
53+
@qunit_tests.text
54+
end
55+
56+
def duration
57+
match = /Tests completed in (?<milliseconds>\d+) milliseconds/.match @qunit_testresult.text
58+
match[:milliseconds].to_i / 1000
59+
end
960

61+
%w(total passed failed).each do |result|
62+
define_method("#{result}_assertions".to_sym) do
63+
@qunit_testresult.find_elements(:class, result).first.text.to_i
64+
end
65+
end
66+
67+
def total_tests
68+
@qunit_tests.find_elements(:css, "##{ID_TESTS} > *").count
69+
end
70+
71+
%w(pass fail).each do |result|
72+
define_method("#{result}_tests".to_sym) do
73+
@qunit_tests.find_elements(:css, "##{ID_TESTS} > .#{result}").count
74+
end
75+
end
76+
end
77+
78+
driver = if ARGV[1]
79+
::Selenium::WebDriver.for(:remote, url: ARGV[1], desired_capabilities: :chrome)
80+
else
1081
driver_options = Selenium::WebDriver::Chrome::Options.new
1182
driver_options.add_argument("--headless")
1283
driver_options.add_argument("--disable-gpu")
1384
driver_options.add_argument("--no-sandbox")
1485

15-
driver = ::Selenium::WebDriver.for(:chrome, options: driver_options)
86+
::Selenium::WebDriver.for(:chrome, options: driver_options)
1687
end
1788

18-
result = QUnit::Selenium::TestRunner.new(driver).open(ARGV[0], timeout: 60)
89+
driver.get(ARGV[0])
90+
91+
result = TestRun.new(driver).tap do |run|
92+
::Selenium::WebDriver::Wait.new(timeout: 60).until do
93+
run.completed?
94+
end
95+
end.result
96+
1997
driver.quit
2098

2199
puts "Time: #{result.duration} seconds, Total: #{result.assertions[:total]}, Passed: #{result.assertions[:passed]}, Failed: #{result.assertions[:failed]}"
100+
if result.tests[:failed] > 0
101+
puts "Qunit output follows. Look for lines that have failures, eg (1, n, n) - those are your failing lines\r\n\r\n#{result.raw_output}"
102+
end
22103
exit(result.tests[:failed] > 0 ? 1 : 0)

0 commit comments

Comments
 (0)