diff options
author | ddavison <ddavison@gitlab.com> | 2019-08-27 13:32:09 -0700 |
---|---|---|
committer | ddavison <ddavison@gitlab.com> | 2019-08-27 13:32:09 -0700 |
commit | b4c289fcfef2d57d24a8f2f7d8cc4875f0a4860d (patch) | |
tree | 979482ab517d06de370898ab22bdb6ec6c209ab0 | |
parent | de385eee8507a35f1c01ca784ba9aab9bc38e11d (diff) | |
download | gitlab-ce-qa-extensible-data-qa-selectors.tar.gz |
Add extensible data-qa-* selection mechanismsqa-extensible-data-qa-selectors
Primarily this will prevent the necessity to match
on text. By providing this mechanism, we add the
ability to match on dynamic content
-rw-r--r-- | qa/qa/page/element.rb | 10 | ||||
-rw-r--r-- | qa/spec/page/element_spec.rb | 15 |
2 files changed, 24 insertions, 1 deletions
diff --git a/qa/qa/page/element.rb b/qa/qa/page/element.rb index 9e6fd2fdd4f..796caa7745a 100644 --- a/qa/qa/page/element.rb +++ b/qa/qa/page/element.rb @@ -28,7 +28,7 @@ module QA end def selector_css - %Q([data-qa-selector="#{@name}"],.#{selector}) + %Q([data-qa-selector="#{@name}"]#{additional_selectors},.#{selector}) end def expression @@ -42,6 +42,14 @@ module QA def matches?(line) !!(line =~ /["']#{name}['"]|#{expression}/) end + + private + + def additional_selectors + @attributes.dup.delete_if { |a| a == :pattern || a == :required }.map do |k, v| + %Q([data-qa-#{k}="#{v}"]) + end.join + end end end end diff --git a/qa/spec/page/element_spec.rb b/qa/spec/page/element_spec.rb index 20d4a00c020..5a61c3ad453 100644 --- a/qa/spec/page/element_spec.rb +++ b/qa/spec/page/element_spec.rb @@ -113,8 +113,23 @@ describe QA::Page::Element do describe 'data-qa selectors' do subject { described_class.new(:my_element) } + it 'properly translates to a data-qa-selector' do expect(subject.selector_css).to include(%q([data-qa-selector="my_element"])) end + + context 'additional selectors' do + let(:element) { described_class.new(:my_element, index: 3) } + let(:required_element) { described_class.new(:my_element, required: true, index: 3) } + + it 'matches on additional data-qa properties' do + expect(element.selector_css).to include(%q([data-qa-selector="my_element"][data-qa-index="3"])) + end + + it 'doesnt conflict with element requirement' do + expect(required_element).to be_required + expect(required_element.selector_css).not_to include(%q(data-qa-required)) + end + end end end |