Class: Async::WebDriver::Locator
- Inherits:
-
Object
- Object
- Async::WebDriver::Locator
- Defined in:
- lib/async/webdriver/locator.rb
Overview
A locator is used to find elements in the DOM.
You can use the following convenience methods to create locators:
“‘ ruby Locator.css(“main#content”) Locator.xpath(“//main“) 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:
“‘ ruby Locator.wrap(css: “main#content”) Locator.wrap(xpath: “//main”) Locator.wrap(link_text: “Home”) Locator.wrap(partial_link_text: “Ho”) Locator.wrap(tag_name: “main”) “`
For more information, see: <w3c.github.io/webdriver/#locator-strategies>.
Instance Attribute Summary collapse
- #The locator strategy to use.(locatorstrategytouse.) ⇒ Object readonly
- #The value to use with the locator strategy.(valuetousewiththelocatorstrategy.) ⇒ Object readonly
-
#using ⇒ Object
readonly
Returns the value of attribute using.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.css(css) ⇒ Object
A convenience wrapper for specifying CSS locators.
-
.link_text(text) ⇒ Object
A convenience wrapper for specifying link text locators.
-
.partial_link_text(text) ⇒ Object
A convenience wrapper for specifying partial link text locators.
-
.tag_name(name) ⇒ Object
A convenience wrapper for specifying tag name locators.
-
.wrap(locator = nil, **options) ⇒ Object
A convenience wrapper for specifying locators.
-
.xpath(xpath) ⇒ Object
A convenience wrapper for specifying XPath locators.
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(using, value) ⇒ Locator
constructor
Initialize the locator.
- #to_json ⇒ Object
Constructor Details
#initialize(using, value) ⇒ Locator
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.
105 106 107 108 |
# File 'lib/async/webdriver/locator.rb', line 105 def initialize(using, value) @using = using @value = value end |
Instance Attribute Details
#The locator strategy to use.(locatorstrategytouse.) ⇒ Object (readonly)
111 |
# File 'lib/async/webdriver/locator.rb', line 111 attr :using |
#The value to use with the locator strategy.(valuetousewiththelocatorstrategy.) ⇒ Object (readonly)
114 |
# File 'lib/async/webdriver/locator.rb', line 114 attr :value |
#using ⇒ Object (readonly)
Returns the value of attribute using.
111 112 113 |
# File 'lib/async/webdriver/locator.rb', line 111 def using @using end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
114 115 116 |
# File 'lib/async/webdriver/locator.rb', line 114 def value @value end |
Class Method Details
.css(css) ⇒ Object
A convenience wrapper for specifying CSS locators.
70 71 72 |
# File 'lib/async/webdriver/locator.rb', line 70 def self.css(css) new("css selector", css) end |
.link_text(text) ⇒ Object
A convenience wrapper for specifying link text locators.
75 76 77 |
# File 'lib/async/webdriver/locator.rb', line 75 def self.link_text(text) new("link text", text) end |
.partial_link_text(text) ⇒ Object
A convenience wrapper for specifying partial link text locators.
80 81 82 |
# File 'lib/async/webdriver/locator.rb', line 80 def self.partial_link_text(text) new("partial link text", text) end |
.tag_name(name) ⇒ Object
A convenience wrapper for specifying tag name locators.
85 86 87 |
# File 'lib/async/webdriver/locator.rb', line 85 def self.tag_name(name) new("tag name", name) end |
.wrap(locator = nil, **options) ⇒ Object
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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/async/webdriver/locator.rb', line 49 def self.wrap(locator = nil, **) if locator.is_a?(Locator) locator elsif css = [:css] css(css) elsif xpath = [:xpath] xpath(xpath) elsif link_text = [:link_text] link_text(link_text) elsif partial_link_text = [:partial_link_text] partial_link_text(partial_link_text) elsif tag_name = [:tag_name] tag_name(tag_name) elsif using = [:using] new(using, [:value]) else raise ArgumentError, "Unable to interpret #{locator.inspect} with #{.inspect}!" end end |
.xpath(xpath) ⇒ Object
A convenience wrapper for specifying XPath locators.
90 91 92 |
# File 'lib/async/webdriver/locator.rb', line 90 def self.xpath(xpath) new("xpath", xpath) end |
Instance Method Details
#as_json ⇒ Object
117 118 119 |
# File 'lib/async/webdriver/locator.rb', line 117 def as_json {using: @using, value: @value} end |
#to_json ⇒ Object
122 123 124 |
# File 'lib/async/webdriver/locator.rb', line 122 def to_json(...) as_json.to_json(...) end |