diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-01-09 10:02:55 +0100 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-01-09 10:02:55 +0100 |
commit | 4b945e2845cf40456ef71faafbce181f0e60dba7 (patch) | |
tree | bfa83f4a89f8977a2ab5ea5d21507a6dc3407c95 /qa | |
parent | 489267e6dd10b9eac15f2aea4c1f3259cc5510f2 (diff) | |
download | gitlab-ce-4b945e2845cf40456ef71faafbce181f0e60dba7.tar.gz |
Make it possible to define a QA-specific page element
Diffstat (limited to 'qa')
-rw-r--r-- | qa/qa/page/element.rb | 19 | ||||
-rw-r--r-- | qa/qa/page/view.rb | 2 | ||||
-rw-r--r-- | qa/spec/page/element_spec.rb | 21 |
3 files changed, 23 insertions, 19 deletions
diff --git a/qa/qa/page/element.rb b/qa/qa/page/element.rb index 173fdbf6d36..af93a575cb6 100644 --- a/qa/qa/page/element.rb +++ b/qa/qa/page/element.rb @@ -3,21 +3,22 @@ module QA class Element attr_reader :name - def initialize(name, pattern) + def initialize(name, pattern = nil) @name = name - @pattern = pattern + @pattern = pattern || "qa-#{@name.to_s.gsub('_', '-')}" end - def matches?(line) - case @pattern - when Regexp - !!(line =~ @pattern) - when String - line.include?(@pattern) + def expression + if @pattern.is_a?(String) + @_regexp ||= Regexp.new(Regexp.escape(@pattern)) else - raise ArgumentError, 'Pattern should be either String or Regexp!' + @pattern end end + + def matches?(line) + !!(line =~ expression) + end end end end diff --git a/qa/qa/page/view.rb b/qa/qa/page/view.rb index fa0ed8be9d9..47658144783 100644 --- a/qa/qa/page/view.rb +++ b/qa/qa/page/view.rb @@ -46,7 +46,7 @@ module QA @elements = [] end - def element(name, pattern) + def element(name, pattern = nil) @elements.push(Page::Element.new(name, pattern)) end end diff --git a/qa/spec/page/element_spec.rb b/qa/spec/page/element_spec.rb index 238c4d1ac66..c665267b90e 100644 --- a/qa/spec/page/element_spec.rb +++ b/qa/spec/page/element_spec.rb @@ -2,11 +2,11 @@ describe QA::Page::Element do context 'when pattern is an expression' do subject { described_class.new(:something, /button 'Sign in'/) } - it 'is correctly matches against a string' do + it 'matches when there is a match' do expect(subject.matches?("button 'Sign in'")).to be true end - it 'does not match if string does not match against a pattern' do + it 'does not match if pattern is not present' do expect(subject.matches?("button 'Sign out'")).to be false end end @@ -14,21 +14,24 @@ describe QA::Page::Element do context 'when pattern is a string' do subject { described_class.new(:something, 'button') } - it 'is correctly matches against a string' do + it 'matches when there is match' do expect(subject.matches?('some button in the view')).to be true end - it 'does not match if string does not match against a pattern' do + it 'does not match if pattern is not present' do expect(subject.matches?('text_field :name')).to be false end end - context 'when pattern is not supported' do - subject { described_class.new(:something, [/something/]) } + context 'when pattern is not provided' do + subject { described_class.new(:some_name) } - it 'raises an error' do - expect { subject.matches?('some line') } - .to raise_error ArgumentError + it 'matches when QA specific selector is present' do + expect(subject.matches?('some qa-some-name selector')).to be true + end + + it 'does not match if QA selector is not there' do + expect(subject.matches?('some_name selector')).to be false end end end |