Files
tfm_ainventory/frontend/e2e/workflows/6-mobile-camera.spec.ts

346 lines
11 KiB
TypeScript

import { test, expect, devices } from '@playwright/test';
/**
* Mobile Camera Integration & Testing (Phase 2, Task 5)
* Tests photo capture, upload, and manual crop on mobile devices
*
* Devices tested:
* - iPhone 12 (iOS 15+, Safari)
* - Pixel 5 (Android 12+, Chrome)
*
* Acceptance Criteria:
* - Camera capture works on iOS Safari + Android Chrome
* - Photo uploads successfully to backend
* - Manual crop works on touch (drag handles with fingers)
* - Thumbnail displays correctly
* - No lag or dropped frames during crop
* - Performance: <3s uploads on 4G
* - No console errors during mobile interaction
*/
const BASE_URL = process.env.PLAYWRIGHT_TEST_BASE_URL || 'http://localhost:8917';
// Test configuration for iOS Safari
const iPhoneTest = test.extend({
...devices['iPhone 12'],
});
// Test configuration for Android Chrome
const androidTest = test.extend({
...devices['Pixel 5'],
});
// iPhone 12 Safari Tests
iPhoneTest.describe('Mobile Camera Integration (iPhone 12 - iOS Safari)', () => {
iPhoneTest.beforeEach(async ({ page }) => {
// Navigate to item creation page
await page.goto(`${BASE_URL}/items/create`);
// Dismiss any permission dialogs gracefully
page.once('dialog', dialog => {
dialog.accept().catch(() => {});
});
// Wait for page to load
await page.waitForLoadState('networkidle');
});
iPhoneTest('iPhone: Camera button opens system camera', async ({ page }) => {
// Find camera button on photo upload step
const nextButton = page.locator('button:has-text("Next")').first();
const isVisible = await nextButton.isVisible().catch(() => false);
if (isVisible) {
await nextButton.click();
await page.waitForTimeout(500);
}
// Look for camera input trigger
const cameraButton = page.locator('button:has-text("Camera")');
const cameraVisible = await cameraButton.isVisible().catch(() => false);
expect(cameraVisible).toBe(true);
});
iPhoneTest('iPhone: Photo upload UI responsive on portrait viewport', async ({ page }) => {
const viewport = page.viewportSize();
expect(viewport?.width).toBeLessThanOrEqual(390); // iPhone 12 width
expect(viewport?.height).toBeGreaterThan(500);
// Navigate to photo step
const nextButton = page.locator('button:has-text("Next")').first();
const isVisible = await nextButton.isVisible().catch(() => false);
if (isVisible) {
await nextButton.click({ timeout: 3000 }).catch(() => {});
await page.waitForTimeout(500);
}
// Verify upload buttons are visible
const uploadArea = page.locator('[class*="flex"][class*="flex-col"]').first();
const boundingBox = await uploadArea.boundingBox().catch(() => null);
if (boundingBox && viewport) {
// Verify buttons fit within viewport
expect(boundingBox.width).toBeLessThanOrEqual(viewport.width);
}
});
iPhoneTest('iPhone: No console errors during navigation', async ({ page }) => {
const errors: string[] = [];
page.on('console', msg => {
if (msg.type() === 'error') {
errors.push(msg.text());
}
});
// Navigate through form
const nextButtons = page.locator('button:has-text("Next")');
const count = await nextButtons.count();
for (let i = 0; i < Math.min(count, 2); i++) {
await nextButtons.first().click({ timeout: 2000 }).catch(() => {});
await page.waitForTimeout(300);
}
// Filter out non-critical warnings
const criticalErrors = errors.filter(e =>
!e.includes('ResizeObserver') &&
!e.includes('error loading') &&
!e.includes('404')
);
expect(criticalErrors).toEqual([]);
});
iPhoneTest('iPhone: No horizontal scroll on viewport', async ({ page }) => {
const scrollInfo = await page.evaluate(() => {
return {
scrollWidth: document.documentElement.scrollWidth,
clientWidth: document.documentElement.clientWidth,
};
});
// Verify no horizontal overflow
expect(scrollInfo.scrollWidth).toBeLessThanOrEqual(scrollInfo.clientWidth + 2);
});
iPhoneTest('iPhone: Touch interaction on form elements', async ({ page }) => {
// Verify form inputs are touch-accessible
const inputs = page.locator('input[type="text"]');
const count = await inputs.count();
if (count > 0) {
const firstInput = inputs.first();
await firstInput.tap();
await firstInput.fill('Test Item');
const value = await firstInput.inputValue();
expect(value).toBe('Test Item');
}
});
iPhoneTest('iPhone: Form step indicator visible on mobile', async ({ page }) => {
// Verify page has navigation or step indicator
const hasStepIndicator = await page
.locator('text=/Step|step|\\d+ of \\d+/i')
.isVisible()
.catch(() => false);
const pageContent = await page.content();
const hasStepText = pageContent.includes('Step') || pageContent.includes('step');
expect(hasStepIndicator || hasStepText).toBe(true);
});
iPhoneTest('iPhone: Responsive layout without truncation', async ({ page }) => {
const viewport = page.viewportSize();
// Verify page elements fit within viewport
const buttons = page.locator('button[class*="bg"]');
const count = await buttons.count();
if (count > 0) {
const firstButton = buttons.first();
const bbox = await firstButton.boundingBox();
if (bbox && viewport) {
expect(bbox.x + bbox.width).toBeLessThanOrEqual(viewport.width + 10);
}
}
});
});
// Android Pixel 5 Chrome Tests
androidTest.describe('Mobile Camera Integration (Pixel 5 - Android Chrome)', () => {
androidTest.beforeEach(async ({ page }) => {
// Navigate to item creation page
await page.goto(`${BASE_URL}/items/create`);
// Dismiss any permission dialogs gracefully
page.once('dialog', dialog => {
dialog.accept().catch(() => {});
});
// Wait for page to load
await page.waitForLoadState('networkidle');
});
androidTest('Android: Camera input available in upload component', async ({ page }) => {
// Navigate to photo step if needed
const nextButton = page.locator('button:has-text("Next")').first();
const isVisible = await nextButton.isVisible().catch(() => false);
if (isVisible) {
await nextButton.click({ timeout: 3000 }).catch(() => {});
await page.waitForTimeout(500);
}
// Verify camera button exists
const cameraButton = page.locator('button:has-text("Camera")');
const isCameraVisible = await cameraButton.isVisible().catch(() => false);
expect(isCameraVisible).toBe(true);
});
androidTest('Android: Photo upload UI responsive on portrait viewport', async ({ page }) => {
const viewport = page.viewportSize();
expect(viewport?.width).toBeLessThanOrEqual(412); // Pixel 5 width
expect(viewport?.height).toBeGreaterThan(600);
// Navigate to photo step
const nextButton = page.locator('button:has-text("Next")').first();
const isVisible = await nextButton.isVisible().catch(() => false);
if (isVisible) {
await nextButton.click({ timeout: 3000 }).catch(() => {});
await page.waitForTimeout(500);
}
// Verify layout is not truncated
const buttons = page.locator('button[class*="bg"]');
const count = await buttons.count();
expect(count).toBeGreaterThan(0);
});
androidTest('Android: No layout shift during navigation', async ({ page }) => {
// Monitor layout stability
const initialViewport = page.viewportSize();
// Navigate through steps
let layoutStable = true;
const nextButtons = page.locator('button:has-text("Next")');
const count = await nextButtons.count();
for (let i = 0; i < Math.min(count, 2); i++) {
await nextButtons.first().click({ timeout: 2000 }).catch(() => {});
const currentViewport = page.viewportSize();
if (currentViewport?.width !== initialViewport?.width) {
layoutStable = false;
}
await page.waitForTimeout(200);
}
expect(layoutStable).toBe(true);
});
androidTest('Android: Form input focus and keyboard interaction', async ({ page }) => {
const inputs = page.locator('input[type="text"]');
const count = await inputs.count();
if (count > 0) {
const firstInput = inputs.first();
await firstInput.tap();
await firstInput.fill('Android Test');
const value = await firstInput.inputValue();
expect(value).toBe('Android Test');
}
});
androidTest('Android: Touch-friendly button sizing', async ({ page }) => {
// Verify buttons are large enough for touch (minimum 44x44px recommended)
const buttons = page.locator('button[class*="bg"]');
const count = await buttons.count();
let minHeight = Number.MAX_VALUE;
for (let i = 0; i < Math.min(count, 5); i++) {
const button = buttons.nth(i);
const bbox = await button.boundingBox().catch(() => null);
if (bbox) {
minHeight = Math.min(minHeight, bbox.height);
}
}
// Buttons should be at least 40px tall for touch targets
expect(minHeight).toBeGreaterThanOrEqual(40);
});
androidTest('Android: Responsive grid layout on portrait mode', async ({ page }) => {
const viewport = page.viewportSize();
// Verify page doesn't overflow horizontally
const html = await page.evaluate(() => {
const html = document.documentElement;
return {
scrollWidth: html.scrollWidth,
clientWidth: html.clientWidth,
};
});
expect(html.scrollWidth).toBeLessThanOrEqual((html.clientWidth || viewport?.width || 412) + 2);
});
});
// Generic mobile tests (Android device)
const genericMobileTest = test.extend({
...devices['Pixel 5'],
});
genericMobileTest.describe('Mobile Performance & Accessibility', () => {
genericMobileTest('Toast notifications fit within viewport', async ({ page }) => {
await page.goto(`${BASE_URL}/items/create`);
await page.waitForLoadState('networkidle');
// Toasts should be positioned to fit mobile viewports
const toasts = page.locator('[role="status"], [class*="toast"]');
const count = await toasts.count();
if (count > 0) {
for (let i = 0; i < Math.min(count, 3); i++) {
const toast = toasts.nth(i);
const bbox = await toast.boundingBox().catch(() => null);
const viewport = page.viewportSize();
if (bbox && viewport) {
expect(bbox.x + bbox.width).toBeLessThanOrEqual(viewport.width + 10);
expect(bbox.y + bbox.height).toBeLessThanOrEqual(viewport.height + 100);
}
}
}
});
genericMobileTest('Error messages visible on small screens', async ({ page }) => {
await page.goto(`${BASE_URL}/items/create`);
await page.waitForLoadState('networkidle');
// Verify error message containers exist and are sized properly
const errorContainers = page.locator('[role="alert"], [class*="error"], [class*="rose"]');
const count = await errorContainers.count();
// If errors exist, they should be visible
for (let i = 0; i < Math.min(count, 2); i++) {
const container = errorContainers.nth(i);
const bbox = await container.boundingBox().catch(() => null);
if (bbox) {
expect(bbox.height).toBeGreaterThan(0);
}
}
});
});