Module: WatiRspec::SpecHelper

Includes:
Waiter
Defined in:
lib/watirspec/spec_helper.rb

Overview

main helper module

these methods can be used in specs directly

Instance Method Summary collapse

Methods included from Waiter

#wait_until, #wait_until!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arg) ⇒ Object

:nodoc:



63
64
65
# File 'lib/watirspec/spec_helper.rb', line 63

def method_missing name, *arg #:nodoc:
  @browser.respond_to?(name) ? @browser.send(name, *arg) : super
end

Instance Method Details

#download_file(file_name) ⇒ Object

downloads file with browser

you need to use click_no_wait to use this method:

button(:id => "something").click_no_wait # opens a browser save as dialog
download_file("document.pdf")
  • raises an exception if saving the file is unsuccessful

  • returns absolute file_path of the saved file



29
30
31
32
33
34
35
36
# File 'lib/watirspec/spec_helper.rb', line 29

def download_file file_name
  AutoItHelper.click_save
  file_path = native_file_path(file_path(file_name))
  AutoItHelper.set_edit(file_path)
  AutoItHelper.click_save("Save As")
  wait_until! {File.exists?(file_path)}
  file_path
end

#file_path(file_name, description = nil) ⇒ Object

returns unique file path for use in examples.

all file names generated with this method will be shown on the report upon test failure.



47
48
49
50
51
52
53
54
# File 'lib/watirspec/spec_helper.rb', line 47

def file_path(file_name, description=nil)
  formatter.file_path(file_name, description)
rescue
  extension = File.extname(file_name)
  basename = File.basename(file_name, extension)
  file_path = File.join(Dir.pwd, "#{basename}_#{Time.now.strftime("%H%M%S")}#{extension}")
  file_path
end

#formatterObject

returns WatiRspec::HtmlFormatter object, nil if not in use



39
40
41
# File 'lib/watirspec/spec_helper.rb', line 39

def formatter
  @formatter ||= Spec::Runner.options.formatters.find {|f| f.kind_of?(WatiRspec::HtmlFormatter) rescue false}
end

#native_file_path(file_path) ⇒ Object

returns native file path e.g. on Windows:

native_file_path("c:/blah/blah2/file.txt") => c:\\blah\\blah2\\file.txt


59
60
61
# File 'lib/watirspec/spec_helper.rb', line 59

def native_file_path(file_path)
  File::ALT_SEPARATOR ? file_path.gsub(File::SEPARATOR, File::ALT_SEPARATOR) : file_path
end

#open_browser_at(url) ⇒ Object

opens the browser at specified url



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/watirspec/spec_helper.rb', line 9

def open_browser_at url
  @browser = Watir::Browser.new
  @browser.speed = :fast
  add_checker Watir::PageCheckers::JAVASCRIPT_ERRORS_CHECKER
  begin
    formatter.browser = @browser
  rescue
  end
  goto url
  maximize
end