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.
72 73 74 75 76 77 78 79 |
# File 'lib/async/webdriver/element.rb', line 72 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.
95 96 97 |
# File 'lib/async/webdriver/element.rb', line 95 def delegate @delegate end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
98 99 100 |
# File 'lib/async/webdriver/element.rb', line 98 def id @id end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
92 93 94 |
# File 'lib/async/webdriver/element.rb', line 92 def session @session end |
#The element identifier.(elementidentifier.) ⇒ Object (readonly)
98 |
# File 'lib/async/webdriver/element.rb', line 98 attr :id |
#The underlying HTTP client (or wrapper).(underlyingHTTPclient() ⇒ Object
95 |
# File 'lib/async/webdriver/element.rb', line 95 attr :delegate |
Instance Method Details
#as_json ⇒ Object
82 83 84 |
# File 'lib/async/webdriver/element.rb', line 82 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”) “`
150 151 152 |
# File 'lib/async/webdriver/element.rb', line 150 def attribute(name) get("attribute/#{name}") end |
#attributes ⇒ Object
Get attributes associated with the element.
163 164 165 |
# File 'lib/async/webdriver/element.rb', line 163 def attributes @attributes ||= Attributes.new(self) end |
#clear ⇒ Object
Clear the element.
258 259 260 |
# File 'lib/async/webdriver/element.rb', line 258 def clear post("clear") end |
#click ⇒ Object
Click the element.
253 254 255 |
# File 'lib/async/webdriver/element.rb', line 253 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 “`
191 192 193 |
# File 'lib/async/webdriver/element.rb', line 191 def css(name) get("css/#{name}") end |
#current_scope ⇒ Object
The current scope to use for making subsequent requests.
115 116 117 |
# File 'lib/async/webdriver/element.rb', line 115 def current_scope self end |
#displayed? ⇒ Boolean
Whether the element is displayed.
248 249 250 |
# File 'lib/async/webdriver/element.rb', line 248 def displayed? get("displayed") end |
#enabled? ⇒ Boolean
Whether the element is enabled.
242 243 244 |
# File 'lib/async/webdriver/element.rb', line 242 def enabled? get("enabled") end |
#execute(script, *arguments) ⇒ Object
Execute a script in the context of the element. ‘this` will be the element.
122 123 124 |
# File 'lib/async/webdriver/element.rb', line 122 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.
129 130 131 |
# File 'lib/async/webdriver/element.rb', line 129 def execute_async(script, *arguments) @session.execute_async("return (function(){#{script}}).call(...arguments)", self, *arguments) end |
#frame? ⇒ Boolean
Whether the element is a frame.
270 271 272 |
# File 'lib/async/webdriver/element.rb', line 270 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 “`
177 178 179 |
# File 'lib/async/webdriver/element.rb', line 177 def property(name) get("property/#{name}") end |
#rectangle ⇒ Object
Get the element’s bounding rectangle.
226 227 228 229 230 |
# File 'lib/async/webdriver/element.rb', line 226 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.
103 104 105 106 107 108 109 |
# File 'lib/async/webdriver/element.rb', line 103 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.
234 235 236 |
# File 'lib/async/webdriver/element.rb', line 234 def selected? get("selected") end |
#send_keys(text) ⇒ Object
Send keys to the element. Simulates a user typing keys while the element is focused.
263 264 265 |
# File 'lib/async/webdriver/element.rb', line 263 def send_keys(text) post("value", {text: text}) end |
#set_attribute(name, value) ⇒ Object
Set the value of an attribute.
157 158 159 |
# File 'lib/async/webdriver/element.rb', line 157 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 “`
217 218 219 |
# File 'lib/async/webdriver/element.rb', line 217 def tag_name get("name").downcase 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 “`
204 205 206 |
# File 'lib/async/webdriver/element.rb', line 204 def text get("text") end |
#The session the element belongs to.=(sessiontheelementbelongsto. = (value)) ⇒ Object
92 |
# File 'lib/async/webdriver/element.rb', line 92 attr :session |
#to_json ⇒ Object
87 88 89 |
# File 'lib/async/webdriver/element.rb', line 87 def to_json(...) as_json.to_json(...) end |