common_actions.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. # Logs out the currently logged in user.
  59. #
  60. # @example
  61. # logout
  62. #
  63. def logout
  64. visit('logout')
  65. end
  66. # Overwrites the Capybara::Session#visit method to allow SPA navigation.
  67. # All routes not starting with `/` will be handled as SPA routes.
  68. #
  69. # @see Capybara::Session#visit
  70. #
  71. # @example
  72. # visit('logout')
  73. # => visited SPA route '/#logout'
  74. #
  75. # @example
  76. # visit('/test/ui')
  77. # => visited regular route '/test/ui'
  78. #
  79. def visit(route)
  80. if !route.start_with?('/')
  81. route = "/##{route}"
  82. end
  83. super(route)
  84. end
  85. # This method is equivalent to Capybara::RSpecMatchers#have_current_path
  86. # but checks the SPA route instead of the actual path.
  87. #
  88. # @see Capybara::RSpecMatchers#have_current_path
  89. #
  90. # @example
  91. # expect(page).to have_current_route('login')
  92. # => checks for SPA route '/#login'
  93. #
  94. def have_current_route(route, **options)
  95. if route.is_a?(String)
  96. route = Regexp.new(Regexp.quote("/##{route}"))
  97. end
  98. # wait 1 sec by default because Firefox is slow
  99. options.reverse_merge!(wait: 1, url: true)
  100. have_current_path(route, **options)
  101. end
  102. # This is a convenient wrapper method around #have_current_route
  103. # which requires no previous `expect(page).to ` call.
  104. #
  105. # @example
  106. # expect_current_route('login')
  107. # => checks for SPA route '/#login'
  108. #
  109. def expect_current_route(route, **options)
  110. expect(page).to have_current_route(route, **options)
  111. end
  112. end
  113. RSpec.configure do |config|
  114. config.include CommonActions, type: :system
  115. end