Skip to content

Commit a8abbc6

Browse files
authored
Merge pull request #302 from el-cms/custom-prefixes
Manually save screenshots with custom prefixes
2 parents 22863bf + 85521ff commit a8abbc6

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,21 @@ Anywhere the Capybara DSL methods (visit, click etc.) are available so too are t
100100

101101
```ruby
102102
screenshot_and_save_page
103+
104+
# with custom prefix
105+
screenshot_and_save_page(filename_prefix: 'custom_prefix')
106+
107+
# image only, with custom prefix:
108+
screenshot_and_save_page(filename_prefix: 'custom_prefix', save_html: false)
103109
```
104110

105111
Or for screenshot only, which will automatically open the image:
106112

107113
```ruby
108114
screenshot_and_open_image
115+
116+
# with custom prefix
117+
screenshot_and_open_image(filename_prefix: 'custom_prefix')
109118
```
110119

111120
These are just calls on the main library methods:

lib/capybara-screenshot.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ def self.append_screenshot_path=(value)
2929
RSpec.add_link_to_screenshot_for_failed_examples = value
3030
end
3131

32-
def self.screenshot_and_save_page
33-
saver = new_saver(Capybara, Capybara.page)
32+
def self.screenshot_and_save_page(filename_prefix: nil, save_html: true)
33+
saver = new_saver(Capybara, Capybara.page, save_html, filename_prefix)
3434
if saver.save
3535
{:html => saver.html_path, :image => saver.screenshot_path}
36+
elsif !save_html
37+
warn 'WARN: no screenshot has been saved. You should configure the driver used by Capybara to save images.'
3638
end
3739
end
3840

39-
def self.screenshot_and_open_image
41+
def self.screenshot_and_open_image(filename_prefix: nil)
4042
require "launchy"
4143

42-
saver = new_saver(Capybara, Capybara.page, false)
44+
saver = new_saver(Capybara, Capybara.page, false, filename_prefix)
4345
if saver.save
4446
Launchy.open saver.screenshot_path
4547
{:html => nil, :image => saver.screenshot_path}

lib/capybara-screenshot/capybara.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ module DSL
1919
# Adds class methods to Capybara module and gets mixed into
2020
# the current scope during Cucumber and RSpec tests
2121

22-
def screenshot_and_save_page
23-
Capybara::Screenshot.screenshot_and_save_page
22+
def screenshot_and_save_page(filename_prefix: nil, save_html: true)
23+
Capybara::Screenshot.screenshot_and_save_page(filename_prefix: filename_prefix, save_html: save_html)
2424
end
2525

26-
def screenshot_and_open_image
27-
Capybara::Screenshot.screenshot_and_open_image
26+
def screenshot_and_open_image(filename_prefix: nil)
27+
Capybara::Screenshot.screenshot_and_open_image(filename_prefix: filename_prefix)
2828
end
2929
end
3030

spec/cucumber/cucumber_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ def run_case(code, options = {})
6161
expect('tmp/my_screenshot.html').to have_file_content('This is a different page')
6262
end
6363

64+
it 'saves a screenshot with custom prefix' do
65+
run_case(<<-CUCUMBER)
66+
Feature: Custom prefix
67+
Scenario: Screenshot with custom prefix
68+
Given I visit "/"
69+
And I save the page with a custom prefix
70+
CUCUMBER
71+
expect('tmp/custom_prefix.html').to have_file_content('This is the root page')
72+
end
73+
74+
it 'displays a warning when a screenshot without HTML is asked but not saved' do
75+
run_case(<<-CUCUMBER)
76+
Feature: Custom prefix
77+
Scenario: Empty screenshot
78+
Given I visit "/different_page"
79+
And I take a screenshot which is not saved
80+
CUCUMBER
81+
expect(last_command_started.output).to include('WARN: no screenshot has been saved.')
82+
end
83+
6484
context 'pruning' do
6585
before do
6686
create_screenshot_for_pruning

spec/cucumber/step_definitions/step_definitions.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@
1616
Then(/^I trigger an unhandled exception/) do
1717
raise "you can't handle me"
1818
end
19+
20+
Then(/^I save the page with a custom prefix$/) do
21+
screenshot_and_save_page filename_prefix: 'custom_prefix'
22+
end
23+
24+
Then(/^I take a screenshot which is not saved$/) do
25+
screenshot_and_save_page save_html: false
26+
end

0 commit comments

Comments
 (0)