File: save_page_spec.rb

package info (click to toggle)
ruby-capybara 3.40.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,368 kB
  • sloc: ruby: 23,988; javascript: 752; makefile: 11
file content (110 lines) | stat: -rw-r--r-- 3,434 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# frozen_string_literal: true

Capybara::SpecHelper.spec '#save_page' do
  let(:alternative_path) { File.join(Dir.pwd, 'save_and_open_page_tmp') }
  before do
    @old_save_path = Capybara.save_path
    Capybara.save_path = nil
    @session.visit('/foo')
  end

  after do
    Capybara.save_path = @old_save_path
    Dir.glob('capybara-*.html').each do |file|
      FileUtils.rm(file)
    end
    FileUtils.rm_rf alternative_path
  end

  it 'saves the page in the root directory' do
    @session.save_page
    path = Dir.glob('capybara-*.html').first
    expect(File.read(path)).to include('Another World')
  end

  it 'generates a sensible filename' do
    @session.save_page
    filename = Dir.glob('capybara-*.html').first
    expect(filename).to match(/^capybara-\d+\.html$/)
  end

  it 'can store files in a specified directory' do
    Capybara.save_path = alternative_path
    @session.save_page
    path = Dir.glob("#{alternative_path}/capybara-*.html").first
    expect(File.read(path)).to include('Another World')
  end

  it 'uses the given filename' do
    @session.save_page('capybara-001122.html')
    expect(File.read('capybara-001122.html')).to include('Another World')
  end

  it 'can store files in a specified directory with a given filename' do
    Capybara.save_path = alternative_path
    @session.save_page('capybara-001133.html')
    path = "#{alternative_path}/capybara-001133.html"
    expect(File.read(path)).to include('Another World')
  end

  it 'can store files in a specified directory with a given relative filename' do
    Capybara.save_path = alternative_path
    @session.save_page('tmp/capybara-001144.html')
    path = "#{alternative_path}/tmp/capybara-001144.html"
    expect(File.read(path)).to include('Another World')
  end

  it 'returns an absolute path in pwd' do
    result = @session.save_page
    path = File.expand_path(Dir.glob('capybara-*.html').first, Dir.pwd)
    expect(result).to eq(path)
  end

  it 'returns an absolute path in given directory' do
    Capybara.save_path = alternative_path
    result = @session.save_page
    path = File.expand_path(Dir.glob("#{alternative_path}/capybara-*.html").first, alternative_path)
    expect(result).to eq(path)
  end

  context 'asset_host contains a string' do
    before { Capybara.asset_host = 'http://example.com' }

    after { Capybara.asset_host = nil }

    it 'prepends base tag with value from asset_host to the head' do
      @session.visit('/with_js')
      path = @session.save_page

      result = File.read(path)
      expect(result).to include("<head><base href='http://example.com' />")
    end

    it "doesn't prepend base tag to pages when asset_host is nil" do
      Capybara.asset_host = nil
      @session.visit('/with_js')
      path = @session.save_page

      result = File.read(path)
      expect(result).to include('<html')
      expect(result).not_to include('http://example.com')
    end

    it "doesn't prepend base tag to pages which already have it" do
      @session.visit('/with_base_tag')
      path = @session.save_page

      result = File.read(path)
      expect(result).to include('<html')
      expect(result).not_to include('http://example.com')
    end

    it 'executes successfully even if the page is missing a <head>' do
      @session.visit('/with_simple_html')
      path = @session.save_page

      result = File.read(path)
      expect(result).to include('Bar')
    end
  end
end