common_actions.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. return if User.find_by(login: current_login).preferences[:intro]
  34. find(:clues_close, wait: 3).in_fixed_postion.click
  35. end
  36. # Checks if the current session is logged in.
  37. #
  38. # @example
  39. # logged_in?
  40. # => true
  41. #
  42. # @return [true, false]
  43. def logged_in?
  44. current_login.present?
  45. rescue Capybara::ElementNotFound
  46. false
  47. end
  48. # Returns the login of the currently logged in user.
  49. #
  50. # @example
  51. # current_login
  52. # => 'master@example.com'
  53. #
  54. # @return [String] the login of the currently logged in user.
  55. def current_login
  56. find('.user-menu .user a')[:title]
  57. end
  58. # Returns the User record for the currently logged in user.
  59. #
  60. # @example
  61. # current_user.login
  62. # => 'master@example.com'
  63. #
  64. # @example
  65. # current_user do |user|
  66. # user.group_names_access_map = group_names_access_map
  67. # user.save!
  68. # end
  69. #
  70. # @return [User] the current user record.
  71. def current_user
  72. ::User.find_by(login: current_login).tap do |user|
  73. yield user if block_given?
  74. end
  75. end
  76. # Logs out the currently logged in user.
  77. #
  78. # @example
  79. # logout
  80. #
  81. def logout
  82. visit('logout')
  83. end
  84. # Overwrites the Capybara::Session#visit method to allow SPA navigation.
  85. # All routes not starting with `/` will be handled as SPA routes.
  86. #
  87. # @see Capybara::Session#visit
  88. #
  89. # @example
  90. # visit('logout')
  91. # => visited SPA route '/#logout'
  92. #
  93. # @example
  94. # visit('/test/ui')
  95. # => visited regular route '/test/ui'
  96. #
  97. def visit(route)
  98. if !route.start_with?('/')
  99. route = "/##{route}"
  100. end
  101. super(route)
  102. end
  103. # This method is equivalent to Capybara::RSpecMatchers#have_current_path
  104. # but checks the SPA route instead of the actual path.
  105. #
  106. # @see Capybara::RSpecMatchers#have_current_path
  107. #
  108. # @example
  109. # expect(page).to have_current_route('login')
  110. # => checks for SPA route '/#login'
  111. #
  112. def have_current_route(route, **options)
  113. if route.is_a?(String)
  114. route = Regexp.new(Regexp.quote("/##{route}"))
  115. end
  116. # wait 1 sec by default because Firefox is slow
  117. options.reverse_merge!(wait: 1, url: true)
  118. have_current_path(route, **options)
  119. end
  120. # This is a convenient wrapper method around #have_current_route
  121. # which requires no previous `expect(page).to ` call.
  122. #
  123. # @example
  124. # expect_current_route('login')
  125. # => checks for SPA route '/#login'
  126. #
  127. def expect_current_route(route, **options)
  128. expect(page).to have_current_route(route, **options)
  129. end
  130. # Create and migrate an object manager attribute and verify that it exists. Returns the newly attribute.
  131. #
  132. # Create a select attribute:
  133. # @example
  134. # attribute = setup_attribute :object_manager_attribute_select
  135. #
  136. # Create a required text attribute:
  137. # @example
  138. # attribute = setup_attribute :object_manager_attribute_text,
  139. # screens: attributes_for(:required_screen)
  140. #
  141. # Create a date attribute with custom parameters:
  142. # @example
  143. # attribute = setup_attribute :object_manager_attribute_date,
  144. # data_option: {
  145. # 'future' => true,
  146. # 'past' => false,
  147. # 'diff' => 24,
  148. # 'null' => true,
  149. # }
  150. #
  151. # return [attribute]
  152. def create_attribute(attribute_name, attribute_parameters = {})
  153. attribute = create(attribute_name, attribute_parameters)
  154. ObjectManager::Attribute.migration_execute
  155. page.driver.browser.navigate.refresh
  156. attribute
  157. end
  158. # opens the macro list in the ticket view via click
  159. #
  160. # @example
  161. # open_macro_list
  162. #
  163. def open_macro_list
  164. click '.js-openDropdownMacro'
  165. end
  166. end
  167. RSpec.configure do |config|
  168. config.include CommonActions, type: :system
  169. end