Skip to content

Commit e46bb63

Browse files
author
Landon Wilkins
committed
lint: discourage NoSuchElementError, fixes SD-1770
test plan: * see test commit Change-Id: Iab77491cdacf65249a8edd732617d7c771c0c78e Reviewed-on: https://gerrit.instructure.com/97276 Tested-by: Jenkins Reviewed-by: Shawn Meredith <shawn@instructure.com> Product-Review: Shawn Meredith <shawn@instructure.com> QA-Review: Shawn Meredith <shawn@instructure.com>
1 parent 21f0c8a commit e46bb63

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

gems/rubocop-canvas/config/default.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Specs/NoExecuteScript:
7070
Exclude:
7171
- "**/spec/selenium/test_setup/**/*.rb"
7272

73+
Specs/NoNoSuchElementError:
74+
<<: *Specs
75+
7376
Specs/NoStrftime:
7477
<<: *Specs
7578

gems/rubocop-canvas/lib/rubocop_canvas.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
require 'rubocop_canvas/cops/specs/no_before_once_stubs'
2929
require 'rubocop_canvas/cops/specs/ensure_spec_extension'
3030
require 'rubocop_canvas/cops/specs/no_execute_script'
31+
require 'rubocop_canvas/cops/specs/no_no_such_element_error'
3132
require 'rubocop_canvas/cops/specs/no_strftime'
3233
require 'rubocop_canvas/cops/specs/prefer_f_over_fj'
3334
require 'rubocop_canvas/cops/specs/scope_helper_modules'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module RuboCop
2+
module Cop
3+
module Specs
4+
class NoNoSuchElementError < Cop
5+
MSG = "Avoid using Selenium::WebDriver::Error::NoSuchElementError.\n" \
6+
"Our finders (f/fj and ff/ffj) will wait up to the implicit wait" \
7+
" (just like find_element, etc), and will raise a" \
8+
" Selenium::WebDriver::Error::NoSuchElementError" \
9+
" (just like find_element, etc).\n" \
10+
"Look through custom_selenium_rspec_matchers.rb, particularly" \
11+
" contain_css and contain_jqcss."
12+
13+
BAD_CONST = "Selenium::WebDriver::Error::NoSuchElementError"
14+
BAD_CONST_MATCHER = BAD_CONST.split("::")
15+
.map { |name| ":#{name})" }
16+
.join(" ")
17+
18+
# (const
19+
# (const
20+
# (const
21+
# (const nil :Selenium) :WebDriver) :Error) :NoSuchElementError)
22+
def_node_matcher :bad_const?, <<-PATTERN
23+
(const
24+
(const
25+
(const
26+
(const nil #{BAD_CONST_MATCHER}
27+
PATTERN
28+
29+
def on_const(node)
30+
return unless bad_const?(node)
31+
add_offense node, :expression, MSG, :warning
32+
end
33+
end
34+
end
35+
end
36+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
describe RuboCop::Cop::Specs::NoNoSuchElementError do
2+
subject(:cop) { described_class.new }
3+
let(:msg_regex) { /Avoid using Selenium::WebDriver::Error::NoSuchElementError/ }
4+
5+
it 'disallows Selenium::WebDriver::Error::NoSuchElementError' do
6+
inspect_source(cop, %{
7+
describe "breaks all the things" do
8+
it 'is a bad spec' do
9+
Selenium::WebDriver::Error::NoSuchElementError
10+
end
11+
end
12+
})
13+
expect(cop.offenses.size).to eq(1)
14+
expect(cop.messages.first).to match(msg_regex)
15+
expect(cop.offenses.first.severity.name).to eq(:warning)
16+
end
17+
18+
it 'disallows rescuing Selenium::WebDriver::Error::NoSuchElementError' do
19+
inspect_source(cop, %{
20+
def not_found?
21+
find("#yar")
22+
false
23+
rescue Selenium::WebDriver::Error::NoSuchElementError
24+
true
25+
end
26+
})
27+
expect(cop.offenses.size).to eq(1)
28+
expect(cop.messages.first).to match(msg_regex)
29+
expect(cop.offenses.first.severity.name).to eq(:warning)
30+
end
31+
32+
it 'disallows raising Selenium::WebDriver::Error::NoSuchElementError' do
33+
inspect_source(cop, %{
34+
def not_found?
35+
a = find("#yar")
36+
return true if a
37+
raise Selenium::WebDriver::Error::NoSuchElementError
38+
end
39+
})
40+
expect(cop.offenses.size).to eq(1)
41+
expect(cop.messages.first).to match(msg_regex)
42+
expect(cop.offenses.first.severity.name).to eq(:warning)
43+
end
44+
end

0 commit comments

Comments
 (0)