Module: MediawikiSelenium::ScreenshotHelper

Defined in:
lib/mediawiki_selenium/helpers/screenshot_helper.rb

Overview

Adds support to Environment for taking screenshots of the current browser window. If screenshot_failures is set to true for the current environment, one is automatically taken at the end of each failed test case.

Instance Method Summary collapse

Instance Method Details

#screenshot(browser, name) ⇒ String

Takes a screenshot of the given browser window and returns the path to the saved image.

Parameters:

  • browser (Watir::Browser)

    Browser to take a sceeenshot of.

  • name (String)

    Base name to use for the saved image file.

Returns:

  • (String)

    Path to the new screenshot.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mediawiki_selenium/helpers/screenshot_helper.rb', line 18

def screenshot(browser, name)
  dir = screenshot_failures_path
  FileUtils.mkdir_p dir

  name = name.tr("#{File::SEPARATOR}\000", '-')

  path = File.join(dir, "#{name}.png")
  browser.screenshot.save(path)

  path
end

#teardown(info = {}) ⇒ Object

Takes screenshots for failed tests.

Parameters:

  • info (Hash) (defaults to: {})

    Test case information.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mediawiki_selenium/helpers/screenshot_helper.rb', line 34

def teardown(info = {})
  screenshots = []

  artifacts = super(info) do |browser|
    if info[:status] == :failed && screenshot_failures?
      screenshots << screenshot(browser, info[:name] || 'scenario')
    end

    yield browser if block_given?
  end

  artifacts.merge(screenshots.each.with_object({}) { |img, arts| arts[img] = 'image/png' })
end