Welcome to mirror list, hosted at ThFree Co, Russian Federation.

capybara_slow_finder_spec.rb « support_specs « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b0438a7a78b49b4b69770c9bde0dc4561c16849c (plain)
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
# frozen_string_literal: true

require 'fast_spec_helper'
require 'capybara'
require 'support/capybara_slow_finder'

RSpec.describe Capybara::Node::Base::SlowFinder do # rubocop:disable RSpec/FilePath
  context 'without timeout' do
    context 'when element is found' do
      let(:slow_finder) do
        Class.new do
          def synchronize(seconds = nil, errors: nil)
            true
          end

          prepend Capybara::Node::Base::SlowFinder
        end.new
      end

      it 'does not raise error' do
        expect { slow_finder.synchronize }.not_to raise_error
      end
    end

    context 'when element is not found' do
      let(:slow_finder) do
        Class.new do
          def synchronize(seconds = nil, errors: nil)
            raise Capybara::ElementNotFound
          end

          prepend Capybara::Node::Base::SlowFinder
        end.new
      end

      it 'raises Capybara::ElementNotFound error' do
        expect { slow_finder.synchronize }.to raise_error(Capybara::ElementNotFound)
      end
    end
  end

  context 'with timeout' do
    let(:timeout) { 0.01 }

    let(:slow_finder) do
      Class.new do
        def synchronize(seconds = nil, errors: nil)
          sleep 0.02

          raise Capybara::ElementNotFound
        end

        prepend Capybara::Node::Base::SlowFinder
      end.new
    end

    context 'with default timeout' do
      it 'raises a timeout error' do
        expect(Capybara).to receive(:default_max_wait_time).and_return(timeout)

        expect { slow_finder.synchronize }.to raise_error_element_not_found
      end
    end

    context 'when passed as paramater' do
      it 'raises a timeout error' do
        expect { slow_finder.synchronize(timeout) }.to raise_error_element_not_found
      end
    end

    def raise_error_element_not_found
      raise_error(
        Capybara::ElementNotFound,
        /\n\nTimeout \(#{timeout}s\) reached while running a waiting Capybara finder./
      )
    end
  end
end