# ShopFlow Playwright Test Suite

This repository contains comprehensive Playwright tests for the ShopFlow application (https://shopflow.php8).

## Test Organization

Tests are organized into **6 phases** covering all aspects of the application:

### **Phase 1: Authentication** (`tests/01-authentication/`)
- ✅ **Login** - Basic login functionality
- ✅ **Logout** - Session termination
- ✅ **Invalid Login** - Error handling
- ✅ **2FA Validation** - Two-factor authentication

### **Phase 2: Core Commerce** (`tests/02-core-commerce/`)
- ✅ **Product Catalog** - Browse and search products
- ✅ **Add to Cart** - Product selection and cart operations
- ✅ **Cart Management** - Cart operations and updates
- ✅ **Checkout** - Order placement (immediate and scheduled)
- ✅ **Order Management** - View and manage orders
- ✅ **Order History** - Historical orders and tracking
- ✅ **Saved Carts** - Cart management and operations
- ✅ **JAB Orders** - Just-A-Box order processing

### **Phase 3: User Management** (`tests/03-user-management/`)
- ✅ **User List** - View and manage users
- ✅ **User Profile View** - Profile information display
- ✅ **User Profile Edit** - Profile updates and management
- ✅ **Role Permissions** - User role and permission management

### **Phase 4: Partner Management** (`tests/04-partner-management/`)
- ✅ **Buyer Group Management** - Buyer group operations
- ✅ **Seller Management** - Seller company management
- ✅ **Seller Groups Management** - Seller group operations
- ✅ **Store Management** - Store configuration
- ✅ **Industry Management** - Industry categorization

### **Phase 5: Price Management** (`tests/05-price-management/`)
- ✅ **Price File Management** - Upload and manage price files
- ✅ **Price File Upload** - File upload and validation
- ✅ **Price File Applications** - Apply pricing rules
- ✅ **Pricing Rules Management** - Configure pricing rules
- ✅ **Product Catalog Management** - Product pricing configuration

### **Phase 6: Seller Portal** (`tests/06-seller-portal/`) **[NEW]**
- ✅ **Seller Dashboard** - Dashboard statistics and metrics (8 tests)
- ✅ **Seller Orders Management** - Process and manage orders (10 tests)
- ✅ **Seller Products Management** - Product listing and management (8 tests)
- ✅ **Seller Analytics** - Analytics and reporting (6 tests)
- ✅ **Seller Settings** - Settings and profile management (6 tests)

**Total Test Coverage:** 100+ test cases across 6 phases

---

## Test Coverage

The test suite includes the following test files:

### 1. `shopflow-comprehensive.spec.ts`
- **Authentication Tests**: Basic login functionality and form validation
- **2FA Validation Tests**: Two-factor authentication flow testing
- **Essential Functionality**: Core application features after authentication

### 2. `shopflow-login-flow.spec.ts`
- **Authentication Tests**: Simplified login process testing
- **2FA Integration**: Two-factor authentication handling
- **Form Validation**: Basic form validation and error handling

### 3. `shopflow-ecommerce.spec.ts`
- **Product Catalog**: Browse products, search, and filter functionality
- **Shopping Cart**: Add to cart, view cart, and cart management
- **Order Management**: View orders and order history
- **Dashboard**: User dashboard and navigation
- **Core E-Commerce**: Essential shopping functionality

### 4. `shopflow-admin.spec.ts`
- **Admin Management**: Company, user, and store management
- **Price Management**: Price files, pricing rules, and product catalog
- **Reports & Analytics**: Generate reports and view analytics
- **Administrative Functions**: Core admin functionality

### 5. `shopflow-security.spec.ts`
- **Basic Security**: XSS prevention, SQL injection protection, HTTPS usage
- **Essential Security**: Core security measures for production readiness

### 6. `shopflow-performance.spec.ts`
- **Page Load Performance**: Basic load time validation
- **Concurrent Users**: Simple concurrent user handling

### 7. `shopflow-2fa-automated.spec.ts`
- **Automated 2FA Code Fetching**: Retrieves 2FA codes from Gmail automatically
- **Complete Login Flow**: End-to-end authentication with automated 2FA
- **2FA Utility Testing**: Tests the email fetching utility independently
- **2FA Validation**: Tests 2FA code validation and error handling
- **2FA Page Elements**: Verifies 2FA page functionality and elements

## Test Configuration

The tests use environment variables for configuration. Copy `config.env.example` to `.env` and customize the values:

```bash
cp config.env.example .env
```

### Required Configuration
- **SHOPFLOW_URL**: The application URL (default: https://shopflow.php8)
- **TEST_EMAIL**: Test user email (default: siliconkatalontest@gmail.com)
- **TEST_PASSWORD**: Test user password (default: o9s8b73y4vtol978mynsl!2ASD;)

### Optional Configuration
- **GMAIL_APP_PASSWORD**: Gmail app password for 2FA automation
- **TWO_FA_MAX_WAIT_TIME**: Maximum wait time for 2FA emails (default: 30000ms)
- **TWO_FA_RETRY_INTERVAL**: Retry interval for 2FA fetching (default: 5000ms)

## 2FA Automation Setup

The test suite includes automated 2FA code fetching from Gmail, similar to the Katalon implementation. To enable this feature:

### 1. Gmail App Password Setup
1. Go to your Google Account settings
2. Navigate to Security > 2-Step Verification
3. Generate an App Password for "Mail"
4. Copy the generated password

### 2. Environment Configuration
Copy the example configuration file:
```bash
cp config.env.example .env
```

Then edit `.env` and set your Gmail app password:
```bash
GMAIL_APP_PASSWORD=your_gmail_app_password_here
```

Or set the environment variable directly:
```bash
export GMAIL_APP_PASSWORD=your_gmail_app_password_here
```

### 3. 2FA Email Requirements
The system looks for emails with:
- **Subject**: "2FA Validation"
- **Content**: Contains "Your 2FA code is: XXXXXX" or similar patterns
- **Timing**: Emails sent after the login attempt

### 4. Fallback Behavior
If 2FA automation is not configured or fails:
- Tests will wait for manual 2FA code input
- Tests will continue with manual verification
- No test failures due to missing 2FA automation

## Running Tests

### Prerequisites
```bash
npm install
npx playwright install
```

### Run All Tests
```bash
npm run test:phases
```

### Run Specific Test Suites

#### Run by Phase
```bash
# Phase 1: Authentication
npm test -- tests/01-authentication/ --project=chromium

# Phase 2: Core Commerce (Buyer Side)
npx playwright test tests/02-core-commerce --project=chromium

# Phase 3: User Management
npm test -- tests/03-user-management/ --project=chromium

# Phase 4: Partner Management
npm test -- tests/04-partner-management/ --project=chromium

# Phase 5: Price Management
npm test -- tests/05-price-management/ --project=chromium

# Phase 6: Seller Portal (NEW)
npm test -- tests/06-seller-portal/ --project=chromium
```

#### Run Legacy Test Suites
```bash
# Run all ShopFlow tests
npm run test:shopflow

# Run login flow tests
npm run test:login

# Run security tests
npm run test:security

# Run performance tests
npm run test:performance

# Run comprehensive tests
npm run test:comprehensive

# Run 2FA automated tests
npm run test:2fa

# Run application functionality tests
npm run test:app

# Run e-commerce functionality tests
npm run test:ecommerce

# Run admin functionality tests
npm run test:admin
```

#### Run Specific Phase 6 Test Suites
```bash
# Dashboard tests
npm test -- tests/06-seller-portal/seller-dashboard.spec.ts --project=chromium

# Orders management tests
npm test -- tests/06-seller-portal/seller-orders-management.spec.ts --project=chromium

# Products management tests
npm test -- tests/06-seller-portal/seller-products-management.spec.ts --project=chromium

# Analytics tests
npm test -- tests/06-seller-portal/seller-analytics.spec.ts --project=chromium

# Settings tests
npm test -- tests/06-seller-portal/seller-settings.spec.ts --project=chromium
```

### Run Tests with Different Options
```bash
# Run tests in headed mode (visible browser)
npm run test:headed

# Run tests in debug mode
npm run test:debug

# Run tests with UI mode
npm run test:ui
```

### View Test Reports
```bash
# Show HTML report
npm run report
```

## Test Configuration

The tests are configured to run against:
- **Base URL**: https://shopflow.php8
- **Browsers**: Chrome, Firefox, Safari
- **Viewports**: Desktop, Mobile, Tablet
- **Timeouts**: 10s action timeout, 30s navigation timeout

## Test Features

### Security Testing
- SQL injection prevention
- XSS attack prevention
- Input validation
- Authentication security
- Data protection
- Error handling security

### Performance Testing
- Page load times
- Resource optimization
- Memory usage
- Network performance
- Concurrent user handling

### Accessibility Testing
- Keyboard navigation
- Screen reader compatibility
- Form accessibility
- ARIA attributes

### Cross-Browser Testing
- Chrome compatibility
- Firefox compatibility
- Safari compatibility
- Mobile responsiveness

## Test Results

Test results are generated in multiple formats:
- **HTML Report**: Interactive test report with screenshots and videos
- **JSON Report**: Machine-readable test results
- **Screenshots**: Captured on test failures
- **Videos**: Recorded on test failures
- **Traces**: Detailed execution traces for debugging

## Continuous Integration

The test suite is designed to run in CI/CD environments with:
- Parallel test execution
- Retry logic for flaky tests
- Comprehensive reporting
- Cross-browser testing
- Performance monitoring

## Maintenance

### Adding New Tests
1. Create new test files following the naming convention: `shopflow-*.spec.ts`
2. Use the existing test structure and patterns
3. Include proper test descriptions and grouping
4. Add appropriate assertions and error handling

### Updating Credentials
If test credentials change, update them in:
- All test files that use `TEST_EMAIL` and `TEST_PASSWORD` constants
- This README file
- Any CI/CD configuration files

### Test Data Management
- Use consistent test data across all test files
- Avoid hardcoding sensitive information
- Use environment variables for configuration when possible

## Troubleshooting

### Common Issues
1. **Certificate Errors**: The application uses HTTPS with self-signed certificates
2. **Network Timeouts**: Increase timeout values for slow networks
3. **Element Not Found**: Check for dynamic content loading
4. **Test Flakiness**: Use proper wait strategies and retry logic

### Debug Mode
Run tests in debug mode to step through execution:
```bash
npm run test:debug
```

### Trace Viewer
Use Playwright's trace viewer to debug failed tests:
```bash
npx playwright show-trace trace.zip
```

## Contributing

When adding new tests:
1. Follow the existing test structure
2. Include comprehensive test coverage
3. Add proper error handling
4. Document any special requirements
5. Test across multiple browsers
6. Include performance considerations