Class: Watir::AppleScripter
- Includes:
- Exception
- Defined in:
- lib/safariwatir/scripter.rb
Overview
:nodoc:
Constant Summary collapse
- TIMEOUT =
10
Instance Attribute Summary collapse
-
#typing_lag ⇒ Object
Returns the value of attribute typing_lag.
Instance Method Summary collapse
- #append_text_input(value, element = @element) ⇒ Object
- #blur(element) ⇒ Object
-
#checkbox_is_checked?(element = @element) ⇒ Boolean
Contributed by Kyle Campos.
- #clear_text_input(element = @element) ⇒ Object
- #click_alert ⇒ Object
- #click_element(element = @element) ⇒ Object
- #click_link(element = @element) ⇒ Object
- #click_security_warning(label) ⇒ Object
- #close ⇒ Object
- #document_html ⇒ Object
- #document_text ⇒ Object
- #document_title ⇒ Object
- #element_disabled?(element = @element) ⇒ Boolean
- #element_exists?(element = @element, &block) ⇒ Boolean
- #ensure_window_ready ⇒ Object
- #focus(element) ⇒ Object
- #for_frame(element) ⇒ Object
- #for_table(element) ⇒ Object
- #get_html_for(element = @element) ⇒ Object
- #get_text_for(element = @element) ⇒ Object
- #get_value_for(element = @element) ⇒ Object
- #handle_selected(select_list) ⇒ Object
- #hide ⇒ Object
- #highlight(element, &block) ⇒ Object
-
#initialize(scripter = JavaScripter.new, opts = {}) ⇒ AppleScripter
constructor
A new instance of AppleScripter.
- #navigate_to(url, &extra_action) ⇒ Object
- #operate_by(element, attribute) ⇒ Object
- #operate_by_class(element) ⇒ Object
- #operate_by_id(element) ⇒ Object
- #operate_by_index(element) ⇒ Object
- #operate_by_input_value(element) ⇒ Object
- #operate_by_name(element) ⇒ Object
- #operate_by_src(element, &block) ⇒ Object
- #operate_by_table_cell(element = @element) ⇒ Object
- #operate_by_text(element, &block) ⇒ Object
- #operate_by_xpath(element) ⇒ Object
- #operate_on_link(element) ⇒ Object
- #option_exists?(element = @element) ⇒ Boolean
- #option_selected?(element = @element) ⇒ Boolean
- #reload ⇒ Object
- #select_option(element = @element) ⇒ Object
- #speak(string) ⇒ Object
- #speak_options_for(element = @element) ⇒ Object
- #speak_text_of(element = @element) ⇒ Object
- #speak_value_of(element = @element) ⇒ Object
- #submit_form(element) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(scripter = JavaScripter.new, opts = {}) ⇒ AppleScripter
Returns a new instance of AppleScripter.
98 99 100 101 102 103 |
# File 'lib/safariwatir/scripter.rb', line 98 def initialize(scripter = JavaScripter.new, opts={}) @js = scripter @appname = opts[:appname] || "Safari" @app = Appscript.app(@appname) @document = @app.documents[1] end |
Instance Attribute Details
#typing_lag ⇒ Object
Returns the value of attribute typing_lag.
93 94 95 |
# File 'lib/safariwatir/scripter.rb', line 93 def typing_lag @typing_lag end |
Instance Method Details
#append_text_input(value, element = @element) ⇒ Object
261 262 263 264 265 266 267 268 |
# File 'lib/safariwatir/scripter.rb', line 261 def append_text_input(value, element = @element) sleep typing_lag execute(element.operate do %|element.value += '#{value}'; dispatchOnChange(element); element.setSelectionRange(element.value.length, element.value.length);| end, element) end |
#blur(element) ⇒ Object
177 178 179 |
# File 'lib/safariwatir/scripter.rb', line 177 def blur(element) execute(element.operate { %|element.blur();| }, element) end |
#checkbox_is_checked?(element = @element) ⇒ Boolean
Contributed by Kyle Campos
366 367 368 |
# File 'lib/safariwatir/scripter.rb', line 366 def checkbox_is_checked?(element = @element) execute(element.operate { %|return element.checked;| }, element) end |
#clear_text_input(element = @element) ⇒ Object
257 258 259 |
# File 'lib/safariwatir/scripter.rb', line 257 def clear_text_input(element = @element) execute(element.operate { %|element.value = '';| }, element) end |
#click_alert ⇒ Object
456 457 458 459 460 461 462 463 |
# File 'lib/safariwatir/scripter.rb', line 456 def click_alert execute_system_events(%| tell window 1 if button named "OK" exists then click button named "OK" end if end tell|) end |
#click_element(element = @element) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/safariwatir/scripter.rb', line 270 def click_element(element = @element) page_load do # Not sure if these events should be either/or. But if you have an image with an onclick, it fires twice without the else clause. execute(element.operate { %| if (element.click) { element.click(); } else { if (element.onclick) { var event = DOCUMENT.createEvent('HTMLEvents'); event.initEvent('click', true, true); element.onclick(event); } else { var event = DOCUMENT.createEvent('MouseEvents'); event.initEvent('click', true, true); element.dispatchEvent(event); } }| }) end end |
#click_link(element = @element) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/safariwatir/scripter.rb', line 291 def click_link(element = @element) click = %/ function baseTarget() { var bases = document.getElementsByTagName('BASE'); if (bases.length > 0) { return bases[0].target; } else { return; } } function undefinedTarget(target) { return target == undefined || target == ''; } function topTarget(target) { return undefinedTarget(target) || target == '_top'; } function nextLocation(element) { var target = element.target; if (undefinedTarget(target) && baseTarget()) { top[baseTarget()].location = element.href; } else if (topTarget(target)) { top.location = element.href; } else { top[target].location = element.href; } } var click = DOCUMENT.createEvent('HTMLEvents'); click.initEvent('click', true, true); if (element.onclick) { if (false != element.onclick(click)) { nextLocation(element); } } else { nextLocation(element); }/ page_load do execute(js.operate(find_link(element), click)) end end |
#click_security_warning(label) ⇒ Object
465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/safariwatir/scripter.rb', line 465 def click_security_warning(label) execute_system_events(%| tell window 1 tell sheet 1 tell group 2 if button named "#{label}" exists then click button named "#{label}" return "#{EXTRA_ACTION_SUCCESS}" end if end tell end tell end tell|, true) end |
#close ⇒ Object
127 128 129 |
# File 'lib/safariwatir/scripter.rb', line 127 def close @app.quit end |
#document_html ⇒ Object
165 166 167 |
# File 'lib/safariwatir/scripter.rb', line 165 def document_html execute(%|return document.all[0].outerHTML;|) end |
#document_text ⇒ Object
161 162 163 |
# File 'lib/safariwatir/scripter.rb', line 161 def document_text execute(%|return document.getElementsByTagName('BODY').item(0).innerText;|) end |
#document_title ⇒ Object
169 170 171 |
# File 'lib/safariwatir/scripter.rb', line 169 def document_title execute(%|return document.title;|) end |
#element_disabled?(element = @element) ⇒ Boolean
370 371 372 |
# File 'lib/safariwatir/scripter.rb', line 370 def element_disabled?(element = @element) execute(element.operate { %|return element.disabled;| }, element) end |
#element_exists?(element = @element, &block) ⇒ Boolean
194 195 196 197 198 199 200 |
# File 'lib/safariwatir/scripter.rb', line 194 def element_exists?(element = @element, &block) block ||= Proc.new {} execute(element.operate(&block), element) return true rescue UnknownObjectException return false end |
#ensure_window_ready ⇒ Object
105 106 107 108 109 |
# File 'lib/safariwatir/scripter.rb', line 105 def ensure_window_ready @app.activate @app.make(:new => :document) if @app.documents.get.size == 0 @document = @app.documents[1] end |
#focus(element) ⇒ Object
173 174 175 |
# File 'lib/safariwatir/scripter.rb', line 173 def focus(element) execute(element.operate { %|element.focus();| }, element) end |
#for_frame(element) ⇒ Object
483 484 485 486 487 488 489 490 |
# File 'lib/safariwatir/scripter.rb', line 483 def for_frame(element) # verify the frame exists execute( %|if (parent.#{element.name} == undefined) { return '#{FRAME_NOT_FOUND}'; }|, element) AppleScripter.new(FrameJavaScripter.new(element)) end |
#for_table(element) ⇒ Object
479 480 481 |
# File 'lib/safariwatir/scripter.rb', line 479 def for_table(element) AppleScripter.new(TableJavaScripter.new(element)) end |
#get_html_for(element = @element) ⇒ Object
145 146 147 |
# File 'lib/safariwatir/scripter.rb', line 145 def get_html_for(element = @element) execute(element.operate { %|return element.innerHTML| }, element) end |
#get_text_for(element = @element) ⇒ Object
141 142 143 |
# File 'lib/safariwatir/scripter.rb', line 141 def get_text_for(element = @element) execute(element.operate { %|return element.innerText| }, element) end |
#get_value_for(element = @element) ⇒ Object
157 158 159 |
# File 'lib/safariwatir/scripter.rb', line 157 def get_value_for(element = @element) execute(element.operate { %|return element.value;| }, element) end |
#handle_selected(select_list) ⇒ Object
246 247 248 249 250 251 252 253 254 |
# File 'lib/safariwatir/scripter.rb', line 246 def handle_selected(select_list) %|var selected = false; for (var i = 0; i < element.options.length; i++) { if (element.options[i].#{select_list.how} == '#{select_list.what}' && element.options[i].selected) { selected = true; } } return selected;| end |
#hide ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/safariwatir/scripter.rb', line 115 def hide # because applescript is stupid and annoying you have # to get all the processes from System Events, grab # the right one for this instance and then set visible # of it to false. se = Appscript.app("System Events") safari = se.processes.get.select do |app| app.name.get == @appname end.first safari.visible.set(false) end |
#highlight(element, &block) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/safariwatir/scripter.rb', line 181 def highlight(element, &block) execute(element.operate do %|element.originalColor = element.style.backgroundColor; element.style.backgroundColor = 'yellow';| end, element) @element = element instance_eval(&block) @element = nil execute_and_ignore(element.operate { %|element.style.backgroundColor = element.originalColor;| }) end |
#navigate_to(url, &extra_action) ⇒ Object
131 132 133 134 135 |
# File 'lib/safariwatir/scripter.rb', line 131 def navigate_to(url, &extra_action) page_load(extra_action) do @document.URL.set(url) end end |
#operate_by(element, attribute) ⇒ Object
439 440 441 442 443 444 445 446 447 448 |
# File 'lib/safariwatir/scripter.rb', line 439 def operate_by(element, attribute) js.operate(%|var elements = document.getElementsByTagName('#{element.tag}'); var element = undefined; for (var i = 0; i < elements.length; i++) { if (elements[i].#{handle_match(element, attribute)}) { element = elements[i]; break; } }|, yield) end |
#operate_by_class(element) ⇒ Object
397 398 399 400 401 |
# File 'lib/safariwatir/scripter.rb', line 397 def operate_by_class(element) js.operate(%| var elements = document.getElementsByClassName('#{element.what}'); var element = elements[0];|, yield) end |
#operate_by_id(element) ⇒ Object
416 417 418 |
# File 'lib/safariwatir/scripter.rb', line 416 def operate_by_id(element) js.operate("var element = document.getElementById('#{element.what}');", yield) end |
#operate_by_index(element) ⇒ Object
420 421 422 |
# File 'lib/safariwatir/scripter.rb', line 420 def operate_by_index(element) js.operate(%|var element = document.getElementsByTagName('#{element.tag}')[#{element.what-1}];|, yield) end |
#operate_by_input_value(element) ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/safariwatir/scripter.rb', line 374 def operate_by_input_value(element) js.operate(%| var elements = document.getElementsByTagName('INPUT'); var element = undefined; for (var i = 0; i < elements.length; i++) { if (elements[i].value == '#{element.what}') { element = elements[i]; break; } }|, yield) end |
#operate_by_name(element) ⇒ Object
386 387 388 389 390 391 392 393 394 395 |
# File 'lib/safariwatir/scripter.rb', line 386 def operate_by_name(element) js.operate(%| var elements = document.getElementsByName('#{element.what}'); var element = undefined; for (var i = 0; i < elements.length; i++) { if (elements[i].tagName != 'META' && elements[i].tagName == '#{element.tag}') { #{handle_form_element_name_match(element)} } }|, yield) end |
#operate_by_src(element, &block) ⇒ Object
424 425 426 |
# File 'lib/safariwatir/scripter.rb', line 424 def operate_by_src(element, &block) operate_by(element, 'src', &block) end |
#operate_by_table_cell(element = @element) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/safariwatir/scripter.rb', line 149 def operate_by_table_cell(element = @element) %|var element = document; if (element == undefined) { return '#{TABLE_CELL_NOT_FOUND}'; } #{yield}| end |
#operate_by_text(element, &block) ⇒ Object
428 429 430 |
# File 'lib/safariwatir/scripter.rb', line 428 def operate_by_text(element, &block) operate_by(element, 'innerText', &block) end |
#operate_by_xpath(element) ⇒ Object
432 433 434 435 436 437 |
# File 'lib/safariwatir/scripter.rb', line 432 def operate_by_xpath(element) js.operate(%| var result = document.evaluate('#{element.what}', document.documentElement, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null); var element = result ? result.singleNodeValue : null; |, yield) end |
#operate_on_link(element) ⇒ Object
331 332 333 |
# File 'lib/safariwatir/scripter.rb', line 331 def operate_on_link(element) js.operate(find_link(element), yield) end |
#option_exists?(element = @element) ⇒ Boolean
225 226 227 |
# File 'lib/safariwatir/scripter.rb', line 225 def option_exists?(element = @element) element_exists?(element) { handle_option(element) } end |
#option_selected?(element = @element) ⇒ Boolean
242 243 244 |
# File 'lib/safariwatir/scripter.rb', line 242 def option_selected?(element = @element) element_exists?(element) { handle_selected(element) == "true" } end |
#reload ⇒ Object
137 138 139 |
# File 'lib/safariwatir/scripter.rb', line 137 def reload execute(%|window.location.reload()|) end |
#select_option(element = @element) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/safariwatir/scripter.rb', line 202 def select_option(element = @element) execute(element.operate do %|var selected = -1; var previous_selection = 0; for (var i = 0; i < element.options.length; i++) { if (element.options[i].selected) { previous_selection = i; } if (element.options[i].#{element.how} == '#{element.what}') { element.options[i].selected = true; selected = i; } } if (selected == -1) { return '#{ELEMENT_NOT_FOUND}'; } else if (previous_selection != selected) { element.selectedIndex = selected; dispatchOnChange(element.options[selected]); } | end, element) end |
#speak(string) ⇒ Object
513 514 515 516 517 518 |
# File 'lib/safariwatir/scripter.rb', line 513 def speak(string) `osascript <<SCRIPT say "#{string.quote_safe}" SCRIPT` nil end |
#speak_options_for(element = @element) ⇒ Object
500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/safariwatir/scripter.rb', line 500 def (element = @element) values = execute(element.operate do %|var values = ''; for (var i = 0; i < element.options.length; i++) { if (element.options[i].selected == true) { values += ' ' + element.options[i].text; } } return values| end, element) speak(values) end |
#speak_text_of(element = @element) ⇒ Object
496 497 498 |
# File 'lib/safariwatir/scripter.rb', line 496 def speak_text_of(element = @element) speak(element.text) end |
#speak_value_of(element = @element) ⇒ Object
492 493 494 |
# File 'lib/safariwatir/scripter.rb', line 492 def speak_value_of(element = @element) speak(get_value_for(element)) end |
#submit_form(element) ⇒ Object
450 451 452 453 454 |
# File 'lib/safariwatir/scripter.rb', line 450 def submit_form(element) page_load do execute(element.operate { %|element.submit();| }) end end |
#url ⇒ Object
111 112 113 |
# File 'lib/safariwatir/scripter.rb', line 111 def url @document.URL.get end |