common_actions.rb 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. module CommonActions
  2. delegate :app_host, to: Capybara
  3. # Performs a login with the given credentials and closes the clues (if present).
  4. # The 'remember me' can optionally be checked.
  5. #
  6. # @example
  7. # login(
  8. # username: 'master@example.com',
  9. # password: 'test',
  10. # )
  11. #
  12. # @example
  13. # login(
  14. # username: 'master@example.com',
  15. # password: 'test',
  16. # remember_me: true,
  17. # )
  18. #
  19. # return [nil]
  20. def login(username:, password:, remember_me: false)
  21. visit '/'
  22. within('#login') do
  23. fill_in 'username', with: username
  24. fill_in 'password', with: password
  25. # check via label because checkbox is hidden
  26. click('.checkbox-replacement') if remember_me
  27. # submit
  28. click_button
  29. end
  30. wait(4).until_exists do
  31. current_login
  32. end
  33. end
  34. # Checks if the current session is logged in.
  35. #
  36. # @example
  37. # logged_in?
  38. # => true
  39. #
  40. # @return [true, false]
  41. def logged_in?
  42. current_login.present?
  43. rescue Capybara::ElementNotFound
  44. false
  45. end
  46. # Returns the login of the currently logged in user.
  47. #
  48. # @example
  49. # current_login
  50. # => 'master@example.com'
  51. #
  52. # @return [String] the login of the currently logged in user.
  53. def current_login
  54. find('.user-menu .user a')[:title]
  55. end
  56. # Returns the User record for the currently logged in user.
  57. #
  58. # @example
  59. # current_user.login
  60. # => 'master@example.com'
  61. #
  62. # @example
  63. # current_user do |user|
  64. # user.group_names_access_map = group_names_access_map
  65. # user.save!
  66. # end
  67. #
  68. # @return [User] the current user record.
  69. def current_user
  70. ::User.find_by(login: current_login).tap do |user|
  71. yield user if block_given?
  72. end
  73. end
  74. # Logs out the currently logged in user.
  75. #
  76. # @example
  77. # logout
  78. #
  79. def logout
  80. visit('logout')
  81. end
  82. # Overwrites the Capybara::Session#visit method to allow SPA navigation.
  83. # All routes not starting with `/` will be handled as SPA routes.
  84. #
  85. # @see Capybara::Session#visit
  86. #
  87. # @example
  88. # visit('logout')
  89. # => visited SPA route '/#logout'
  90. #
  91. # @example
  92. # visit('/test/ui')
  93. # => visited regular route '/test/ui'
  94. #
  95. def visit(route)
  96. if !route.start_with?('/')
  97. route = "/##{route}"
  98. end
  99. super(route)
  100. end
  101. # This method is equivalent to Capybara::RSpecMatchers#have_current_path
  102. # but checks the SPA route instead of the actual path.
  103. #
  104. # @see Capybara::RSpecMatchers#have_current_path
  105. #
  106. # @example
  107. # expect(page).to have_current_route('login')
  108. # => checks for SPA route '/#login'
  109. #
  110. def have_current_route(route, **options)
  111. if route.is_a?(String)
  112. route = Regexp.new(Regexp.quote("/##{route}"))
  113. end
  114. # wait 1 sec by default because Firefox is slow
  115. options.reverse_merge!(wait: 1, url: true)
  116. have_current_path(route, **options)
  117. end
  118. # This is a convenient wrapper method around #have_current_route
  119. # which requires no previous `expect(page).to ` call.
  120. #
  121. # @example
  122. # expect_current_route('login')
  123. # => checks for SPA route '/#login'
  124. #
  125. def expect_current_route(route, **options)
  126. expect(page).to have_current_route(route, **options)
  127. end
  128. # Create and migrate an object manager attribute and verify that it exists. Returns the newly attribute.
  129. #
  130. # Create a select attribute:
  131. # @example
  132. # attribute = setup_attribute :object_manager_attribute_select
  133. #
  134. # Create a required text attribute:
  135. # @example
  136. # attribute = setup_attribute :object_manager_attribute_text,
  137. # screens: attributes_for(:required_screen)
  138. #
  139. # Create a date attribute with custom parameters:
  140. # @example
  141. # attribute = setup_attribute :object_manager_attribute_date,
  142. # data_option: {
  143. # 'future' => true,
  144. # 'past' => false,
  145. # 'diff' => 24,
  146. # 'null' => true,
  147. # }
  148. #
  149. # return [attribute]
  150. def create_attribute(attribute_name, attribute_parameters = {})
  151. attribute = create(attribute_name, attribute_parameters)
  152. ObjectManager::Attribute.migration_execute
  153. page.driver.browser.navigate.refresh
  154. attribute
  155. end
  156. # opens the macro list in the ticket view via click
  157. #
  158. # @example
  159. # open_macro_list
  160. #
  161. def open_macro_list
  162. click '.js-openDropdownMacro'
  163. end
  164. def open_article_meta
  165. wrapper = all(%(div.ticket-article-item)).last
  166. wrapper.find('.article-content .textBubble').click
  167. wait(3).until do
  168. wrapper.find('.article-content-meta .article-meta.top').in_fixed_position
  169. end
  170. end
  171. def use_template(template)
  172. wait(4).until do
  173. field = find('#form-template select[name="id"]')
  174. option = field.find(:option, template.name)
  175. option.select_option
  176. click '.sidebar-content .js-apply'
  177. # this is a workaround for a race condition where
  178. # the template selection get's re-rendered after
  179. # a selection was made. The selection is lost and
  180. # the apply click has no effect.
  181. template.options.any? do |attribute, value|
  182. selector = %([name="#{attribute}"])
  183. next if !page.has_css?(selector, wait: 0)
  184. find(selector, wait: 0, visible: false).value == value
  185. end
  186. end
  187. end
  188. # Checks if modal is ready
  189. #
  190. # @param timeout [Integer] seconds to wait
  191. def modal_ready(timeout: 4)
  192. wait(timeout).until_exists { find('.modal.in', wait: 0) }
  193. end
  194. # Checks if modal has disappeared
  195. #
  196. # @param timeout [Integer] seconds to wait
  197. def modal_disappear(timeout: 4)
  198. wait(timeout).until_disappears { find('.modal', wait: 0) }
  199. end
  200. # Executes action inside of modal. Makes sure modal has opened and closes
  201. #
  202. # @param timeout [Integer] seconds to wait
  203. def in_modal(timeout: 4)
  204. modal_ready(timeout: timeout)
  205. within '.modal' do
  206. yield
  207. end
  208. modal_disappear(timeout: timeout)
  209. end
  210. end
  211. RSpec.configure do |config|
  212. config.include CommonActions, type: :system
  213. end