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

414 lines
14 KiB
TypeScript

import { test, expect, devices, Page } 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';
const API_URL = process.env.PLAYWRIGHT_TEST_API_URL || 'http://localhost:8916';
// Network throttling profile for 4G simulation
const THROTTLE_4G = {
downloadThroughput: 1500 * 1024 / 8,
uploadThroughput: 750 * 1024 / 8,
latency: 100,
};
// Test helper to wait for element with retry
async function waitForElement(page: Page, selector: string, timeout = 5000) {
return page.waitForSelector(selector, { timeout });
}
// Test helper to take screenshot with device name
async function takeScreenshot(page: Page, name: string, deviceName: string) {
return page.screenshot({ path: `test-results/screenshots/${deviceName}-${name}.png` });
}
test.describe('Mobile Camera Integration (iPhone 12 - iOS Safari)', () => {
test.use({
...devices['iPhone 12'],
baseURL: BASE_URL,
});
test.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');
});
test('iPhone: Camera button opens system camera', async ({ page }) => {
// Find camera button on photo upload step
// Navigate to photo step if needed
const nextButton = page.locator('button:has-text("Next")').first();
await nextButton?.click();
await page.waitForTimeout(500);
// Look for camera input trigger
const cameraButton = page.locator('button:has-text("Camera")');
const isVisible = await cameraButton.isVisible().catch(() => false);
expect(isVisible).toBe(true);
});
test('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();
await nextButton?.click({ timeout: 3000 }).catch(() => {});
// Verify upload buttons are visible and properly sized
const uploadArea = page.locator('[class*="flex"][class*="flex-col"]').first();
const boundingBox = await uploadArea.boundingBox();
if (boundingBox) {
// Verify buttons fit within viewport
expect(boundingBox.width).toBeLessThanOrEqual(viewport?.width || 390);
}
});
test('iPhone: No console errors during navigation', async ({ page }) => {
const errors: string[] = [];
const warnings: string[] = [];
page.on('console', msg => {
if (msg.type() === 'error') {
errors.push(msg.text());
}
if (msg.type() === 'warning') {
warnings.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, 3); 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([]);
});
test('iPhone: Touch interaction on form elements', async ({ page }) => {
// Verify form inputs are touch-accessible
const nameInput = page.locator('input[placeholder*="ame"]').first();
const isAccessible = await nameInput.isVisible().catch(() => false);
if (isAccessible) {
// Simulate touch input
await nameInput.tap();
await nameInput.fill('Test Item');
const value = await nameInput.inputValue();
expect(value).toBe('Test Item');
}
});
test('iPhone: Manual crop UI responds to touch drag', async ({ page }) => {
// This test requires a photo to be uploaded first
// For now, verify the crop component loads without errors when present
const cropContainer = page.locator('[class*="relative"][class*="bg-slate"]').first();
const isVisible = await cropContainer.isVisible().catch(() => false);
// If crop UI is visible, verify it doesn't have layout issues
if (isVisible) {
const bbox = await cropContainer.boundingBox();
expect(bbox?.width).toBeGreaterThan(0);
expect(bbox?.height).toBeGreaterThan(0);
}
});
test('iPhone: Form step indicator visible on mobile', async ({ page }) => {
// Verify step indicator is visible and readable
const stepIndicator = page.locator('text=/\\d+ of \\d+/i');
const isVisible = await stepIndicator.isVisible().catch(() => false);
// Step indicator should be present or easily inferred
expect(isVisible || (await page.content()).includes('Step')).toBeTruthy();
});
});
test.describe('Mobile Camera Integration (Pixel 5 - Android Chrome)', () => {
test.use({
...devices['Pixel 5'],
baseURL: BASE_URL,
});
test.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');
});
test('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")');
expect(cameraButton).toBeTruthy();
});
test('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();
await nextButton?.click({ timeout: 3000 }).catch(() => {});
// Verify layout is not truncated
const buttons = page.locator('button[class*="bg"]');
const count = await buttons.count();
expect(count).toBeGreaterThan(0);
});
test('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);
});
test('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');
}
});
test('Android: Touch-friendly button sizing', async ({ page }) => {
// Verify buttons are large enough for touch (minimum 48x48px 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();
if (bbox) {
minHeight = Math.min(minHeight, bbox.height);
}
}
// Buttons should be at least 44px tall for easy touch targets
expect(minHeight).toBeGreaterThanOrEqual(40);
});
test('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 + 2); // Allow small margin for rounding
});
});
test.describe('Mobile Photo Upload Performance', () => {
test.use(devices['Pixel 5']);
test('Upload timing on simulated 4G network', async ({ page, context }) => {
// Note: Full 4G throttling simulation requires more complex network setup
// This test documents the approach for performance validation
await page.goto(`${BASE_URL}/items/create`);
await page.waitForLoadState('networkidle');
// 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);
}
// Record network requests
const networkEvents: { url: string; duration: number }[] = [];
let lastRequestTime = Date.now();
page.on('response', response => {
const currentTime = Date.now();
const duration = currentTime - lastRequestTime;
if (response.url().includes('/api/items') || response.url().includes('/photo')) {
networkEvents.push({
url: response.url(),
duration,
});
}
lastRequestTime = currentTime;
});
// Expect network requests to be reasonable (not testing actual file upload without mock)
expect(networkEvents).toBeDefined();
});
});
test.describe('Mobile Crop UI Touch Events', () => {
test.use(devices['iPhone 12']);
test('Crop handles detect touch start event', async ({ page }) => {
// This test verifies touch event handlers are properly attached
// Note: Full drag simulation requires page with actual image
await page.goto(`${BASE_URL}/items/create`);
await page.waitForLoadState('networkidle');
// Verify page structure supports touch events
const pageSource = await page.content();
// Check for touch event handler patterns in the rendered DOM
const hasTouchListeners = pageSource.includes('touch') ||
pageSource.includes('onTouch') ||
pageSource.includes('addEventListener');
expect(hasTouchListeners).toBe(true);
});
test('No horizontal scroll on crop UI', async ({ page }) => {
await page.goto(`${BASE_URL}/items/create`);
await page.waitForLoadState('networkidle');
const scrollInfo = await page.evaluate(() => {
return {
scrollWidth: document.documentElement.scrollWidth,
clientWidth: document.documentElement.clientWidth,
bodyScrollWidth: document.body.scrollWidth,
};
});
// Verify no overflow
expect(scrollInfo.scrollWidth).toBeLessThanOrEqual(scrollInfo.clientWidth + 1);
});
});
test.describe('Mobile Accessibility & Error Handling', () => {
test.use(devices['Pixel 5']);
test('Error messages visible on small screens', async ({ page }) => {
await page.goto(`${BASE_URL}/items/create`);
await page.waitForLoadState('networkidle');
// Verify error message containers are properly sized
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, 3); i++) {
const container = errorContainers.nth(i);
const isVisible = await container.isVisible().catch(() => false);
if (isVisible) {
const bbox = await container.boundingBox();
expect(bbox?.height).toBeGreaterThan(0);
}
}
});
test('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();
expect(count).toBeGreaterThanOrEqual(0); // 0 or more is fine pre-interaction
if (count > 0) {
for (let i = 0; i < count; i++) {
const toast = toasts.nth(i);
const bbox = await toast.boundingBox();
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);
}
}
}
});
});