class Locator
A locator is used to find elements in the DOM.
You can use the following convenience methods to create locators:
Locator.css("main#content")
Locator.xpath("//main[@id='content']")
Locator.link_text("Home")
Locator.partial_link_text("Ho")
Locator.tag_name("main")
You can also use the Locator.wrap
method to create locators from a hash:
Locator.wrap(css: "main#content")
Locator.wrap(xpath: "//main[@id='content']")
Locator.wrap(link_text: "Home")
Locator.wrap(partial_link_text: "Ho")
Locator.wrap(tag_name: "main")
For more information, see: https://w3c.github.io/webdriver/#locator-strategies.
Definitions
def self.wrap(locator = nil, **options)
A convenience wrapper for specifying locators.
You may provide either:
- A locator instance, or
- A single option
css:
,xpath:
,link_text:
,partial_link_text:
ortag_name:
, or - A
using:
andvalue:
option which will be used directly.
Signature
-
parameter
locator
Locator
A locator to use directly.
Implementation
def self.wrap(locator = nil, **options)
if locator.is_a?(Locator)
locator
elsif css = options[:css]
css(css)
elsif xpath = options[:xpath]
xpath(xpath)
elsif link_text = options[:link_text]
link_text(link_text)
elsif partial_link_text = options[:partial_link_text]
partial_link_text(partial_link_text)
elsif tag_name = options[:tag_name]
tag_name(tag_name)
elsif using = options[:using]
new(using, options[:value])
else
raise ArgumentError, "Unable to interpret #{locator.inspect} with #{options.inspect}!"
end
end
def self.css(css)
A convenience wrapper for specifying CSS locators.
Implementation
def self.css(css)
new("css selector", css)
end
def self.link_text(text)
A convenience wrapper for specifying link text locators.
Implementation
def self.link_text(text)
new("link text", text)
end
def self.partial_link_text(text)
A convenience wrapper for specifying partial link text locators.
Implementation
def self.partial_link_text(text)
new("partial link text", text)
end
def self.tag_name(name)
A convenience wrapper for specifying tag name locators.
Implementation
def self.tag_name(name)
new("tag name", name)
end
def self.xpath(xpath)
A convenience wrapper for specifying XPath locators.
Implementation
def self.xpath(xpath)
new("xpath", xpath)
end
def initialize(using, value)
Initialize the locator.
A locator strategy must usually be one of the following:
css selector
: Used to find elements via CSS selectors.link text
: Used to find anchor elements by their link text.partial link text
: Used to find anchor elements by their partial link text.tag name
: Used to find elements by their tag name.xpath
: Used to find elements via XPath expressions.
Signature
-
parameter
using
String
The locator strategy to use.
-
parameter
value
String
The value to use with the locator strategy.
Implementation
def initialize(using, value)
@using = using
@value = value
end
attr :using
Signature
-
attribute
String
The locator strategy to use.
attr :value
Signature
-
attribute
String
The value to use with the locator strategy.
def as_json
Signature
-
returns
Hash
A JSON representation of the locator.
Implementation
def as_json
{using: @using, value: @value}
end
def to_json(...)
Signature
-
returns
String
A JSON representation of the locator.
Implementation
def to_json(...)
as_json.to_json(...)
end