summaryrefslogtreecommitdiff
path: root/qa/qa/runtime/browser.rb
blob: e05dd9612dffac2063cc373ad7186f3adcd7671c (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
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
require 'rspec/core'
require 'capybara/rspec'
require 'capybara-screenshot/rspec'
require 'selenium-webdriver'

module QA
  module Runtime
    class Browser
      include QA::Scenario::Actable

      def initialize
        self.class.configure!
      end

      ##
      # 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|
          session.perform(&block)
        end
      end

      def self.visit(address, page, &block)
        new.visit(address, page, &block)
      end

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

        Capybara.register_driver :chrome do |app|
          capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
            'chromeOptions' => {
              'args' => %w[headless no-sandbox disable-gpu window-size=1280,1680]
            }
          )

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

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

        Capybara.configure do |config|
          config.default_driver = :chrome
          config.javascript_driver = :chrome
          config.default_max_wait_time = 4
          # https://github.com/mattheworiordan/capybara-screenshot/issues/164
          config.save_path = 'tmp'
        end
      end

      class Session
        include Capybara::DSL

        def initialize(instance, page = nil)
          @instance = instance
          @address = host + page&.path
        end

        def host
          if @instance.is_a?(Symbol)
            Runtime::Scenario.send("#{@instance}_address")
          else
            @instance.to_s
          end
        end

        def perform(&block)
          visit(@address)

          block.call if block_given?
        rescue
          raise if block.nil?

          # RSpec examples will take care of screenshots on their own
          #
          unless block.binding.receiver.is_a?(RSpec::Core::ExampleGroup)
            Capybara::Screenshot.screenshot_and_save_page
          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)
          Capybara.reset_session!
        end
      end
    end
  end
end