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.
103 104 105 106 |
# File 'lib/async/webdriver/locator.rb', line 103 def initialize(using, value) @using = using @value = value end |
Instance Attribute Details
#The locator strategy to use.(locatorstrategytouse.) ⇒ Object (readonly)
109 |
# File 'lib/async/webdriver/locator.rb', line 109 attr :using |
#The value to use with the locator strategy.(valuetousewiththelocatorstrategy.) ⇒ Object (readonly)
112 |
# File 'lib/async/webdriver/locator.rb', line 112 attr :value |
#using ⇒ Object (readonly)
Returns the value of attribute using.
109 110 111 |
# File 'lib/async/webdriver/locator.rb', line 109 def using @using end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
112 113 114 |
# File 'lib/async/webdriver/locator.rb', line 112 def value @value end |
Class Method Details
.css(css) ⇒ Object
A convenience wrapper for specifying CSS locators.
68 69 70 |
# File 'lib/async/webdriver/locator.rb', line 68 def self.css(css) new("css selector", css) end |
.link_text(text) ⇒ Object
A convenience wrapper for specifying link text locators.
73 74 75 |
# File 'lib/async/webdriver/locator.rb', line 73 def self.link_text(text) new("link text", text) end |
.partial_link_text(text) ⇒ Object
A convenience wrapper for specifying partial link text locators.
78 79 80 |
# File 'lib/async/webdriver/locator.rb', line 78 def self.partial_link_text(text) new("partial link text", text) end |
.tag_name(name) ⇒ Object
A convenience wrapper for specifying tag name locators.
83 84 85 |
# File 'lib/async/webdriver/locator.rb', line 83 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:` or `tag_name:`, or
-
A ‘using:` and `value:` option which will be used directly.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/async/webdriver/locator.rb', line 47 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.
88 89 90 |
# File 'lib/async/webdriver/locator.rb', line 88 def self.xpath(xpath) new("xpath", xpath) end |
Instance Method Details
#as_json ⇒ Object
115 116 117 |
# File 'lib/async/webdriver/locator.rb', line 115 def as_json {using: @using, value: @value} end |
#to_json ⇒ Object
120 121 122 |
# File 'lib/async/webdriver/locator.rb', line 120 def to_json(...) as_json.to_json(...) end |