Class: Async::WebDriver::Locator

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#usingObject (readonly)

Returns the value of attribute using.



111
112
113
# File 'lib/async/webdriver/locator.rb', line 111

def using
  @using
end

#valueObject (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

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

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:

  1. A locator instance, or

  2. A single option css:, xpath:, link_text:, partial_link_text: or tag_name:, or

  3. A using: and value: option which will be used directly.

Parameters:

  • css (Hash)

    a customizable set of options

  • xpath (Hash)

    a customizable set of options

  • link_text (Hash)

    a customizable set of options

  • partial_link_text (Hash)

    a customizable set of options

  • tag_name (Hash)

    a customizable set of options

  • using (Hash)

    a customizable set of options

  • value (Hash)

    a customizable set of options



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, **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

.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_jsonObject



117
118
119
# File 'lib/async/webdriver/locator.rb', line 117

def as_json
	{using: @using, value: @value}
end

#to_jsonObject



122
123
124
# File 'lib/async/webdriver/locator.rb', line 122

def to_json(...)
	as_json.to_json(...)
end