spec/watirspec/elements/element_spec.rb in watir-6.11.0 vs spec/watirspec/elements/element_spec.rb in watir-6.12.0
- old
+ new
@@ -21,10 +21,23 @@
expect { Watir::Element.new(container, 1, 2, 3, 4) }.to raise_error(ArgumentError)
expect { Watir::Element.new(container, "foo") }.to raise_error(ArgumentError)
end
end
+ describe "#element_call" do
+ it 'handles exceptions when taking an action on a stale element' do
+ browser.goto WatirSpec.url_for('removed_element.html')
+
+ element = browser.div(id: "text").tap(&:exists?)
+
+ browser.refresh
+
+ expect(element).to be_stale
+ expect { element.text }.to_not raise_error
+ end
+ end
+
describe "#eq and #eql?" do
before { browser.goto WatirSpec.url_for("definition_lists.html") }
it "returns true if the two elements point to the same DOM element" do
a = browser.dl(id: "experience-list")
@@ -117,11 +130,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 21
+ expect(browser.fieldset.elements(xpath: ".//label").length).to eq 22
end
end
describe "#to_subtype" do
it "returns a CheckBox instance" do
@@ -189,47 +202,151 @@
end
end
describe "#visible?" do
it "returns true if the element is visible" do
- expect(browser.text_field(id: "new_user_email")).to be_visible
+ msg = /WARN Watir \[\"visible_element\"\]/
+ expect {
+ expect(browser.text_field(id: "new_user_email")).to be_visible
+ }.to output(msg).to_stdout_from_any_process
end
it "raises UnknownObjectException exception if the element does not exist" do
- expect { browser.text_field(id: "no_such_id").visible? }.to raise_unknown_object_exception
+ msg = /WARN Watir \[\"visible_element\"\]/
+ expect {
+ expect { browser.text_field(id: "no_such_id").visible? }.to raise_unknown_object_exception
+ }.to output(msg).to_stdout_from_any_process
end
it "raises UnknownObjectException exception if the element is stale" do
element = browser.text_field(id: "new_user_email").tap(&:exists?)
browser.refresh
expect(element).to be_stale
- expect { element.visible? }.to raise_unknown_object_exception
+ expect {
+ expect { element.visible? }.to raise_unknown_object_exception
+ }.to have_deprecated_stale_visible
end
it "returns true if the element has style='visibility: visible' even if parent has style='visibility: hidden'" do
- expect(browser.div(id: "visible_child")).to be_visible
+ msg = /WARN Watir \[\"visible_element\"\]/
+ expect {
+ expect(browser.div(id: "visible_child")).to be_visible
+ }.to output(msg).to_stdout_from_any_process
end
it "returns false if the element is input element where type eq 'hidden'" do
expect(browser.hidden(id: "new_user_interests_dolls")).to_not be_visible
end
it "returns false if the element has style='display: none;'" do
- expect(browser.div(id: 'changed_language')).to_not be_visible
+ msg = /WARN Watir \[\"visible_element\"\]/
+ expect {
+ expect(browser.div(id: 'changed_language')).to_not be_visible
+ }.to output(msg).to_stdout_from_any_process
end
it "returns false if the element has style='visibility: hidden;" do
- expect(browser.div(id: 'wants_newsletter')).to_not be_visible
+ msg = /WARN Watir \[\"visible_element\"\]/
+ expect {
+ expect(browser.div(id: 'wants_newsletter')).to_not be_visible
+ }
end
it "returns false if one of the parent elements is hidden" do
- expect(browser.div(id: 'hidden_parent')).to_not be_visible
+ msg = /WARN Watir \[\"visible_element\"\]/
+ expect {
+ expect(browser.div(id: 'hidden_parent')).to_not be_visible
+ }
end
end
+ describe "#exists?" do
+ before do
+ browser.goto WatirSpec.url_for('removed_element.html')
+ end
+
+ it "element from a collection returns false when it becomes stale" do
+ element = browser.divs(id: "text").first.tap(&:exists?)
+
+ browser.refresh
+
+ expect(element).to be_stale
+ expect(element).to_not exist
+ end
+
+ it "returns false when tag name does not match id" do
+ watir_element = browser.span(id: "text")
+ expect(watir_element).to_not exist
+ end
+ end
+
+ describe '#present?' do
+ before do
+ browser.goto(WatirSpec.url_for("wait.html"))
+ end
+
+ it 'returns true if the element exists and is visible' do
+ expect(browser.div(id: 'foo')).to be_present
+ end
+
+ it 'returns false if the element exists but is not visible' do
+ expect(browser.div(id: 'bar')).to_not be_present
+ end
+
+ it 'returns false if the element does not exist' do
+ expect(browser.div(id: 'should-not-exist')).to_not be_present
+ end
+
+ # TODO Refactor so that this returns true
+ it "returns false if the element is stale" do
+ element = browser.div(id: "foo").tap(&:exists?)
+
+ browser.refresh
+
+ expect(element).to be_stale
+
+ expect {
+ expect(element).to_not be_present
+ }.to have_deprecated_stale_present
+ end
+
+ # TODO Documents Current Behavior, but needs to be refactored/removed
+ it "returns true the second time if the element is stale" do
+ element = browser.div(id: "foo").tap(&:exists?)
+
+ browser.refresh
+
+ expect(element).to be_stale
+
+ expect {
+ expect(element).to_not be_present
+ }.to have_deprecated_stale_present
+ expect(element).to be_present
+ end
+
+ end
+
+ describe "#enabled?" do
+ before do
+ browser.goto(WatirSpec.url_for("forms_with_input_elements.html"))
+ end
+
+ it "returns true if the element is enabled" do
+ expect(browser.element(name: 'new_user_submit')).to be_enabled
+ end
+
+ it "returns false if the element is disabled" do
+ expect(browser.element(name: 'new_user_submit_disabled')).to_not be_enabled
+ end
+
+ it "raises UnknownObjectException if the element doesn't exist" do
+ expect { browser.element(name: "no_such_name").enabled? }.to raise_unknown_object_exception
+ end
+ end
+
describe "#exist?" do
context ":class locator" do
before do
browser.goto(WatirSpec.url_for("class_locator.html"))
end
@@ -337,10 +454,15 @@
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
end
+
+ it "returns false when tag name does not match id" do
+ watir_element = browser.span(id: "text")
+ expect(watir_element).to_not exist
+ end
end
describe '#send_keys' do
before(:each) do
@c = Selenium::WebDriver::Platform.mac? ? :command : :control
@@ -387,18 +509,96 @@
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
expect(h2.flash).to eq h2
end
+
+ it 'should keep the element background color after flashing' do
+ expect(h2.style('background-color')).to eq h2.flash(:rainbow).style('background-color')
+ expect(h1.style('background-color')).to eq h1.flash.style('background-color')
+ end
+
+ it 'should respond to preset symbols like :fast and :slow' do
+ expect(h1.flash(:rainbow)).to eq h1
+ expect(h2.flash(:slow)).to eq h2
+ expect(h1.flash(:fast)).to eq h1
+ expect(h2.flash(:long)).to eq h2
+ end
+
end
+ describe "#hover" do
+ not_compliant_on :internet_explorer, :safari do
+ it "should hover over the element" do
+ browser.goto WatirSpec.url_for('hover.html')
+ link = browser.a
+
+ expect(link.style("font-size")).to eq "10px"
+ link.hover
+ link.wait_until { |l| l.style("font-size") == "20px" }
+ expect(link.style("font-size")).to eq "20px"
+ end
+ end
+ end
+
+ describe "#inspect" do
+ before(:each) { browser.goto(WatirSpec.url_for("nested_iframes.html")) }
+
+ it "does displays specified element type" do
+ expect(browser.div.inspect).to include('Watir::Div')
+ end
+
+ it "does not display element type if not specified" do
+ element = browser.element(index: 4)
+ expect(element.inspect).to include('Watir::HTMLElement')
+ end
+
+ it "displays keyword if specified" do
+ element = browser.h3
+ element.keyword = 'foo'
+ expect(element.inspect).to include('keyword: foo')
+ end
+
+ it "does not display keyword if not specified" do
+ element = browser.h3
+ expect(element.inspect).to_not include('keyword')
+ end
+
+ it "locate is false when not located" do
+ element = browser.div(id: 'not_present')
+ expect(element.inspect).to include('located: false')
+ end
+
+ it "locate is true when located" do
+ element = browser.h3
+ element.exists?
+ expect(element.inspect).to include('located: true')
+ end
+
+ it "displays selector string for element from colection" do
+ elements = browser.frames
+ expect(elements.last.inspect).to include '{:tag_name=>"frame", :index=>-1}'
+ end
+
+ it "displays selector string for nested element" do
+ browser.goto(WatirSpec.url_for("wait.html"))
+ element = browser.div(index: 1).div(id: 'div2')
+ expect(element.inspect).to include '{:index=>1, :tag_name=>"div"} --> {:id=>"div2", :tag_name=>"div"}'
+ end
+
+ it "displays selector string for nested element under frame" do
+ element = browser.iframe(id: 'one').iframe(id: 'three')
+ expect(element.inspect).to include '{:id=>"one", :tag_name=>"iframe"} --> {:id=>"three", :tag_name=>"iframe"}'
+ end
+ end
+
describe '#text_content' do
it 'returns inner Text code of element' do
browser.goto WatirSpec.url_for('non_control_elements.html')
expect(browser.div(id: 'shown').text_content).to eq('Not shownNot hidden')
end
@@ -491,6 +691,17 @@
expect(center['y']).to be > 0.0
expect(center['x']).to be > 0.0
end
end
+ describe '#attribute_value' do
+ before { browser.goto WatirSpec.url_for("data_attributes.html") }
+
+ it 'returns attribute value by string attribute name' do
+ expect(browser.p.attribute_value('data-type')).to eq "ruby-library"
+ end
+
+ it 'returns attribute value by symbol attribute name' do
+ expect(browser.p.attribute_value(:data_type)).to eq "ruby-library"
+ end
+ end
end