Class: Async::WebDriver::Element
- Inherits:
-
Object
- Object
- Async::WebDriver::Element
- Includes:
- RequestHelper, Scope::Alerts, Scope::Cookies, Scope::Elements, Scope::Fields, Scope::Printing, Scope::ScreenCapture
- Defined in:
- lib/async/webdriver/element.rb
Overview
An element represents a DOM element. This class is used to interact with the DOM.
“‘ ruby element = session.find_element(:css, “main#content”) element.click “`
Defined Under Namespace
Classes: Attributes, Rectangle
Constant Summary collapse
- FRAME_TAGS =
["frame", "iframe"].freeze
Constants included from RequestHelper
RequestHelper::CONTENT_TYPE, RequestHelper::ELEMENT_KEY, RequestHelper::GET_HEADERS, RequestHelper::POST_HEADERS
Instance Attribute Summary collapse
-
#delegate ⇒ Object
readonly
Returns the value of attribute delegate.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
- #The element identifier.(elementidentifier.) ⇒ Object readonly
- #The underlying HTTP client (or wrapper).(underlyingHTTPclient() ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
-
#attribute(name) ⇒ Object
Get the value of an attribute.
-
#attributes ⇒ Object
Get attributes associated with the element.
-
#clear ⇒ Object
Clear the element.
-
#click ⇒ Object
Click the element.
-
#css(name) ⇒ Object
Get the value of a CSS property.
-
#current_scope ⇒ Object
The current scope to use for making subsequent requests.
-
#displayed? ⇒ Boolean
Whether the element is displayed.
-
#enabled? ⇒ Boolean
Whether the element is enabled.
-
#execute(script, *arguments) ⇒ Object
Execute a script in the context of the element.
-
#execute_async(script, *arguments) ⇒ Object
Execute a script in the context of the element.
-
#frame? ⇒ Boolean
Whether the element is a frame.
-
#initialize(session, id) ⇒ Element
constructor
Initialize the element.
-
#property(name) ⇒ Object
Get the value of a property.
-
#rectangle ⇒ Object
Get the element’s bounding rectangle.
-
#request_path(path = nil) ⇒ Object
The path used for making requests to the web driver bridge.
-
#selected? ⇒ Boolean
(also: #checked?)
Whether the element is selected OR checked.
-
#send_keys(text) ⇒ Object
Send keys to the element.
-
#set_attribute(name, value) ⇒ Object
Set the value of an attribute.
-
#tag_name ⇒ Object
Get the element’s tag name.
-
#text ⇒ Object
Get the text content of the element.
- #The session the element belongs to.=(sessiontheelementbelongsto. = (value)) ⇒ Object
- #to_json ⇒ Object
Methods included from Scope::ScreenCapture
Methods included from Scope::Printing
Methods included from Scope::Fields
#check, #click_button, #fill_in, #find_field
Methods included from Scope::Elements
#children, #find_element, #find_element_by_css, #find_element_by_link_text, #find_element_by_partial_link_text, #find_element_by_tag_name, #find_element_by_xpath, #find_elements, #find_elements_by_css, #find_elements_by_link_text, #find_elements_by_partial_link_text, #find_elements_by_tag_name, #find_elements_by_xpath
Methods included from Scope::Cookies
#add_cookie, #cookie, #cookies, #delete_all_cookies, #delete_cookie
Methods included from Scope::Alerts
#accept_alert, #alert_text, #dismiss_alert, #set_alert_text
Methods included from RequestHelper
#delete, #extract_value, #get, #post, #unwrap_object, #unwrap_objects
Constructor Details
#initialize(session, id) ⇒ Element
Initialize the element.
70 71 72 73 74 75 76 77 |
# File 'lib/async/webdriver/element.rb', line 70 def initialize(session, id) @session = session @delegate = session.delegate @id = id @attributes = nil @properties = nil end |
Instance Attribute Details
#delegate ⇒ Object (readonly)
Returns the value of attribute delegate.
93 94 95 |
# File 'lib/async/webdriver/element.rb', line 93 def delegate @delegate end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
96 97 98 |
# File 'lib/async/webdriver/element.rb', line 96 def id @id end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
90 91 92 |
# File 'lib/async/webdriver/element.rb', line 90 def session @session end |
#The element identifier.(elementidentifier.) ⇒ Object (readonly)
96 |
# File 'lib/async/webdriver/element.rb', line 96 attr :id |
#The underlying HTTP client (or wrapper).(underlyingHTTPclient() ⇒ Object
93 |
# File 'lib/async/webdriver/element.rb', line 93 attr :delegate |
Instance Method Details
#as_json ⇒ Object
80 81 82 |
# File 'lib/async/webdriver/element.rb', line 80 def as_json {ELEMENT_KEY => @id} end |
#attribute(name) ⇒ Object
Get the value of an attribute.
Given an attribute name, e.g. ‘href`, this method will return the value of the attribute, as if you had executed the following JavaScript:
“‘js element.getAttribute(“href”) “`
148 149 150 |
# File 'lib/async/webdriver/element.rb', line 148 def attribute(name) get("attribute/#{name}") end |
#attributes ⇒ Object
Get attributes associated with the element.
161 162 163 |
# File 'lib/async/webdriver/element.rb', line 161 def attributes @attributes ||= Attributes.new(self) end |
#clear ⇒ Object
Clear the element.
256 257 258 |
# File 'lib/async/webdriver/element.rb', line 256 def clear post("clear") end |
#click ⇒ Object
Click the element.
251 252 253 |
# File 'lib/async/webdriver/element.rb', line 251 def click post("click") end |
#css(name) ⇒ Object
Get the value of a CSS property.
Given a CSS property name, e.g. ‘width`, this method will return the value of the property, as if you had executed the following JavaScript:
“‘js window.getComputedStyle(element).width “`
189 190 191 |
# File 'lib/async/webdriver/element.rb', line 189 def css(name) get("css/#{name}") end |
#current_scope ⇒ Object
The current scope to use for making subsequent requests.
113 114 115 |
# File 'lib/async/webdriver/element.rb', line 113 def current_scope self end |
#displayed? ⇒ Boolean
Whether the element is displayed.
246 247 248 |
# File 'lib/async/webdriver/element.rb', line 246 def displayed? get("displayed") end |
#enabled? ⇒ Boolean
Whether the element is enabled.
240 241 242 |
# File 'lib/async/webdriver/element.rb', line 240 def enabled? get("enabled") end |
#execute(script, *arguments) ⇒ Object
Execute a script in the context of the element. ‘this` will be the element.
120 121 122 |
# File 'lib/async/webdriver/element.rb', line 120 def execute(script, *arguments) @session.execute("return (function(){#{script}}).call(...arguments)", self, *arguments) end |
#execute_async(script, *arguments) ⇒ Object
Execute a script in the context of the element. ‘this` will be the element.
127 128 129 |
# File 'lib/async/webdriver/element.rb', line 127 def execute_async(script, *arguments) @session.execute_async("return (function(){#{script}}).call(...arguments)", self, *arguments) end |
#frame? ⇒ Boolean
Whether the element is a frame.
268 269 270 |
# File 'lib/async/webdriver/element.rb', line 268 def frame? FRAME_TAGS.include?(self.tag_name) end |
#property(name) ⇒ Object
Get the value of a property.
Given a property name, e.g. ‘offsetWidth`, this method will return the value of the property, as if you had executed the following JavaScript:
“‘js element.offsetWidth “`
175 176 177 |
# File 'lib/async/webdriver/element.rb', line 175 def property(name) get("property/#{name}") end |
#rectangle ⇒ Object
Get the element’s bounding rectangle.
224 225 226 227 228 |
# File 'lib/async/webdriver/element.rb', line 224 def rectangle get("rect").tap do |reply| Rectangle.new(reply["x"], reply["y"], reply["width"], reply["height"]) end end |
#request_path(path = nil) ⇒ Object
The path used for making requests to the web driver bridge.
101 102 103 104 105 106 107 |
# File 'lib/async/webdriver/element.rb', line 101 def request_path(path = nil) if path "/session/#{@session.id}/element/#{@id}/#{path}" else "/session/#{@session}/element/#{@id}" end end |
#selected? ⇒ Boolean Also known as: checked?
Whether the element is selected OR checked.
232 233 234 |
# File 'lib/async/webdriver/element.rb', line 232 def selected? get("selected") end |
#send_keys(text) ⇒ Object
Send keys to the element. Simulates a user typing keys while the element is focused.
261 262 263 |
# File 'lib/async/webdriver/element.rb', line 261 def send_keys(text) post("value", {text: text}) end |
#set_attribute(name, value) ⇒ Object
Set the value of an attribute.
155 156 157 |
# File 'lib/async/webdriver/element.rb', line 155 def set_attribute(name, value) execute("this.setAttribute(...arguments)", name, value) end |
#tag_name ⇒ Object
Get the element’s tag name.
This method will return the tag name of the element, as if you had executed the following JavaScript:
“‘js element.tagName “`
215 216 217 |
# File 'lib/async/webdriver/element.rb', line 215 def tag_name get("name") end |
#text ⇒ Object
Get the text content of the element.
This method will return the text content of the element, as if you had executed the following JavaScript:
“‘js element.textContent “`
202 203 204 |
# File 'lib/async/webdriver/element.rb', line 202 def text get("text") end |
#The session the element belongs to.=(sessiontheelementbelongsto. = (value)) ⇒ Object
90 |
# File 'lib/async/webdriver/element.rb', line 90 attr :session |
#to_json ⇒ Object
85 86 87 |
# File 'lib/async/webdriver/element.rb', line 85 def to_json(...) as_json.to_json(...) end |