1# frozen_string_literal: true
2
3module QA
4  module Page
5    module Main
6      class Menu < Page::Base
7        prepend Mobile::Page::Main::Menu if Runtime::Env.mobile_layout?
8
9        view 'app/views/layouts/header/_current_user_dropdown.html.haml' do
10          element :sign_out_link
11          element :edit_profile_link
12        end
13
14        view 'app/views/layouts/header/_default.html.haml' do
15          element :navbar, required: true
16          element :canary_badge_link
17          element :user_avatar, required: !QA::Runtime::Env.mobile_layout?
18          element :user_menu, required: !QA::Runtime::Env.mobile_layout?
19          element :stop_impersonation_link
20          element :issues_shortcut_button, required: !QA::Runtime::Env.mobile_layout?
21          element :merge_requests_shortcut_button, required: !QA::Runtime::Env.mobile_layout?
22          element :todos_shortcut_button, required: !QA::Runtime::Env.mobile_layout?
23        end
24
25        view 'app/assets/javascripts/nav/components/top_nav_app.vue' do
26          element :navbar_dropdown
27        end
28
29        view 'app/assets/javascripts/nav/components/top_nav_dropdown_menu.vue' do
30          element :menu_subview_container
31        end
32
33        view 'lib/gitlab/nav/top_nav_menu_item.rb' do
34          element :menu_item_link
35        end
36
37        view 'app/helpers/nav/top_nav_helper.rb' do
38          element :admin_area_link
39          element :projects_dropdown
40          element :groups_dropdown
41          element :snippets_link
42        end
43
44        view 'app/views/layouts/_search.html.haml' do
45          element :search_term_field
46        end
47
48        def go_to_groups
49          within_groups_menu do
50            click_element(:menu_item_link, title: 'Your groups')
51          end
52        end
53
54        def go_to_create_group
55          within_groups_menu do
56            click_element(:menu_item_link, title: 'Create group')
57          end
58        end
59
60        def go_to_projects
61          within_projects_menu do
62            click_element(:menu_item_link, title: 'Your projects')
63          end
64        end
65
66        def go_to_create_project
67          within_projects_menu do
68            click_element(:menu_item_link, title: 'Create new project')
69          end
70        end
71
72        def go_to_menu_dropdown_option(option_name)
73          within_top_menu do
74            click_element(:navbar_dropdown, title: 'Menu')
75            click_element(option_name)
76          end
77        end
78
79        # To go to one of the popular pages using the provided shortcut buttons within top menu
80        # @param [Symbol] the name of the element (e.g: `:issues_shortcut button`)
81        # @example:
82        #   Menu.perform do |menu|
83        #     menu.go_to_page_by_shortcut(:issues_shortcut_button) #=> Go to Issues page using shortcut button
84        #   end
85        def go_to_page_by_shortcut(button)
86          within_top_menu do
87            click_element(button)
88          end
89        end
90
91        def go_to_admin_area
92          click_admin_area
93
94          return unless has_text?('Enter Admin Mode', wait: 1.0)
95
96          Admin::NewSession.perform do |new_session|
97            new_session.set_password(Runtime::User.admin_password)
98            new_session.click_enter_admin_mode
99          end
100        end
101
102        def signed_in?
103          return false if Page::Main::Login.perform(&:on_login_page?)
104
105          has_personal_area?(wait: 0)
106        end
107
108        def not_signed_in?
109          return true if Page::Main::Login.perform(&:on_login_page?)
110
111          has_no_personal_area?
112        end
113
114        def sign_out
115          retry_until do
116            wait_if_retry_later
117
118            break true unless signed_in?
119
120            within_user_menu do
121              click_element :sign_out_link
122            end
123
124            not_signed_in?
125          end
126        end
127
128        def sign_out_if_signed_in
129          sign_out if signed_in?
130        end
131
132        def click_edit_profile_link
133          retry_until(reload: false) do
134            within_user_menu do
135              click_element(:edit_profile_link)
136            end
137
138            has_text?('User Settings')
139          end
140        end
141
142        def click_user_profile_link
143          within_user_menu do
144            click_element(:user_profile_link)
145          end
146        end
147
148        def search_for(term)
149          fill_element :search_term_field, "#{term}\n"
150        end
151
152        def has_personal_area?(wait: Capybara.default_max_wait_time)
153          has_element?(:user_avatar, wait: wait)
154        end
155
156        def has_no_personal_area?(wait: Capybara.default_max_wait_time)
157          has_no_element?(:user_avatar, wait: wait)
158        end
159
160        def has_admin_area_link?(wait: Capybara.default_max_wait_time)
161          within_top_menu do
162            click_element(:navbar_dropdown, title: 'Menu')
163            has_element?(:admin_area_link, wait: wait)
164          end
165        end
166
167        def has_no_admin_area_link?(wait: Capybara.default_max_wait_time)
168          within_top_menu do
169            click_element(:navbar_dropdown, title: 'Menu')
170            has_no_element?(:admin_area_link, wait: wait)
171          end
172        end
173
174        def click_stop_impersonation_link
175          click_element(:stop_impersonation_link)
176        end
177
178        # To verify whether the user has been directed to a canary web node
179        # @return [Boolean] result of checking existence of :canary_badge_link element
180        # @example:
181        #   Menu.perform do |menu|
182        #     expect(menu.canary?).to be(true)
183        #   end
184        def canary?
185          has_element?(:canary_badge_link)
186        end
187
188        private
189
190        def within_top_menu(&block)
191          within_element(:navbar, &block)
192        end
193
194        def within_user_menu(&block)
195          within_top_menu do
196            click_element :user_avatar
197
198            within_element(:user_menu, &block)
199          end
200        end
201
202        def within_groups_menu(&block)
203          go_to_menu_dropdown_option(:groups_dropdown)
204
205          within_element(:menu_subview_container, &block)
206        end
207
208        def within_projects_menu(&block)
209          go_to_menu_dropdown_option(:projects_dropdown)
210
211          within_element(:menu_subview_container, &block)
212        end
213
214        def click_admin_area
215          go_to_menu_dropdown_option(:admin_area_link)
216        end
217      end
218    end
219  end
220end
221
222QA::Page::Main::Menu.prepend_mod_with('Page::Main::Menu', namespace: QA)
223