spec_ui 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,18 +3,17 @@ Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
5
  lib/spec/ui.rb
6
- lib/spec/ui/behaviour.rb
7
- lib/spec/ui/configuration.rb
8
6
  lib/spec/ui/formatter.rb
9
- lib/spec/ui/rake_tasks.rb
10
- lib/spec/ui/rmagick_not_installed.png
11
- lib/spec/ui/screenshot_helper.rb
12
- lib/spec/ui/selenium_helper.rb
7
+ lib/spec/ui/images/rmagick_not_installed.png
8
+ lib/spec/ui/images/win32screenshot_not_installed.png
9
+ lib/spec/ui/images/wrong_win32screenshot.png
10
+ lib/spec/ui/screenshot_saver.rb
11
+ lib/spec/ui/selenium
12
+ lib/spec/ui/selenium/driver_ext.rb
13
13
  lib/spec/ui/version.rb
14
- lib/spec/ui/watir_helper.rb
15
- lib/spec/ui/webapp_helper.rb
16
- lib/spec/ui/win32screenshot_not_installed.png
17
- lib/spec/ui/wrong_win32screenshot.png
14
+ lib/spec/ui/watir.rb
15
+ lib/spec/ui/watir/browser.rb
16
+ lib/spec/ui/watir/matchers.rb
18
17
  examples/selenium/Rakefile
19
18
  examples/selenium/README.txt
20
19
  examples/selenium/spec/google/find_rspecs_homepage_spec.rb
@@ -23,3 +22,5 @@ examples/watir/Rakefile
23
22
  examples/watir/README.txt
24
23
  examples/watir/spec/google/find_rspecs_homepage_spec.rb
25
24
  examples/watir/spec/spec_helper.rb
25
+ spec/spec_helper.rb
26
+ spec/spec/ui/watir/matchers_spec.rb
data/Rakefile CHANGED
@@ -46,3 +46,7 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
46
46
  #p.extra_deps - An array of rubygem dependencies.
47
47
  #p.spec_extras - A hash of extra values to set in the gemspec.
48
48
  end
49
+
50
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../rspec/lib'
51
+ require 'spec/rake/spectask'
52
+ Spec::Rake::SpecTask.new
@@ -1,2 +1,12 @@
1
1
  require File.dirname(__FILE__) + '/spec/spec_helper'
2
- Spec::Ui.create_tasks
2
+ require 'spec/rake/spectask'
3
+
4
+ desc "Run UI Specs"
5
+ Spec::Rake::SpecTask.new('spec:ui') do |t|
6
+ t.spec_files = FileList['spec/**/*.rb']
7
+ t.spec_opts = [
8
+ '--require', 'spec/spec_helper',
9
+ '--format', 'Spec::Ui::ScreenshotFormatter:spec_report.html',
10
+ '--format', 'progress',
11
+ ]
12
+ end
@@ -29,7 +29,7 @@ describe "Google's search page" do
29
29
  end
30
30
 
31
31
  after(:each) do
32
- save_screenshot_and_source(@browser)
32
+ Spec::Ui::WebappFormatter.browser = @browser
33
33
  end
34
34
 
35
35
  after(:all) do
@@ -5,7 +5,4 @@ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../../lib')
5
5
  require 'rubygems'
6
6
  require 'spec'
7
7
  require 'spec/ui'
8
-
9
- Spec::Runner.configure do |config|
10
- config.spec_ui_report_dir = File.dirname(__FILE__) + '/report'
11
- end
8
+ require 'spec/ui/selenium'
@@ -1,2 +1,12 @@
1
1
  require File.dirname(__FILE__) + '/spec/spec_helper'
2
- Spec::Ui.create_tasks
2
+ require 'spec/rake/spectask'
3
+
4
+ desc "Run UI Specs"
5
+ Spec::Rake::SpecTask.new('spec:ui') do |t|
6
+ t.spec_files = FileList['spec/**/*.rb']
7
+ t.spec_opts = [
8
+ '--require', 'spec/spec_helper',
9
+ '--format', 'Spec::Ui::ScreenshotFormatter:spec_report.html',
10
+ '--format', 'progress',
11
+ ]
12
+ end
@@ -26,9 +26,9 @@ describe "Google's search page" do
26
26
  @browser.button(:name, "btnG").click
27
27
  @browser.should_not have_text("Ali G")
28
28
  end
29
-
29
+
30
30
  after(:each) do
31
- save_screenshot_and_source(@browser)
31
+ Spec::Ui::ScreenshotFormatter.browser = @browser
32
32
  end
33
33
 
34
34
  after(:all) do
@@ -5,7 +5,10 @@ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../../lib')
5
5
  require 'rubygems'
6
6
  require 'spec'
7
7
  require 'spec/ui'
8
+ require 'spec/ui/watir'
8
9
 
9
- Spec::Runner.configure do |config|
10
- config.spec_ui_report_dir = File.dirname(__FILE__) + '/report'
11
- end
10
+ class Spec::DSL::Behaviour
11
+ def before_eval #:nodoc:
12
+ include Spec::Matchers::Watir # This gives us Watir matchers
13
+ end
14
+ end
@@ -1,7 +1,2 @@
1
- require File.dirname(__FILE__) + '/ui/configuration'
2
- require File.dirname(__FILE__) + '/ui/behaviour'
3
- require File.dirname(__FILE__) + '/ui/webapp_helper'
4
- require File.dirname(__FILE__) + '/ui/watir_helper'
5
- require File.dirname(__FILE__) + '/ui/selenium_helper'
6
- require File.dirname(__FILE__) + '/ui/screenshot_helper'
7
- require File.dirname(__FILE__) + '/ui/rake_tasks'
1
+ require 'spec/ui/formatter'
2
+ require 'spec/ui/version'
@@ -1,14 +1,112 @@
1
+ require 'tempfile'
2
+ require 'base64'
3
+ require 'cgi'
4
+ require 'spec/ui/screenshot_saver'
5
+
1
6
  module Spec
2
7
  module Ui
3
8
  class ScreenshotFormatter < Spec::Runner::Formatter::HtmlFormatter
4
- def extra_failure_content(failure)
5
- super + " <div><a href=\"images/#{current_example_number}.png\"><img width=\"25%\" height=\"25%\" src=\"images/#{current_example_number}.png\"></a></div>\n"
9
+ class << self
10
+ include ScreenshotSaver
11
+ attr_reader :html
12
+
13
+ # Takes screenshot and snapshot of the +browser+'s html.
14
+ # This method calls #screenshot! so that method should not be called
15
+ # when this method is used.
16
+ # This method *must* be called in an after(:each) block.
17
+ def browser=(browser)
18
+ screenshot!
19
+ @html = browser.html
20
+ end
21
+
22
+ # Takes a screenshot of the current window. Use this method when
23
+ # you don't have a browser object.
24
+ def screenshot!
25
+ @png_path = Tempfile.new("spec:ui").path
26
+ save_screenshot(png_path)
27
+ end
28
+
29
+ def png_path
30
+ raise "Screenshot not taken. You must call #{self.class}#screenshot! or #{self.class}#browser=(@browser) from after(:each)" if @png_path.nil?
31
+ @png_path
32
+ end
33
+
34
+ # Resets the screenshot and html. Do not call this method from your specs.
35
+ def reset!
36
+ @png_path = nil
37
+ @html = nil
38
+ end
39
+ end
40
+
41
+ def global_scripts
42
+ super + <<-EOF
43
+ function showImage(e) {
44
+ w = window.open();
45
+ w.location = e.childNodes[0].src
46
+ }
47
+
48
+ // Lifted from Ruby RDoc
49
+ function toggleSource( id ) {
50
+ var elem
51
+ var link
52
+
53
+ if( document.getElementById )
54
+ {
55
+ elem = document.getElementById( id )
56
+ link = document.getElementById( "l_" + id )
57
+ }
58
+ else if ( document.all )
59
+ {
60
+ elem = eval( "document.all." + id )
61
+ link = eval( "document.all.l_" + id )
62
+ }
63
+ else
64
+ return false;
65
+
66
+ if( elem.style.display == "block" )
67
+ {
68
+ elem.style.display = "none"
69
+ link.innerHTML = "show source"
70
+ }
71
+ else
72
+ {
73
+ elem.style.display = "block"
74
+ link.innerHTML = "hide source"
75
+ }
76
+ }
77
+ EOF
78
+ end
79
+
80
+ def global_styles
81
+ super + <<-EOF
82
+ div.rspec-report textarea {
83
+ width: 100%;
84
+ }
85
+
86
+ div.rspec-report div.dyn-source {
87
+ background:#FFFFEE none repeat scroll 0%;
88
+ border:1px dotted black;
89
+ color:#000000;
90
+ display:none;
91
+ margin:0.5em 2em;
92
+ padding:0.5em;
93
+ }
94
+ EOF
6
95
  end
7
- end
8
96
 
9
- class WebappFormatter < ScreenshotFormatter
10
97
  def extra_failure_content(failure)
11
- super + " <div><a href=\"images/#{current_example_number}.html\">HTML source</a></div>\n"
98
+ result = super(failure)
99
+ # Add embedded image to the report.
100
+ img_data = Base64.encode64(File.open(self.class.png_path, "rb").read)
101
+ result += " <div><a href=\"#\" onclick=\"showImage(this)\"><img width=\"25%\" height=\"25%\" src=\"data:image/png;base64,#{img_data}\" /></a></div>\n"
102
+ if self.class.html
103
+ escaped_html = CGI::escapeHTML(self.class.html)
104
+ source_id = "#{current_example_number}_source"
105
+ result += " <div>[<a id=\"l_#{source_id}\" href=\"javascript:toggleSource('#{source_id}')\">show source</a>]</div>\n"
106
+ result += " <div id=\"#{source_id}\" class=\"dyn-source\"><textarea rows=\"20\">#{escaped_html}</textarea></div>\n"
107
+ end
108
+ self.class.reset!
109
+ result
12
110
  end
13
111
  end
14
112
  end
@@ -2,34 +2,25 @@ require 'fileutils'
2
2
 
3
3
  module Spec
4
4
  module Ui
5
- module ScreenshotHelper
6
- def md(dir)
7
- FileUtils.mkdir_p(dir) unless File.directory?(dir)
8
- end
9
-
5
+ module ScreenshotSaver
10
6
  if RUBY_PLATFORM =~ /darwin/
11
- def save_screenshot(dir, spec_number)
12
- md(dir)
13
- img_path = "#{dir}/#{spec_number}.png"
7
+ def save_screenshot(png_path)
14
8
  # How do we capture the current window??
15
- `screencapture #{img_path}`
9
+ `screencapture #{png_path}`
16
10
  end
17
11
  else # Win32
18
12
  begin
13
+ # TODO: Move all this code to win32screenshot
19
14
  require 'rubygems'
20
15
  require 'RMagick'
21
16
  gem 'win32screenshot', '>=0.0.2'
22
17
  require 'win32screenshot'
23
- def save_screenshot(dir, spec_number)
24
- md(dir)
18
+ def save_screenshot(png_path)
25
19
  width, height, bmp = ::Win32::Screenshot.foreground
26
20
  begin
27
21
  img = Magick::Image.from_blob(bmp)[0]
28
- img_path = "#{dir}/#{spec_number}.png"
29
- dir = File.dirname(img_path)
30
- FileUtils.mkdir_p(dir) unless File.exist?(dir)
31
22
  img.write(img_path)
32
- img
23
+ nil
33
24
  rescue Magick::ImageMagickError => e
34
25
  if e.message =~ /Insufficient image data in file/
35
26
  e.message << "\nTry this:\n1) Open your app (e.g. Internet Explorer)\n2) Resize the app to be bigger (without maximizing).\n3) close it so Windows remembers its size.\n" +
@@ -39,20 +30,17 @@ module Spec
39
30
  end
40
31
  end
41
32
  rescue Gem::LoadError => e
42
- def save_screenshot(dir, spec_number)
43
- md(dir)
44
- FileUtils.cp(File.dirname(__FILE__) + '/wrong_win32screenshot.png', "#{dir}/#{spec_number}.png")
33
+ def save_screenshot(png_path)
34
+ FileUtils.cp(File.dirname(__FILE__) + '/images/wrong_win32screenshot.png', png_path)
45
35
  end
46
36
  rescue LoadError => e
47
37
  if(e.message =~ /win32screenshot/)
48
- def save_screenshot(dir, spec_number)
49
- md(dir)
50
- FileUtils.cp(File.dirname(__FILE__) + '/win32screenshot_not_installed.png', "#{dir}/#{spec_number}.png")
38
+ def save_screenshot(png_path)
39
+ FileUtils.cp(File.dirname(__FILE__) + '/images/win32screenshot_not_installed.png', png_path)
51
40
  end
52
41
  else
53
- def save_screenshot(dir, spec_number)
54
- md(dir)
55
- FileUtils.cp(File.dirname(__FILE__) + '/rmagick_not_installed.png', "#{dir}/#{spec_number}.png")
42
+ def save_screenshot(png_path)
43
+ FileUtils.cp(File.dirname(__FILE__) + '/images/rmagick_not_installed.png', png_path)
56
44
  end
57
45
  end
58
46
  end
@@ -2,7 +2,7 @@ module Spec
2
2
  module Ui
3
3
  module VERSION #:nodoc:
4
4
  MAJOR = 0
5
- MINOR = 1
5
+ MINOR = 2
6
6
  TINY = 0
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
@@ -0,0 +1,2 @@
1
+ require 'spec/ui/watir/browser'
2
+ require 'spec/ui/watir/matchers'
@@ -0,0 +1,37 @@
1
+ require 'rubygems'
2
+
3
+ if RUBY_PLATFORM =~ /darwin/
4
+ require 'safariwatir'
5
+ Watir::Browser = Watir::Safari
6
+ else
7
+ require 'watir'
8
+ Watir::Browser = Watir::IE
9
+
10
+ class Watir::Browser
11
+ alias old_initialize initialize
12
+ # Brings the IE to the foreground (provided Win32::Screenshot is installed)
13
+ def initialize
14
+ result = old_initialize
15
+ ::Win32::Screenshot.setForegroundWindow(self.getIE.hwnd) rescue nil
16
+ result
17
+ end
18
+ end
19
+ end
20
+
21
+ class Watir::Browser
22
+ def kill!
23
+ close
24
+ end
25
+
26
+ alias _old_goto goto
27
+ # Redefinition of Watir's original goto, which gives a better
28
+ # exception message (the URL is in the message)
29
+ def goto(url)
30
+ begin
31
+ _old_goto(url)
32
+ rescue => e
33
+ e.message << "\nURL: #{url}"
34
+ raise e
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,97 @@
1
+ # Matchers for Watir::IE/Watir::Safari instances
2
+ module Spec::Matchers::Watir
3
+ class ContentMatcher # :nodoc
4
+ def initialize(kind, text_or_regexp)
5
+ @kind, @text_or_regexp = kind, text_or_regexp
6
+ end
7
+
8
+ def matches?(container)
9
+ @container = container
10
+ @content = container.__send__(@kind)
11
+ if @text_or_regexp.is_a?(Regexp)
12
+ !!@content =~ @text_or_regexp
13
+ else
14
+ !!@content.index(@text_or_regexp.to_s)
15
+ end
16
+ end
17
+
18
+ def failure_message
19
+ "Expected #{@container.class} to have #{@kind} matching #{@text_or_regexp}, but it was not found in:\n#{@content}"
20
+ end
21
+
22
+ def negative_failure_message
23
+ "Expected #{@container.class} to not have #{@kind} matching #{@text_or_regexp}, but it was found in:\n#{@content}"
24
+ end
25
+ end
26
+
27
+ # RSpec matcher that passes if @container#text matches +text_or_regexp+ (String or Regexp)
28
+ def have_text(text_or_regexp)
29
+ ContentMatcher.new(:text, text_or_regexp)
30
+ end
31
+
32
+ # RSpec matcher that passes if @container#html matches +text_or_regexp+ (String or Regexp)
33
+ def have_html(text_or_regexp)
34
+ ContentMatcher.new(:html, text_or_regexp)
35
+ end
36
+
37
+ class ElementMatcher # :nodoc
38
+ def initialize(kind, *args)
39
+ @kind, @args = kind, args
40
+ end
41
+
42
+ def matches?(container)
43
+ @container = container
44
+ begin
45
+ element = @container.__send__(@kind, *@args)
46
+ if element.respond_to?(:assert_exists)
47
+ # IE
48
+ element.assert_exists
49
+ true
50
+ else
51
+ # Safari
52
+ element.exists?
53
+ end
54
+ rescue ::Watir::Exception::UnknownObjectException => e
55
+ false
56
+ end
57
+ end
58
+
59
+ def failure_message
60
+ "Expected #{@container.class} to have #{@kind}(#{arg_string}), but it was not found"
61
+ end
62
+
63
+ def negative_failure_message
64
+ "Expected #{@container.class} to not have #{@kind}(#{arg_string}), but it was found"
65
+ end
66
+
67
+ def arg_string
68
+ @args.map{|a| a.inspect}.join(", ")
69
+ end
70
+ end
71
+
72
+ # All the xxx(what, how) methods in Watir
73
+ [
74
+ :button,
75
+ :cell,
76
+ :checkbox,
77
+ :div,
78
+ :file_field,
79
+ :form,
80
+ :hidden,
81
+ :image,
82
+ :label,
83
+ :link,
84
+ :p,
85
+ :radio,
86
+ :row,
87
+ :select_list,
88
+ :span,
89
+ :table,
90
+ :text_field
91
+ ].each do |kind|
92
+ # RSpec matcher that passes if container##{kind}(*args) returns an existing #{kind} element.
93
+ define_method("have_#{kind}".to_sym) do |*args|
94
+ ElementMatcher.new(kind, *args)
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../../../spec_helper'
2
+ require 'spec/ui/watir/matchers'
3
+
4
+ class BrowserStub
5
+ def label(*args)
6
+ LabelStub.new(*args)
7
+ end
8
+ end
9
+
10
+ class LabelStub
11
+ def initialize(how, what)
12
+ @how, @what = how, what
13
+ end
14
+
15
+ def exists?
16
+ @what
17
+ end
18
+ end
19
+
20
+ describe "Browser" do
21
+ include Spec::Matchers::Watir
22
+
23
+ before do
24
+ @b = BrowserStub.new
25
+ end
26
+
27
+ it "should support should have_label" do
28
+ lambda do
29
+ @b.should have_label(:foo, false)
30
+ end.should raise_error(Spec::Expectations::ExpectationNotMetError,
31
+ 'Expected BrowserStub to have label(:foo, false), but it was not found')
32
+ end
33
+
34
+ it "should support should_not have_label" do
35
+ lambda do
36
+ @b.should_not have_label(:foo, true)
37
+ end.should raise_error(Spec::Expectations::ExpectationNotMetError,
38
+ 'Expected BrowserStub to not have label(:foo, true), but it was found')
39
+ end
40
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require 'spec/ui'
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.1
2
+ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: spec_ui
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2007-05-01 00:00:00 +02:00
6
+ version: 0.2.0
7
+ date: 2007-05-10 00:00:00 +02:00
8
8
  summary: Run UI RSpec examples with screenshot reports
9
9
  require_paths:
10
10
  - lib
@@ -34,18 +34,17 @@ files:
34
34
  - README.txt
35
35
  - Rakefile
36
36
  - lib/spec/ui.rb
37
- - lib/spec/ui/behaviour.rb
38
- - lib/spec/ui/configuration.rb
39
37
  - lib/spec/ui/formatter.rb
40
- - lib/spec/ui/rake_tasks.rb
41
- - lib/spec/ui/rmagick_not_installed.png
42
- - lib/spec/ui/screenshot_helper.rb
43
- - lib/spec/ui/selenium_helper.rb
38
+ - lib/spec/ui/images/rmagick_not_installed.png
39
+ - lib/spec/ui/images/win32screenshot_not_installed.png
40
+ - lib/spec/ui/images/wrong_win32screenshot.png
41
+ - lib/spec/ui/screenshot_saver.rb
42
+ - lib/spec/ui/selenium
43
+ - lib/spec/ui/selenium/driver_ext.rb
44
44
  - lib/spec/ui/version.rb
45
- - lib/spec/ui/watir_helper.rb
46
- - lib/spec/ui/webapp_helper.rb
47
- - lib/spec/ui/win32screenshot_not_installed.png
48
- - lib/spec/ui/wrong_win32screenshot.png
45
+ - lib/spec/ui/watir.rb
46
+ - lib/spec/ui/watir/browser.rb
47
+ - lib/spec/ui/watir/matchers.rb
49
48
  - examples/selenium/Rakefile
50
49
  - examples/selenium/README.txt
51
50
  - examples/selenium/spec/google/find_rspecs_homepage_spec.rb
@@ -54,6 +53,8 @@ files:
54
53
  - examples/watir/README.txt
55
54
  - examples/watir/spec/google/find_rspecs_homepage_spec.rb
56
55
  - examples/watir/spec/spec_helper.rb
56
+ - spec/spec_helper.rb
57
+ - spec/spec/ui/watir/matchers_spec.rb
57
58
  test_files: []
58
59
 
59
60
  rdoc_options: []
@@ -1,6 +0,0 @@
1
- class Spec::DSL::Behaviour
2
- def before_eval #:nodoc:
3
- include Spec::Ui::WebappHelper
4
- include Spec::Watir # This gives us Watir matchers
5
- end
6
- end
@@ -1,7 +0,0 @@
1
- class Spec::DSL::Configuration
2
- attr_accessor :spec_ui_report_dir
3
-
4
- def spec_ui_image_dir
5
- File.join(spec_ui_report_dir, 'images')
6
- end
7
- end
@@ -1,31 +0,0 @@
1
- module Spec
2
- module Ui
3
- def self.create_tasks
4
- require 'spec/rake/spectask'
5
-
6
- if Spec::Runner.configuration.spec_ui_report_dir.nil?
7
- STDERR.puts <<-EOF
8
- You must tell Spec::Ui where the report should go.
9
- Please put the following in your spec/spec_helper.rb file:
10
-
11
- require 'spec'
12
- require 'spec/ui'
13
-
14
- Spec::Runner.configure do |config|
15
- config.spec_ui_report_dir = File.dirname(__FILE__) + '/report'
16
- end
17
- EOF
18
- end
19
-
20
- FileUtils.rm_rf(Spec::Runner.configuration.spec_ui_report_dir) if File.exist?(Spec::Runner.configuration.spec_ui_report_dir)
21
- FileUtils.mkdir_p(Spec::Runner.configuration.spec_ui_report_dir)
22
-
23
- desc "Run UI Specs"
24
- Spec::Rake::SpecTask.new('spec:ui') do |t|
25
- t.spec_files = FileList['spec/**/*.rb']
26
- t.spec_opts = ['--require', 'spec/spec_helper', '--format', 'Spec::Ui::WebappFormatter']
27
- t.out = "#{Spec::Runner.configuration.spec_ui_report_dir}/index.html"
28
- end
29
- end
30
- end
31
- end
@@ -1,167 +0,0 @@
1
- require File.dirname(__FILE__) + '/webapp_helper'
2
- require 'rubygems'
3
-
4
- if RUBY_PLATFORM =~ /darwin/
5
- require 'safariwatir'
6
- Watir::Browser = Watir::Safari
7
- else
8
- require 'watir'
9
- Watir::Browser = Watir::IE
10
-
11
- class Watir::Browser
12
- alias old_initialize initialize
13
- # Brings the IE to the foreground (provided Win32::Screenshot is installed)
14
- def initialize
15
- result = old_initialize
16
- ::Win32::Screenshot.setForegroundWindow(self.getIE.hwnd) rescue nil
17
- result
18
- end
19
- end
20
- end
21
-
22
- class Watir::Browser
23
- def kill!
24
- close
25
- end
26
-
27
- alias contain_text? contains_text
28
-
29
- alias old_goto goto
30
- # Redefinition of Watir's original goto, which gives a better
31
- # exception message (the URL is in the message)
32
- def goto(url)
33
- begin
34
- old_goto(url)
35
- rescue => e
36
- e.message << "\nURL: #{url}"
37
- raise e
38
- end
39
- end
40
- end
41
-
42
- module Spec
43
- # Matchers for Watir::IE/Watir::Safari instances
44
- module Watir
45
- # RSpec matcher that passes if @browser#text matches +text+ (String or Regexp)
46
- def have_text(text)
47
- Spec::Watir::HaveText.new(text)
48
- end
49
-
50
- class HaveText # :nodoc
51
- def initialize(text_or_regexp)
52
- @text_or_regexp = text_or_regexp
53
- end
54
-
55
- def matches?(browser)
56
- @browser = browser
57
- if @text_or_regexp.is_a?(Regexp)
58
- !!browser.text =~ @text_or_regexp
59
- else
60
- !!browser.text.index(@text_or_regexp.to_s)
61
- end
62
- end
63
-
64
- def failure_message
65
- "Expected browser to have text matching #{@text_or_regexp}, but it was not found in:\n#{@browser.text}"
66
- end
67
-
68
- def negative_failure_message
69
- "Expected browser to not have text matching #{@text_or_regexp}, but it was found in:\n#{@browser.text}"
70
- end
71
- end
72
-
73
- # RSpec matcher that passes if @browser#html matches +text+ (String or Regexp)
74
- def have_html(text)
75
- Spec::Watir::HaveHtml.new(text)
76
- end
77
-
78
- class HaveHtml # :nodoc
79
- def initialize(text_or_regexp)
80
- @text_or_regexp = text_or_regexp
81
- end
82
-
83
- def matches?(browser)
84
- @browser = browser
85
- if @text_or_regexp.is_a?(Regexp)
86
- !!browser.html =~ @text_or_regexp
87
- else
88
- !!browser.html.index(@text_or_regexp.to_s)
89
- end
90
- end
91
-
92
- def failure_message
93
- "Expected browser to have HTML matching #{@text_or_regexp}, but it was not found in:\n#{@browser.html}"
94
- end
95
-
96
- def negative_failure_message
97
- "Expected browser to not have HTML matching #{@text_or_regexp}, but it was found in:\n#{@browser.html}"
98
- end
99
- end
100
-
101
- # RSpec matcher that passes if @browser#link(+how+,+what+) returns an existing link.
102
- def have_link(how, what)
103
- Spec::Watir::HaveLink.new(how, what)
104
- end
105
-
106
- class HaveLink # :nodoc
107
- def initialize(how, what)
108
- @how, @what = how, what
109
- end
110
-
111
- def matches?(browser)
112
- @browser = browser
113
- begin
114
- link = @browser.link(@how, @what)
115
- if link.respond_to?(:assert_exists)
116
- # IE
117
- link.assert_exists
118
- true
119
- else
120
- # Safari
121
- link.exists?
122
- end
123
- rescue ::Watir::Exception::UnknownObjectException => e
124
- false
125
- end
126
- end
127
-
128
- def failure_message
129
- "Expected browser to have link(#{@how}, #{@what}), but it was not found"
130
- end
131
-
132
- def negative_failure_message
133
- "Expected browser not to have link(#{@how}, #{@what}), but it was found"
134
- end
135
- end
136
-
137
- # RSpec matcher that passes if @browser#text_field(+how+,+what+) returns an existing text field.
138
- def have_text_field(how, what)
139
- Spec::Watir::HaveTextField.new(how, what)
140
- end
141
-
142
- class HaveTextField # :nodoc
143
- def initialize(how, what)
144
- @how, @what = how, what
145
- end
146
-
147
- def matches?(browser)
148
- @browser = browser
149
- begin
150
- text_field = @browser.text_field(@how, @what)
151
- text_field.assert_exists
152
- true
153
- rescue ::Watir::Exception::UnknownObjectException => e
154
- false
155
- end
156
- end
157
-
158
- def failure_message
159
- "Expected browser to have text_field(#{@how}, #{@what}), but it was not found"
160
- end
161
-
162
- def negative_failure_message
163
- "Expected browser not to have text_field(#{@how}, #{@what}), but it was found"
164
- end
165
- end
166
- end
167
- end
@@ -1,23 +0,0 @@
1
- require 'spec'
2
- require File.dirname(__FILE__) + '/screenshot_helper'
3
- require File.dirname(__FILE__) + '/formatter'
4
-
5
- module Spec
6
- module Ui
7
- module WebappHelper
8
- include Spec::Ui::ScreenshotHelper
9
- @@spec_number = 0
10
-
11
- # Call this method from your teardown block to have source and screenshot written to disk.
12
- def save_screenshot_and_source(browser)
13
- save_screenshot(Spec::Runner.configuration.spec_ui_image_dir, @@spec_number)
14
- save_source(Spec::Runner.configuration.spec_ui_image_dir, @@spec_number, browser.html)
15
- @@spec_number += 1
16
- end
17
-
18
- def save_source(dir, spec_number, html)
19
- File.open("#{dir}/#{spec_number}.html", "w") {|io| io.write(html)}
20
- end
21
- end
22
- end
23
- end