browser.rb 2.89 KB
Newer Older
1
2
3
require 'rspec/core'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
4
require 'selenium-webdriver'
5
6

module QA
7
  module Runtime
8
    class Browser
9
      include QA::Scenario::Actable
10

11
12
13
      def initialize
        self.class.configure!
      end
14

15
16
17
18
19
20
21
22
23
24
25
26
27
      ##
      # Visit a page that belongs to a GitLab instance under given address.
      #
      # Example:
      #
      # visit(:gitlab, Page::Main::Login)
      # visit('http://gitlab.example/users/sign_in')
      #
      # In case of an address that is a symbol we will try to guess address
      # based on `Runtime::Scenario#something_address`.
      #
      def visit(address, page, &block)
        Browser::Session.new(address, page).tap do |session|
28
          session.perform(&block)
29
        end
30
31
      end

32
33
      def self.visit(address, page, &block)
        new.visit(address, page, &block)
34
35
      end

36
      def self.configure!
37
38
        return if Capybara.drivers.include?(:chrome)

39
40
41
        Capybara.register_driver :chrome do |app|
          capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
            'chromeOptions' => {
42
              'args' => %w[headless no-sandbox disable-gpu window-size=1280,1680]
43
44
45
46
47
48
49
            }
          )

          Capybara::Selenium::Driver
            .new(app, browser: :chrome, desired_capabilities: capabilities)
        end

50
51
52
53
        Capybara::Screenshot.register_driver(:chrome) do |driver, path|
          driver.browser.save_screenshot(path)
        end

54
55
56
        # Keep only the screenshots generated from the last failing test suite
        Capybara::Screenshot.prune_strategy = :keep_last_run

57
        Capybara.configure do |config|
58
59
          config.default_driver = :chrome
          config.javascript_driver = :chrome
Grzegorz Bizon's avatar
Grzegorz Bizon committed
60
          config.default_max_wait_time = 10
61
62
63
64
          # https://github.com/mattheworiordan/capybara-screenshot/issues/164
          config.save_path = 'tmp'
        end
      end
65
66
67
68

      class Session
        include Capybara::DSL

69
70
71
72
        def initialize(instance, page = nil)
          @instance = instance
          @address = host + page&.path
        end
73

74
75
76
77
78
79
        def host
          if @instance.is_a?(Symbol)
            Runtime::Scenario.send("#{@instance}_address")
          else
            @instance.to_s
          end
80
81
82
83
84
        end

        def perform(&block)
          visit(@address)

85
          yield if block_given?
86
        rescue
87
88
          raise if block.nil?

89
90
          # RSpec examples will take care of screenshots on their own
          #
91
          unless block.binding.receiver.is_a?(RSpec::Core::ExampleGroup)
Grzegorz Bizon's avatar
Grzegorz Bizon committed
92
            screenshot_and_save_page
93
94
95
96
97
98
99
100
101
102
103
104
105
106
          end

          raise
        ensure
          clear! if block_given?
        end

        ##
        # Selenium allows to reset session cookies for current domain only.
        #
        # See gitlab-org/gitlab-qa#102
        #
        def clear!
          visit(@address)
Grzegorz Bizon's avatar
Grzegorz Bizon committed
107
          reset_session!
108
109
        end
      end
110
111
112
    end
  end
end