spec/watirspec/elements/element_spec.rb in watir-6.14.0 vs spec/watirspec/elements/element_spec.rb in watir-6.15.0

- old
+ new

@@ -85,25 +85,32 @@ describe 'visible text' do it 'finds elements by visible text' do browser.goto WatirSpec.url_for('non_control_elements.html') - expect(browser.link(visible_text: 'all visible')).to exist - expect(browser.link(visible_text: /all visible/)).to exist - expect(browser.link(visible_text: 'some visible')).to exist - expect(browser.link(visible_text: /some visible/)).to exist - expect(browser.link(visible_text: 'none visible')).not_to exist - expect(browser.link(visible_text: /none visible/)).not_to exist - - expect(browser.link(visible_text: 'Link 2', class: 'external')).to exist - expect(browser.link(visible_text: /Link 2/, class: 'external')).to exist - expect(browser.element(visible_text: 'all visible')).to exist expect(browser.element(visible_text: /all visible/)).to exist expect(browser.element(visible_text: 'some visible')).to exist expect(browser.element(visible_text: /some visible/)).to exist + expect(browser.element(visible_text: 'none visible')).not_to exist + expect(browser.element(visible_text: /none visible/)).not_to exist + + expect(browser.element(visible_text: 'Link 2', class: 'external')).to exist + expect(browser.element(visible_text: /Link 2/, class: 'external')).to exist end + + it 'raises exception unless value is a String or a RegExp' do + browser.goto WatirSpec.url_for('non_control_elements.html') + msg = /expected string_or_regexp, got 7\:(Fixnum|Integer)/ + expect { browser.element(visible_text: 7).exists? }.to raise_exception(TypeError, msg) + end + + it 'raises exception unless key is valid' do + browser.goto WatirSpec.url_for('non_control_elements.html') + msg = /Unable to build XPath using 7:(Fixnum|Integer)/ + expect { browser.element(7 => /foo/).exists? }.to raise_exception(Watir::Exception::Error, msg) + end end describe 'finding with unknown tag name' do it 'finds an element without arguments' do expect(browser.element).to exist @@ -129,11 +136,11 @@ expect(browser.fieldset.element(id: 'first_label')).to exist expect(browser.field_set.element(id: 'first_label')).to exist end it "finds several elements from an element's subtree" do - expect(browser.fieldset.elements(xpath: './/label').length).to eq 22 + expect(browser.fieldset.elements(xpath: './/label').length).to eq 23 end end describe '#to_subtype' do it 'returns a CheckBox instance' do @@ -263,11 +270,13 @@ element = browser.divs(id: 'text').first.tap(&:exists?) browser.refresh expect(element).to be_stale - expect(element).to_not exist + expect { + expect(element).to_not exist + }.to have_deprecated_stale_exists end it 'returns false when tag name does not match id' do watir_element = browser.span(id: 'text') expect(watir_element).to_not exist @@ -445,27 +454,33 @@ it "doesn't raise when called on nested elements" do expect(browser.div(id: 'no_such_div').link(id: 'no_such_id')).to_not exist end it 'raises if both :xpath and :css are given' do - expect { browser.div(xpath: '//div', css: 'div').exists? }.to raise_error(ArgumentError) + msg = ':xpath and :css cannot be combined ({:xpath=>"//div", :css=>"div"})' + expect { browser.div(xpath: '//div', css: 'div').exists? } + .to raise_exception Watir::Exception::LocatorException, msg end it "doesn't raise when selector has with :xpath has :index" do expect(browser.div(xpath: '//div', index: 1)).to exist end - it 'raises ArgumentError error if selector hash with :xpath has multiple entries' do - expect { browser.div(xpath: '//div', class: 'foo').exists? }.to raise_error(ArgumentError) + it 'raises LocatorException error if selector hash with :xpath has multiple entries' do + msg = 'xpath cannot be combined with all of these locators ({:class=>"foo", :tag_name=>"div"})' + expect { browser.div(xpath: '//div', class: 'foo').exists? } + .to raise_exception Watir::Exception::LocatorException, msg end it "doesn't raise when selector has with :css has :index" do expect(browser.div(css: 'div', index: 1)).to exist end - it 'raises ArgumentError error if selector hash with :css has multiple entries' do - expect { browser.div(css: 'div', class: 'foo').exists? }.to raise_error(ArgumentError) + it 'raises LocatorException error if selector hash with :css has multiple entries' do + msg = 'css cannot be combined with all of these locators ({:class=>"foo", :tag_name=>"div"})' + expect { browser.div(css: 'div', class: 'foo').exists? } + .to raise_exception Watir::Exception::LocatorException, msg end it 'finds element by Selenium name locator' do expect(browser.element(name: 'new_user_first_name')).to exist expect(browser.element(name: /new_user_first_name/)).to exist @@ -522,10 +537,24 @@ end end end end + describe '#click' do + bug 'https://github.com/mozilla/geckodriver/issues/1375', :firefox do + it 'accepts modifiers' do + begin + browser.a.click(:shift) + expect(browser.windows.size).to eq 2 + ensure + browser.windows.reject(&:current?).each(&:close) + expect(browser.windows.size).to eq 1 + end + end + end + end + describe '#flash' do let(:h2) { browser.h2(text: 'Add user') } let(:h1) { browser.h1(text: 'User administration') } it 'returns the element on which it was called' do @@ -641,10 +670,19 @@ text = '<div id="shown"><div id="hidden" style="display: none;">Not shown</div><div>Not hidden</div></div>' expect(div.outer_html).to eq text end end + describe '#select_text and #selected_text' do + it 'selects text and returns selected text' do + browser.goto WatirSpec.url_for('non_control_elements.html') + element = browser.element(visible_text: 'all visible') + element.select_text('all visible') + expect(element.selected_text).to eq 'all visible' + end + end + not_compliant_on %i[remote firefox] do describe '#scroll_into_view' do it 'scrolls element into view' do el = browser.button(name: 'new_user_image') element_center = el.center['y'] @@ -767,8 +805,130 @@ describe '#wd' do it 'returns a Selenium::WebDriver::Element instance' do element = browser.text_field(id: 'new_user_email') expect(element.wd).to be_a(Selenium::WebDriver::Element) + end + end + + describe '#hash' do + it 'returns a hash' do + element = browser.text_field(id: 'new_user_email') + hash1 = element.hash + hash2 = element.locate.hash + expect(hash1).to be_a Integer + expect(hash2).to be_a Integer + expect(hash1).to_not eq hash2 + end + end + + describe 'Float Attribute' do + it 'returns Float value of applicable element' do + element = browser.text_field(id: 'number') + expect(element.valueasnumber).to be_a Float + end + + it 'returns nil value for an inapplicable Element' do + element = browser.input + expect(element.valueasnumber).to be_nil + end + end + + describe 'Integer Attribute' do + it 'returns Float value of applicable element' do + element = browser.form + expect(element.length).to be_a Integer + end + + it 'returns -1 for an inapplicable Element' do + element = browser.input + expect(element.maxlength).to eq(-1) + end + end + + describe '#class_name' do + it 'returns single class name' do + expect(browser.form(id: 'new_user').class_name).to eq 'user' + end + + it 'returns multiple class names in a String' do + expect(browser.div(id: 'messages').class_name).to eq 'multiple classes here' + end + + it 'returns an empty string if the element exists but there is no class attribute' do + expect(browser.div(id: 'changed_language').class_name).to eq '' + end + + it 'raises UnknownObjectException if the element does not exist' do + expect { browser.div(id: 'no_such_id').class_name }.to raise_unknown_object_exception + end + end + + describe '#classes' do + it 'returns the class attribute if the element exists' do + expect(browser.div(id: 'messages').classes).to eq %w[multiple classes here] + end + + it 'returns an empty array if the element exists but there is no class attribute' do + expect(browser.div(id: 'changed_language').classes).to eq [] + end + + it 'raises UnknownObjectException if the element does not exist' do + expect { browser.div(id: 'no_such_id').classes }.to raise_unknown_object_exception + end + end + + describe '#obscured?' do + before { browser.goto WatirSpec.url_for('obscured.html') } + + it 'returns false if element\'s center is not covered' do + btn = browser.button(id: 'not_obscured') + expect(btn).not_to be_obscured + expect { btn.click }.not_to raise_exception + end + + it 'returns false if element\'s center is covered by its descendant' do + btn = browser.button(id: 'has_descendant') + expect(btn).not_to be_obscured + expect { btn.click }.not_to raise_exception + end + + it 'returns true if element\'s center is covered by a non-descendant' do + btn = browser.button(id: 'obscured') + expect(btn).to be_obscured + not_compliant_on :chrome do + expect { btn.click }.to raise_exception(Selenium::WebDriver::Error::ElementClickInterceptedError) + end + compliant_on :chrome do + expect { btn.click }.to raise_exception(Selenium::WebDriver::Error::UnknownError) + end + end + + it 'returns false if element\'s center is surrounded by non-descendants' do + btn = browser.button(id: 'surrounded') + expect(btn).not_to be_obscured + expect { btn.click }.not_to raise_exception + end + + it 'scrolls element into view before checking if obscured' do + btn = browser.button(id: 'requires_scrolling') + expect(btn).not_to be_obscured + expect { btn.click }.not_to raise_exception + end + + it 'returns true if element cannot be scrolled into view' do + btn = browser.button(id: 'off_screen') + expect(btn).to be_obscured + expect { btn.click }.to raise_unknown_object_exception + end + + it 'returns true if element is hidden' do + btn = browser.button(id: 'hidden') + expect(btn).to be_obscured + expect { btn.click }.to raise_unknown_object_exception + end + + it 'raises UnknownObjectException if element does not exist' do + expect { browser.button(id: 'does_not_exist').obscured? }.to raise_unknown_object_exception end end end