|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 |
| -require "qunit/selenium/test_runner" |
| 3 | +require "webdrivers" |
4 | 4 |
|
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 |
9 | 60 |
|
| 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 |
10 | 81 | driver_options = Selenium::WebDriver::Chrome::Options.new
|
11 | 82 | driver_options.add_argument("--headless")
|
12 | 83 | driver_options.add_argument("--disable-gpu")
|
13 | 84 | driver_options.add_argument("--no-sandbox")
|
14 | 85 |
|
15 |
| - driver = ::Selenium::WebDriver.for(:chrome, options: driver_options) |
| 86 | + ::Selenium::WebDriver.for(:chrome, options: driver_options) |
16 | 87 | end
|
17 | 88 |
|
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 | + |
19 | 97 | driver.quit
|
20 | 98 |
|
21 | 99 | 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 |
22 | 103 | exit(result.tests[:failed] > 0 ? 1 : 0)
|
0 commit comments