logging_spec.rb 6.1 KB
Newer Older
M
Mark Lapierre 已提交
1 2 3
# frozen_string_literal: true

require 'capybara/dsl'
M
Mark Lapierre 已提交
4
require 'logger'
M
Mark Lapierre 已提交
5 6

describe QA::Support::Page::Logging do
7
  let(:page) { double.as_null_object }
M
Mark Lapierre 已提交
8 9

  before do
M
Mark Lapierre 已提交
10
    logger = ::Logger.new $stdout
11 12 13
    logger.level = ::Logger::DEBUG
    QA::Runtime::Logger.logger = logger

M
Mark Lapierre 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
    allow(Capybara).to receive(:current_session).and_return(page)
    allow(page).to receive(:current_url).and_return('http://current-url')
    allow(page).to receive(:has_css?).with(any_args).and_return(true)
  end

  subject do
    Class.new(QA::Page::Base) do
      prepend QA::Support::Page::Logging
    end.new
  end

  it 'logs refresh' do
    expect { subject.refresh }
      .to output(%r{refreshing http://current-url}).to_stdout_from_any_process
  end

  it 'logs wait' do
31
    expect { subject.wait_until(max_duration: 0) {} }
M
Mark Lapierre 已提交
32
      .to output(/next wait uses reload: true/).to_stdout_from_any_process
33
    expect { subject.wait_until(max_duration: 0) {} }
34
      .to output(/with wait_until/).to_stdout_from_any_process
35
    expect { subject.wait_until(max_duration: 0) {} }
36
      .to output(/ended wait_until$/).to_stdout_from_any_process
M
Mark Lapierre 已提交
37 38
  end

M
Mark Lapierre 已提交
39
  it 'logs wait with reload false' do
40
    expect { subject.wait_until(max_duration: 0, reload: false) {} }
M
Mark Lapierre 已提交
41
      .to output(/next wait uses reload: false/).to_stdout_from_any_process
42
    expect { subject.wait_until(max_duration: 0, reload: false) {} }
43
      .to output(/with wait_until/).to_stdout_from_any_process
44
    expect { subject.wait_until(max_duration: 0, reload: false) {} }
45
      .to output(/ended wait_until$/).to_stdout_from_any_process
M
Mark Lapierre 已提交
46 47
  end

M
Mark Lapierre 已提交
48 49 50 51 52 53 54 55 56 57 58 59
  it 'logs scroll_to' do
    expect { subject.scroll_to(:element) }
      .to output(/scrolling to :element/).to_stdout_from_any_process
  end

  it 'logs asset_exists?' do
    expect { subject.asset_exists?('http://asset-url') }
      .to output(%r{asset_exists\? http://asset-url returned false}).to_stdout_from_any_process
  end

  it 'logs find_element' do
    expect { subject.find_element(:element) }
60 61 62 63 64
      .to output(/finding :element/).to_stdout_from_any_process
    expect { subject.find_element(:element) }
      .to output(/found :element/).to_stdout_from_any_process
  end

S
Sanad Liaquat 已提交
65 66
  it 'logs find_element with text' do
    expect { subject.find_element(:element, text: 'foo') }
M
Mark Lapierre 已提交
67
      .to output(/finding :element with args {:text=>"foo"}/).to_stdout_from_any_process
S
Sanad Liaquat 已提交
68
    expect { subject.find_element(:element, text: 'foo') }
M
Mark Lapierre 已提交
69 70 71
      .to output(/found :element/).to_stdout_from_any_process
  end

M
Mark Lapierre 已提交
72 73 74 75 76 77 78 79 80 81
  it 'logs find_element with wait' do
    expect { subject.find_element(:element, wait: 0) }
      .to output(/finding :element with args {:wait=>0}/).to_stdout_from_any_process
  end

  it 'logs find_element with class' do
    expect { subject.find_element(:element, class: 'active') }
      .to output(/finding :element with args {:class=>\"active\"}/).to_stdout_from_any_process
  end

M
Mark Lapierre 已提交
82 83 84 85 86 87 88 89 90 91 92 93
  it 'logs click_element' do
    expect { subject.click_element(:element) }
      .to output(/clicking :element/).to_stdout_from_any_process
  end

  it 'logs fill_element' do
    expect { subject.fill_element(:element, 'foo') }
      .to output(/filling :element with "foo"/).to_stdout_from_any_process
  end

  it 'logs has_element?' do
    expect { subject.has_element?(:element) }
94
      .to output(/has_element\? :element \(wait: #{QA::Runtime::Browser::CAPYBARA_MAX_WAIT_TIME}\) returned: true/).to_stdout_from_any_process
S
Sanad Liaquat 已提交
95 96 97 98
  end

  it 'logs has_element? with text' do
    expect { subject.has_element?(:element, text: "some text") }
99
      .to output(/has_element\? :element with text \"some text\" \(wait: #{QA::Runtime::Browser::CAPYBARA_MAX_WAIT_TIME}\) returned: true/).to_stdout_from_any_process
100 101
  end

102 103 104 105
  it 'logs has_no_element?' do
    allow(page).to receive(:has_no_css?).and_return(true)

    expect { subject.has_no_element?(:element) }
106
      .to output(/has_no_element\? :element \(wait: #{QA::Runtime::Browser::CAPYBARA_MAX_WAIT_TIME}\) returned: true/).to_stdout_from_any_process
M
Mark Lapierre 已提交
107 108 109 110 111 112
  end

  it 'logs has_no_element? with text' do
    allow(page).to receive(:has_no_css?).and_return(true)

    expect { subject.has_no_element?(:element, text: "more text") }
113
      .to output(/has_no_element\? :element with text \"more text\" \(wait: #{QA::Runtime::Browser::CAPYBARA_MAX_WAIT_TIME}\) returned: true/).to_stdout_from_any_process
114 115 116 117 118 119
  end

  it 'logs has_text?' do
    allow(page).to receive(:has_text?).and_return(true)

    expect { subject.has_text? 'foo' }
120
      .to output(/has_text\?\('foo', wait: #{QA::Runtime::Browser::CAPYBARA_MAX_WAIT_TIME}\) returned true/).to_stdout_from_any_process
121 122
  end

123
  it 'logs has_no_text?' do
124
    allow(page).to receive(:has_no_text?).with('foo', any_args).and_return(true)
125 126

    expect { subject.has_no_text? 'foo' }
127
      .to output(/has_no_text\?\('foo', wait: #{QA::Runtime::Browser::CAPYBARA_MAX_WAIT_TIME}\) returned true/).to_stdout_from_any_process
M
Mark Lapierre 已提交
128 129 130 131 132 133 134
  end

  it 'logs finished_loading?' do
    expect { subject.finished_loading? }
      .to output(/waiting for loading to complete\.\.\./).to_stdout_from_any_process
    expect { subject.finished_loading? }
      .to output(/loading complete after .* seconds$/).to_stdout_from_any_process
M
Mark Lapierre 已提交
135 136 137
  end

  it 'logs within_element' do
W
Walmyr 已提交
138
    expect { subject.within_element(:element, text: nil) }
M
Mark Lapierre 已提交
139
      .to output(/within element :element/).to_stdout_from_any_process
W
Walmyr 已提交
140
    expect { subject.within_element(:element, text: nil) }
M
Mark Lapierre 已提交
141 142 143 144 145 146 147
      .to output(/end within element :element/).to_stdout_from_any_process
  end

  context 'all_elements' do
    it 'logs the number of elements found' do
      allow(page).to receive(:all).and_return([1, 2])

148
      expect { subject.all_elements(:element, count: 2) }
M
Mark Lapierre 已提交
149
        .to output(/finding all :element/).to_stdout_from_any_process
150
      expect { subject.all_elements(:element, count: 2) }
M
Mark Lapierre 已提交
151 152 153 154 155 156
        .to output(/found 2 :element/).to_stdout_from_any_process
    end

    it 'logs 0 if no elements are found' do
      allow(page).to receive(:all).and_return([])

157
      expect { subject.all_elements(:element, count: 1) }
M
Mark Lapierre 已提交
158
        .to output(/finding all :element/).to_stdout_from_any_process
159
      expect { subject.all_elements(:element, count: 1) }
M
Mark Lapierre 已提交
160 161 162 163
        .not_to output(/found 0 :elements/).to_stdout_from_any_process
    end
  end
end