Class: Bauxite::Action
- Inherits:
-
Object
- Object
- Bauxite::Action
- Includes:
- ActionModule
- Defined in:
- lib/bauxite/actions/js.rb,
lib/bauxite/actions/set.rb,
lib/bauxite/core/action.rb,
lib/bauxite/actions/doif.rb,
lib/bauxite/actions/echo.rb,
lib/bauxite/actions/exec.rb,
lib/bauxite/actions/exit.rb,
lib/bauxite/actions/load.rb,
lib/bauxite/actions/open.rb,
lib/bauxite/actions/ruby.rb,
lib/bauxite/actions/test.rb,
lib/bauxite/actions/wait.rb,
lib/bauxite/actions/alias.rb,
lib/bauxite/actions/break.rb,
lib/bauxite/actions/click.rb,
lib/bauxite/actions/debug.rb,
lib/bauxite/actions/reset.rb,
lib/bauxite/actions/setif.rb,
lib/bauxite/actions/store.rb,
lib/bauxite/actions/write.rb,
lib/bauxite/actions/assert.rb,
lib/bauxite/actions/failif.rb,
lib/bauxite/actions/params.rb,
lib/bauxite/actions/return.rb,
lib/bauxite/actions/select.rb,
lib/bauxite/actions/source.rb,
lib/bauxite/actions/submit.rb,
lib/bauxite/actions/asserth.rb,
lib/bauxite/actions/assertm.rb,
lib/bauxite/actions/assertv.rb,
lib/bauxite/actions/assertw.rb,
lib/bauxite/actions/capture.rb,
lib/bauxite/actions/replace.rb,
lib/bauxite/actions/tryload.rb,
lib/bauxite/actions/dounless.rb
Overview
– Copyright © 2014 Patricio Zavolinsky
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++
Instance Attribute Summary
Attributes included from ActionModule
Instance Method Summary collapse
-
#alias_action(name, action, *args) ⇒ Object
:category: Action Methods.
-
#assert(selector, text) ⇒ Object
:category: Action Methods.
-
#asserth(*args) ⇒ Object
:category: Action Methods.
-
#assertm(text, action = 'accept') ⇒ Object
:category: Action Methods.
-
#assertv(expected, actual) ⇒ Object
:category: Action Methods.
-
#assertw(count = 1) ⇒ Object
:category: Action Methods.
-
#break_action ⇒ Object
:category: Action Methods.
-
#capture(file = nil) ⇒ Object
:category: Action Methods.
-
#click(selector) ⇒ Object
:category: Action Methods.
-
#debug ⇒ Object
:category: Action Methods.
-
#doif(expected, actual, action, *args) ⇒ Object
:category: Action Methods.
-
#dounless(expected, actual, action, *args) ⇒ Object
:category: Action Methods.
-
#echo(text) ⇒ Object
:category: Action Methods.
-
#exec(*command) ⇒ Object
:category: Action Methods.
-
#exit_action ⇒ Object
:category: Action Methods.
-
#failif(action, *args) ⇒ Object
:category: Action Methods.
-
#js(script, name = nil) ⇒ Object
:category: Action Methods.
-
#load(file, *vars) ⇒ Object
:category: Action Methods.
-
#open(url) ⇒ Object
:category: Action Methods.
-
#params(*vars) ⇒ Object
:category: Action Methods.
-
#replace(text, pattern, replacement, name) ⇒ Object
:category: Action Methods.
-
#reset ⇒ Object
:category: Action Methods.
-
#return_action(*vars) ⇒ Object
:category: Action Methods.
-
#ruby(file, *vars) ⇒ Object
:category: Action Methods.
-
#select(selector, text) ⇒ Object
:category: Action Methods.
-
#set(name, value) ⇒ Object
:category: Action Methods.
-
#setif(name, value, action, *args) ⇒ Object
:category: Action Methods.
-
#source(text) ⇒ Object
:category: Action Methods.
-
#store(selector, name) ⇒ Object
:category: Action Methods.
-
#submit(selector) ⇒ Object
:category: Action Methods.
-
#test(file, name = nil, *vars) ⇒ Object
:category: Action Methods.
-
#tryload(file, *vars) ⇒ Object
:category: Action Methods.
-
#wait(seconds) ⇒ Object
:category: Action Methods.
-
#write(selector, text) ⇒ Object
:category: Action Methods.
Methods included from ActionModule
Instance Method Details
#alias_action(name, action, *args) ⇒ Object
:category: Action Methods
48 49 50 |
# File 'lib/bauxite/actions/alias.rb', line 48 def alias_action(name, action, *args) @ctx.add_alias(name, action, args) end |
#assert(selector, text) ⇒ Object
:category: Action Methods
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bauxite/actions/assert.rb', line 38 def assert(selector, text) @ctx.with_timeout Bauxite::Errors::AssertionError do @ctx.find(selector) do |e| actual = @ctx.get_value(e) unless actual =~ _pattern(text) raise Bauxite::Errors::AssertionError, "Assertion failed: expected '#{text}', got '#{actual}'" end true end end end |
#asserth(*args) ⇒ Object
:category: Action Methods
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bauxite/actions/asserth.rb', line 46 def asserth(*args) uri = URI(@ctx.driver.current_url) res = Net::HTTP.get_response(uri) args.each do |a| name,value = a.split('=', 2); name = name.strip.downcase value = value.strip actual = res[name] || '' unless actual =~ _pattern(value) raise Bauxite::Errors::AssertionError, "Assertion failed for HTTP Header '#{name}': expected '#{value}', got '#{actual}'" end end end |
#assertm(text, action = 'accept') ⇒ Object
:category: Action Methods
40 41 42 43 44 45 46 47 48 |
# File 'lib/bauxite/actions/assertm.rb', line 40 def assertm(text, action = 'accept') ctx.with_timeout Selenium::WebDriver::Error::NoAlertPresentError do a = @ctx.driver.switch_to.alert unless a.text =~ _pattern(text) raise Bauxite::Errors::AssertionError, "Assertion failed: expected '#{text}', got '#{a.text}'" end a.send(action.to_sym) end end |
#assertv(expected, actual) ⇒ Object
:category: Action Methods
34 35 36 37 38 39 |
# File 'lib/bauxite/actions/assertv.rb', line 34 def assertv(expected, actual) unless actual =~ _pattern(expected) raise Bauxite::Errors::AssertionError, "Assertion failed: '#{actual}' does not match '#{expected}'" end true end |
#assertw(count = 1) ⇒ Object
:category: Action Methods
39 40 41 42 43 44 45 46 |
# File 'lib/bauxite/actions/assertw.rb', line 39 def assertw(count = 1) @ctx.with_timeout Bauxite::Errors::AssertionError do unless @ctx.driver.window_handles.size == count.to_i raise Bauxite::Errors::AssertionError, "Assertion failed: all popups must be closed." end true end end |
#break_action ⇒ Object
:category: Action Methods
36 37 38 |
# File 'lib/bauxite/actions/break.rb', line 36 def break_action lambda { Bauxite::Context::wait } end |
#capture(file = nil) ⇒ Object
:category: Action Methods
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bauxite/actions/capture.rb', line 40 def capture(file = nil) unless file seq = @ctx.variables['__CAPTURE_SEQ__'] || 0 test = @ctx.variables['__TEST__'] @ctx.variables['__CAPTURE_SEQ__'] = seq + 1 file = @ctx.variables['__FILE__'] || '' file = file[Dir.pwd.size+1..-1] if file.start_with? Dir.pwd file += "_#{seq}" file = "#{test}_#{file}" if test file = file.gsub(/[^A-Z0-9_-]/i, '_') + '.png' end file = @ctx.output_path(file) @ctx.driver.save_screenshot(file) @ctx.variables['__CAPTURE__'] = file true end |
#click(selector) ⇒ Object
:category: Action Methods
32 33 34 35 |
# File 'lib/bauxite/actions/click.rb', line 32 def click(selector) @ctx.find(selector) { |e| e.click } true end |
#debug ⇒ Object
:category: Action Methods
35 36 37 38 39 40 41 |
# File 'lib/bauxite/actions/debug.rb', line 35 def debug lambda do @ctx.with_vars({ '__DEBUG__' => true }) do _debug_process end end end |
#doif(expected, actual, action, *args) ⇒ Object
:category: Action Methods
39 40 41 42 |
# File 'lib/bauxite/actions/doif.rb', line 39 def doif(expected, actual, action, *args) return false unless actual =~ _pattern(expected) @ctx.exec_action_object(@ctx.get_action(action, args)) end |
#dounless(expected, actual, action, *args) ⇒ Object
:category: Action Methods
39 40 41 42 |
# File 'lib/bauxite/actions/dounless.rb', line 39 def dounless(expected, actual, action, *args) return false if actual =~ _pattern(expected) @ctx.exec_action_object(@ctx.get_action(action, args)) end |
#echo(text) ⇒ Object
:category: Action Methods
33 34 35 |
# File 'lib/bauxite/actions/echo.rb', line 33 def echo(text) true end |
#exec(*command) ⇒ Object
:category: Action Methods
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bauxite/actions/exec.rb', line 35 def exec(*command) data = command[0].split('=', 2) name = nil if (data.size == 2) name = data[0] command[0] = data[1] end ret = `#{command.join(' ')}` @ctx.variables[name] = ret.strip if name end |
#exit_action ⇒ Object
:category: Action Methods
32 33 34 |
# File 'lib/bauxite/actions/exit.rb', line 32 def exit_action :break end |
#failif(action, *args) ⇒ Object
:category: Action Methods
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bauxite/actions/failif.rb', line 40 def failif(action, *args) @ctx.with_timeout Bauxite::Errors::AssertionError do begin @ctx.with_vars({ '__TIMEOUT__' => 0}) do @ctx.exec_parsed_action(action, args, false) end rescue Bauxite::Errors::AssertionError, Selenium::WebDriver::Error::NoSuchElementError return true end raise Bauxite::Errors::AssertionError, "Assertion did not failed as expected:#{action} #{args.join(' ')}" end end |
#js(script, name = nil) ⇒ Object
:category: Action Methods
36 37 38 39 40 |
# File 'lib/bauxite/actions/js.rb', line 36 def js(script, name = nil) result = @ctx.driver.execute_script(script) @ctx.variables[name] = result if name true end |
#load(file, *vars) ⇒ Object
:category: Action Methods
46 47 48 |
# File 'lib/bauxite/actions/load.rb', line 46 def load(file, *vars) tryload(file, *vars) || (raise Bauxite::Errors::FileNotFoundError, "File not found: #{file}") end |
#open(url) ⇒ Object
:category: Action Methods
31 32 33 34 |
# File 'lib/bauxite/actions/open.rb', line 31 def open(url) @ctx.driver.navigate.to url true end |
#params(*vars) ⇒ Object
:category: Action Methods
32 33 34 35 36 37 38 39 |
# File 'lib/bauxite/actions/params.rb', line 32 def params(*vars) missing = vars.select { |v| (@ctx.variables[v] || '') == '' }.join(', ') if missing != '' raise Bauxite::Errors::AssertionError, "Assertion failed: the following variables must be defined and not be empty: #{missing}." end true end |
#replace(text, pattern, replacement, name) ⇒ Object
:category: Action Methods
34 35 36 |
# File 'lib/bauxite/actions/replace.rb', line 34 def replace(text, pattern, replacement, name) @ctx.variables[name] = text.gsub(_pattern(pattern), replacement) end |
#reset ⇒ Object
:category: Action Methods
34 35 36 37 |
# File 'lib/bauxite/actions/reset.rb', line 34 def reset() @ctx.reset_driver true end |
#return_action(*vars) ⇒ Object
:category: Action Methods
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/bauxite/actions/return.rb', line 58 def return_action(*vars) if vars == ['*'] @ctx.variables['__RETURN__'] = vars return true end rets = @ctx.variables['__RETURN__'] || [] @ctx.variables['__RETURN__'] = rets + vars unless rets.include? '*' true end |
#ruby(file, *vars) ⇒ Object
:category: Action Methods
50 51 52 53 54 55 56 57 |
# File 'lib/bauxite/actions/ruby.rb', line 50 def ruby(file, *vars) # _load_file_action is defined in tryload.rb _load_file_action(file, *vars) do |f| content = '' File.open(f, 'r') { |ff| content = ff.read } eval(content).call(@ctx) end || (raise Bauxite::Errors::FileNotFoundError, "File not found: #{file}") end |
#select(selector, text) ⇒ Object
:category: Action Methods
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/bauxite/actions/select.rb', line 38 def select(selector, text) @ctx.find(selector) do |e| e = Selenium::WebDriver::Support::Select.new(e) begin e.select_by(:value, text) rescue Selenium::WebDriver::Error::NoSuchElementError e.select_by(:text, text) end end end |
#set(name, value) ⇒ Object
:category: Action Methods
36 37 38 |
# File 'lib/bauxite/actions/set.rb', line 36 def set(name, value) @ctx.variables[name] = value end |
#setif(name, value, action, *args) ⇒ Object
:category: Action Methods
35 36 37 38 39 40 41 42 43 |
# File 'lib/bauxite/actions/setif.rb', line 35 def setif(name, value, action, *args) begin @ctx.exec_parsed_action(action, args, false) @ctx.variables[name] = value true rescue Bauxite::Errors::AssertionError return false end end |
#source(text) ⇒ Object
:category: Action Methods
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bauxite/actions/source.rb', line 34 def source(text) @ctx.with_timeout Bauxite::Errors::AssertionError do actual = @ctx.driver.page_source verbose = @ctx.[:verbose] ? "\nPage source:\n#{actual}" : '' unless actual =~ _pattern(text) raise Bauxite::Errors::AssertionError, "Assertion failed: page source does not match '#{text}'#{verbose}" end true end end |
#store(selector, name) ⇒ Object
:category: Action Methods
35 36 37 |
# File 'lib/bauxite/actions/store.rb', line 35 def store(selector, name) @ctx.find(selector) { |e| @ctx.variables[name] = @ctx.get_value(e) } end |
#submit(selector) ⇒ Object
:category: Action Methods
32 33 34 35 36 |
# File 'lib/bauxite/actions/submit.rb', line 32 def submit(selector) @ctx.find(selector) do |e| e.submit end end |
#test(file, name = nil, *vars) ⇒ Object
:category: Action Methods
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bauxite/actions/test.rb', line 42 def test(file, name = nil, *vars) delayed = load(file, *vars) name = name || file lambda do begin t = Time.new status = 'ERROR' error = nil @ctx.with_vars({ '__TEST__' => name }) do delayed.call status = 'OK' end rescue StandardError => e @ctx.print_error(e) error = e ensure @ctx.tests << { :name => name, :status => status, :time => Time.new - t, :error => error } end end end |
#tryload(file, *vars) ⇒ Object
:category: Action Methods
46 47 48 49 50 |
# File 'lib/bauxite/actions/tryload.rb', line 46 def tryload(file, *vars) _load_file_action(file, *vars) do |f| @ctx.exec_file(f) end end |
#wait(seconds) ⇒ Object
:category: Action Methods
31 32 33 34 35 36 37 |
# File 'lib/bauxite/actions/wait.rb', line 31 def wait(seconds) seconds = seconds.to_i seconds.times do |i| @ctx.logger.progress(seconds-i) sleep(1.0) end end |
#write(selector, text) ⇒ Object
:category: Action Methods
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bauxite/actions/write.rb', line 34 def write(selector, text) @ctx.find(selector) do |e| begin e.clear rescue Selenium::WebDriver::Error::UnknownError # user-editable... # do nothing (if this should fail, it will in the line below) end e.send_keys(text) end end |