Browse Source

Feature: Mobile - Added Selenium testing helper for the new mobile app.

Martin Gruner 3 years ago
parent
commit
3042cfcff7

+ 1 - 0
app/controllers/application_controller/authenticates.rb

@@ -45,6 +45,7 @@ module ApplicationController::Authenticates
   def authentication_check_only(auth_param = {})
     if Rails.env.test? && ENV['FAKE_SELENIUM_LOGIN_USER_ID'].present? && session[:user_id].blank?
       session[:user_id] = ENV['FAKE_SELENIUM_LOGIN_USER_ID'].to_i
+      session[:user_device_updated_at] = Time.zone.now
     end
 
     # logger.debug 'authentication_check'

+ 5 - 1
app/frontend/common/composables/usePushMessages.ts

@@ -8,6 +8,7 @@ import {
 } from '@common/graphql/types'
 import { SubscriptionHandler } from '@common/server/apollo/handler'
 import { NotificationTypes } from '@common/types/notification'
+import testFlags from '@common/utils/testFlags'
 import { onMounted } from 'vue'
 
 let subscription: SubscriptionHandler<
@@ -30,7 +31,10 @@ export default function usePushMessages() {
     subscription = new SubscriptionHandler(usePushMessagesSubscription())
     subscription.onResult((result) => {
       const message = result.data?.pushMessages
-      if (!message?.title && !message?.text) return
+      if (!message?.title && !message?.text) {
+        testFlags.set('usePushMessagesSubscription.subscribed')
+        return
+      }
       notify(`${message.title}: ${message.text}`)
     })
   })

+ 5 - 0
app/frontend/common/stores/session/user.ts

@@ -10,6 +10,7 @@ import type {
   CurrentUserQuery,
   CurrentUserQueryVariables,
 } from '@common/graphql/types'
+import testFlags from '@common/utils/testFlags'
 
 let currentUserQuery: QueryHandler<CurrentUserQuery, CurrentUserQueryVariables>
 
@@ -44,6 +45,10 @@ const useSessionUserStore = defineStore('sessionUser', {
         await locale.updateLocale(userLocale)
       }
 
+      if (this.value) {
+        testFlags.set('useSessionUserStore.getCurrentUser.loaded')
+      }
+
       return this.value
     },
 

+ 10 - 4
spec/support/capybara/authenticated.rb

@@ -29,10 +29,16 @@ RSpec.configure do |config|
     else
       ENV['FAKE_SELENIUM_LOGIN_USER_ID'] = User.find_by(email: credentials[:username]).id.to_s
 
-      visit '/'
-
-      wait.until_exists do
-        current_login
+      case example.metadata[:app]
+      when :mobile
+        visit '/mobile'
+        wait_for_test_flag('applicationLoaded.loaded', skip_clearing: true)
+        wait_for_test_flag('useSessionUserStore.getCurrentUser.loaded', skip_clearing: true)
+      else
+        visit '/'
+        wait.until_exists do
+          current_login
+        end
       end
 
       await_empty_ajax_queue

+ 4 - 0
spec/support/capybara/browser_test_helper.rb

@@ -82,6 +82,10 @@ module BrowserTestHelper
   #  await_empty_ajax_queue
   #
   def await_empty_ajax_queue
+
+    # Waiting not supported/required by mobile app.
+    return if self.class.metadata[:app] == :mobile # self.class needed to get metadata from within an `it` block.
+
     # page.evaluate_script silently discards any present alerts, which is not desired.
     begin
       return if page.driver.browser.switch_to.alert

+ 11 - 1
spec/support/capybara/driven_by.rb

@@ -26,7 +26,17 @@ RSpec.configure do |config|
     browser_name = ENV.fetch('BROWSER', 'firefox')
     driven_by(:"zammad_#{browser_name}")
 
-    case example.metadata.fetch(:screen_size, :desktop)
+    screen_size = example.metadata[:screen_size] || case example.metadata[:app]
+                                                    when :mobile
+                                                      :mobile
+                                                    else
+                                                      :desktop
+                                                    end
+
+    case screen_size
+    when :mobile
+      browser_width  = 390
+      browser_height = 844
     when :tablet
       browser_width  = 1020
       browser_height = 760

+ 1 - 1
spec/support/capybara/test_flags.rb

@@ -2,7 +2,7 @@
 
 module TestFlags
   def wait_for_test_flag(flag, skip_clearing: false)
-    wait.until { page.evaluate_script("window.testFlags.get('#{flag.gsub("'", "\\'")}', #{skip_clearing})") }
+    wait.until { page.evaluate_script("window.testFlags && window.testFlags.get('#{flag.gsub("'", "\\'")}', #{skip_clearing})") }
   end
 end
 

+ 48 - 39
spec/system/apps/mobile/app_maintenance_spec.rb

@@ -2,43 +2,44 @@
 
 require 'rails_helper'
 
-RSpec.describe 'Mobile > App Update Check', type: :system, authenticated_as: false do
-  before do
-    page.driver.browser.manage.window.resize_to(390, 844)
-    visit '/mobile/login?ApplicationRebuildCheckInterval=500'
-    wait_for_test_flag('useAppMaintenanceSubscription.subscribed')
-  end
-
-  it 'shows app rebuild dialog' do
-    # Append a newline to the manifest file to trigger a reload notification.
-    File.open(Rails.root.join('public/vite/manifest.json'), 'a') do |file|
-      file.write("\n")
+RSpec.describe 'Mobile > App Update Check', type: :system, app: :mobile do
+  context 'when checking application rebuild notification', authenticated_as: false do
+    before do
+      visit '/mobile/login?ApplicationRebuildCheckInterval=500'
+      wait_for_test_flag('useAppMaintenanceSubscription.subscribed')
     end
 
-    expect(page).to have_text('A newer version of the app is available. Please reload at your earliest.')
-  end
+    it 'shows app rebuild dialog' do
+      # Append a newline to the manifest file to trigger a reload notification.
+      File.open(Rails.root.join('public/vite/manifest.json'), 'a') do |file|
+        file.write("\n")
+      end
 
-  it 'reacts to app_version message' do
-    AppVersion.set(true, AppVersion::MSG_APP_VERSION)
-    expect(page).to have_text('A newer version of the app is available. Please reload at your earliest.')
-  end
+      expect(page).to have_text('A newer version of the app is available. Please reload at your earliest.')
+    end
 
-  it 'reacts to reload_auto message' do
-    AppVersion.set(true, AppVersion::MSG_RESTART_AUTO)
-    expect(page).to have_text('A newer version of the app is available. Please reload at your earliest.')
-  end
+    it 'reacts to app_version message' do
+      AppVersion.set(true, AppVersion::MSG_APP_VERSION)
+      expect(page).to have_text('A newer version of the app is available. Please reload at your earliest.')
+    end
 
-  it 'reacts to reload_manual message' do
-    AppVersion.set(true, AppVersion::MSG_RESTART_MANUAL)
-    expect(page).to have_text('A newer version of the app is available. Please reload at your earliest.')
-  end
+    it 'reacts to reload_auto message' do
+      AppVersion.set(true, AppVersion::MSG_RESTART_AUTO)
+      expect(page).to have_text('A newer version of the app is available. Please reload at your earliest.')
+    end
+
+    it 'reacts to reload_manual message' do
+      AppVersion.set(true, AppVersion::MSG_RESTART_MANUAL)
+      expect(page).to have_text('A newer version of the app is available. Please reload at your earliest.')
+    end
 
-  it 'reacts to config_updated message' do
-    AppVersion.set(true, AppVersion::MSG_CONFIG_CHANGED)
-    expect(page).to have_text('The configuration of Zammad has changed. Please reload at your earliest.')
+    it 'reacts to config_updated message' do
+      AppVersion.set(true, AppVersion::MSG_CONFIG_CHANGED)
+      expect(page).to have_text('The configuration of Zammad has changed. Please reload at your earliest.')
+    end
   end
 
-  context 'with authenticated user', authenticated_as: :user do
+  context 'when maintenance mode is activated', authenticated_as: :user do
     before do
       wait_for_test_flag('applicationLoaded.loaded')
       wait_for_test_flag('useConfigUpdatesSubscription.subscribed')
@@ -68,15 +69,23 @@ RSpec.describe 'Mobile > App Update Check', type: :system, authenticated_as: fal
     end
   end
 
-  it 'reacts to maintenance broadcast message' do
-    Gql::ZammadSchema.subscriptions.trigger(
-      Gql::Subscriptions::PushMessages.field_name,
-      {},
-      {
-        title: 'Attention',
-        text:  'Maintenance test message.',
-      }
-    )
-    expect(page).to have_text('Attention: Maintenance test message.')
+  context 'when maintenance message is sent', authenticated_as: false do
+    before do
+      visit '/mobile'
+      wait_for_test_flag('applicationLoaded.loaded')
+      wait_for_test_flag('usePushMessagesSubscription.subscribed')
+    end
+
+    it 'reacts to maintenance broadcast message' do
+      Gql::ZammadSchema.subscriptions.trigger(
+        Gql::Subscriptions::PushMessages.field_name,
+        {},
+        {
+          title: 'Attention',
+          text:  'Maintenance test message.',
+        }
+      )
+      expect(page).to have_text('Attention: Maintenance test message.')
+    end
   end
 end