require File.expand_path("spec_helper", File.dirname(__FILE__)) describe Watir::ElementLocator do include LocatorSpecHelper describe "finds a single element" do describe "by delegating to webdriver" do WEBDRIVER_SELECTORS.each do |loc| it "delegates to webdriver's #{loc} locator" do expect_one(loc, "bar").and_return(element(:tag_name => "div")) locate_one loc => "bar" end end end describe "with selectors not supported by webdriver" do it "handles selector with tag name and a single attribute" do if Watir.prefer_css? expect_one :css, 'div[title="foo"]' else expect_one :xpath, ".//div[@title='foo']" end locate_one :tag_name => "div", :title => "foo" end it "handles selector with no tag name and and a single attribute" do if Watir.prefer_css? expect_one :css, '[title="foo"]' else expect_one :xpath, ".//*[@title='foo']" end locate_one :title => "foo" end it "handles single quotes in the attribute string" do if Watir.prefer_css? expect_one :css, %{[title="foo and 'bar'"]} else expect_one :xpath, %{.//*[@title=concat('foo and ',"'",'bar',"'",'')]} end locate_one :title => "foo and 'bar'" end it "handles selector with tag name and multiple attributes" do if Watir.prefer_css? expect_one :css, 'div[title="foo"][dir="bar"]' else expect_one :xpath, ".//div[@title='foo' and @dir='bar']" end locate_one [:tag_name, "div", :title , "foo", :dir , 'bar'] end it "handles selector with no tag name and multiple attributes" do if Watir.prefer_css? expect_one :css, '[dir="foo"][title="bar"]' else expect_one :xpath, ".//*[@dir='foo' and @title='bar']" end locate_one [:dir, "foo", :title, "bar"] end end describe "with special cased selectors" do it "normalizes space for :text" do expect_one :xpath, ".//div[normalize-space()='foo']" locate_one :tag_name => "div", :text => "foo" end it "translates :caption to :text" do expect_one :xpath, ".//div[normalize-space()='foo']" locate_one :tag_name => "div", :caption => "foo" end it "translates :class_name to :class" do if Watir.prefer_css? expect_one :css, "div.foo" else expect_one :xpath, ".//div[contains(concat(' ', @class, ' '), ' foo ')]" end locate_one :tag_name => "div", :class_name => "foo" end it "handles data-* attributes" do if Watir.prefer_css? expect_one :css, 'div[data-name="foo"]' else expect_one :xpath, ".//div[@data-name='foo']" end locate_one :tag_name => "div", :data_name => "foo" end it "normalizes space for the :href attribute" do if Watir.prefer_css? expect_one :css, 'a[href~="foo"]' else expect_one :xpath, ".//a[normalize-space(@href)='foo']" end selector = { :tag_name => "a", :href => "foo" } locate_one selector, Watir::Anchor.attributes end it "wraps :type attribute with translate() for upper case values" do translated_type = "translate(@type,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" expect_one :xpath, ".//input[#{translated_type}='file']" selector = [ :tag_name, "input", :type , "file", ] locate_one selector, Watir::Input.attributes end it "uses the corresponding