# Basic Automation Tutorial

This tutorial will guide you through creating your first automation flow with Flow Master. We'll build a simple email automation that demonstrates core concepts and best practices.

## Table of Contents
- [Tutorial Overview](#tutorial-overview)
- [Prerequisites](#prerequisites)
- [Step 1: Planning Your Automation](#step-1-planning-your-automation)
- [Step 2: Capturing Components](#step-2-capturing-components)
- [Step 3: Building the Flow](#step-3-building-the-flow)
- [Step 4: Testing and Debugging](#step-4-testing-and-debugging)
- [Step 5: Optimization](#step-5-optimization)
- [Next Steps](#next-steps)

## Tutorial Overview

### What We'll Build
In this tutorial, we'll create an automation that:
1. Opens your email client
2. Composes a new email
3. Fills in recipient information
4. Adds a subject line
5. Types a message body
6. Sends the email

### Learning Objectives
By the end of this tutorial, you'll understand:
- How to capture and manage components
- Basic flow creation and editing
- Action types and their uses
- Error handling and validation
- Testing and debugging flows

### Estimated Time
- 30-45 minutes for first-time users
- 15-20 minutes for experienced users

## Prerequisites

### System Setup
- Flow Master installed and activated
- Email client installed (we'll use Apple Mail, but concepts apply to any email client)
- All required permissions granted to Flow Master
- Basic familiarity with Flow Master interface

### Before Starting
1. **Launch Flow Master** and ensure it's running properly
2. **Open your email client** and verify it's working
3. **Position windows** so both Flow Master and email client are visible
4. **Disable email notifications** temporarily to avoid interruptions

## Step 1: Planning Your Automation

### Define the Workflow
Before building, let's outline exactly what our automation will do:

```
Email Automation Workflow:
1. Focus email application window
2. Click "New Email" or "Compose" button
3. Click in "To" field and type recipient email
4. Click in "Subject" field and type subject
5. Click in message body and type content
6. Click "Send" button
7. Verify email was sent successfully
```

### Identify Required Components
We'll need to capture these UI elements:
- **Compose Button**: To start new email
- **To Field**: Recipient input field
- **Subject Field**: Subject line input
- **Message Body**: Main content area
- **Send Button**: To send the email

### Plan for Error Handling
Consider what could go wrong:
- Email client not open or not responsive
- Network connectivity issues
- Invalid email addresses
- Required fields not found

## Step 2: Capturing Components

### Capture 1: Compose Button

1. **Prepare Email Client**
   - Open your email client
   - Make sure you're on the main inbox view
   - Locate the "New Email" or "Compose" button

2. **Start Component Capture**
   - In Flow Master, go to **Components** tab
   - Click **"Capture Component"**
   - Your screen will be captured for selection

3. **Select the Button**
   - Carefully drag to select just the compose button
   - Avoid including surrounding elements
   - Aim for a clean, distinctive capture

4. **Configure Component**
   ```
   Component Settings:
   ├── Name: "Compose Button"
   ├── Description: "Button to create new email"
   ├── Type: Button
   ├── Application: "Mail" (or your email client)
   └── Matching Threshold: 85% (default)
   ```

5. **Save Component**
   - Click "Save" to add to your library
   - Verify it appears in the Components list

### Capture 2: Email Fields

Repeat the capture process for each field:

#### To Field
```
Component: "Email To Field"
├── Capture: The input field where you type recipient email
├── Tips: Include the field border but not the label
├── Description: "Recipient email input field"
└── Type: Input Field
```

#### Subject Field
```
Component: "Email Subject Field"
├── Capture: The subject line input area
├── Tips: Make sure field is empty when capturing
├── Description: "Email subject input field"
└── Type: Input Field
```

#### Message Body
```
Component: "Email Message Body"
├── Capture: The main text area for email content
├── Tips: Capture a distinctive part of the text area
├── Description: "Email message content area"
└── Type: Input Field
```

#### Send Button
```
Component: "Email Send Button"
├── Capture: The send button (usually blue)
├── Tips: Ensure button is in normal state (not disabled)
├── Description: "Button to send email"
└── Type: Button
```

### Verification
After capturing all components:
1. **Review Component Library**: Ensure all 5 components are saved
2. **Test Recognition**: Use "Find Component" to verify each one works
3. **Adjust if Needed**: Recapture any problematic components

## Step 3: Building the Flow

### Create New Flow

1. **Start Flow Creation**
   - Navigate to **Flows** tab
   - Click **"Create New Flow"**
   - Enter flow details:
     ```
     Flow Configuration:
     ├── Name: "Send Basic Email"
     ├── Description: "Automated email composition and sending"
     ├── Category: "Email Automation"
     └── Trigger: Manual (for now)
     ```

2. **Open Flow Editor**
   - Click "Create" to open the visual flow editor
   - You'll see an empty canvas ready for building

### Build the Flow Steps

#### Step 1: Add Compose Action
1. **Drag Component**: From library, drag "Compose Button" to canvas
2. **Configure Action**:
   ```
   Click Action Settings:
   ├── Action Type: Click
   ├── Click Type: Left Click
   ├── Position: Center of component
   ├── Wait After: 2 seconds
   └── On Failure: Stop flow with error
   ```

#### Step 2: Add Recipient Input
1. **Drag "Email To Field"** to canvas below compose action
2. **Configure Click and Type**:
   ```
   Combined Action:
   ├── First: Click on To Field (to focus)
   ├── Then: Type text action
   ├── Text: "recipient@example.com" (use real email for testing)
   ├── Clear First: Yes (clear any existing content)
   └── Wait After: 1 second
   ```

#### Step 3: Add Subject Input
1. **Drag "Email Subject Field"** to canvas
2. **Configure Subject Entry**:
   ```
   Subject Action:
   ├── Click: Focus the subject field
   ├── Type: "Test Email from Flow Master"
   ├── Wait After: 1 second
   └── Validation: Verify text was entered
   ```

#### Step 4: Add Message Body
1. **Drag "Email Message Body"** to canvas
2. **Configure Message Entry**:
   ```
   Message Action:
   ├── Click: Focus the message area
   ├── Type: "This email was sent automatically by Flow Master.
            This is a test of the automation system."
   ├── Wait After: 1 second
   └── Format: Plain text
   ```

#### Step 5: Add Send Action
1. **Drag "Email Send Button"** to canvas
2. **Configure Send Action**:
   ```
   Send Action:
   ├── Action Type: Click
   ├── Confirmation: Add confirmation dialog
   ├── Wait After: 3 seconds
   └── Success Validation: Look for "sent" confirmation
   ```

### Add Flow Logic

#### Error Handling
1. **Add Try-Catch Blocks**:
   ```
   Error Handling Structure:
   ├── Try: All email actions
   ├── Catch: Component not found errors
   ├── Finally: Close any open dialogs
   └── Cleanup: Reset email client state
   ```

#### Validation Steps
1. **Add Validation Points**:
   ```
   Validation Checkpoints:
   ├── After Compose: Verify new email window opened
   ├── After Each Input: Confirm text was entered
   ├── Before Send: Verify all fields filled
   └── After Send: Confirm email sent successfully
   ```

## Step 4: Testing and Debugging

### Initial Test Run

1. **Prepare for Testing**
   - Save your flow
   - Close any open email windows
   - Position Flow Master to watch the automation

2. **Run Test**
   - Click "Test Flow" in Flow Master
   - Watch each step execute
   - Note any issues or failures

3. **Common Issues and Fixes**:
   ```
   Troubleshooting:
   ├── Component Not Found:
   │   ├── Check if email client is focused
   │   ├── Verify component is visible on screen
   │   └── Recapture if UI has changed
   ├── Text Not Entered:
   │   ├── Ensure field was clicked first
   │   ├── Add delay before typing
   │   └── Check if field accepts text input
   ├── Wrong Window Focus:
   │   ├── Add window focus action
   │   ├── Use window title matching
   │   └── Verify application is running
   └── Timing Issues:
       ├── Increase wait times between actions
       ├── Add wait-for-component conditions
       └── Use adaptive timing
   ```

### Debug Mode Testing

1. **Enable Debug Mode**
   - Turn on "Debug Mode" in flow settings
   - This will show each step as it executes

2. **Step-by-Step Execution**
   - Use "Step Through" mode for detailed testing
   - Verify each action works correctly
   - Fix issues one step at a time

3. **Review Debug Logs**
   ```
   Debug Information:
   ├── Component Match Scores: How well components matched
   ├── Execution Timing: Time taken for each step
   ├── Error Details: Specific failure information
   ├── Screenshots: Images captured during execution
   └── System State: Application and window status
   ```

### Refinement Iterations

Repeat testing and refinement until the flow runs reliably:

1. **First Run**: Expect 50-70% success rate
2. **After Adjustments**: Aim for 90%+ success rate
3. **Final Tuning**: Achieve consistent, reliable execution

## Step 5: Optimization

### Performance Optimization

#### Timing Optimization
```
Optimal Timing Settings:
├── Between Actions: 500ms (adjust based on system speed)
├── After Clicks: 200ms (allow UI to respond)
├── After Typing: 100ms per character
├── Before Validation: 1000ms (wait for changes)
└── Error Recovery: 2000ms (time for manual intervention)
```

#### Component Optimization
```
Component Improvements:
├── Refine Captures: Remove unnecessary background
├── Adjust Thresholds: Fine-tune matching sensitivity
├── Add Alternatives: Capture variations for robustness
├── Optimize Search: Limit search regions
└── Cache Results: Improve repeated executions
```

### Reliability Improvements

#### Add Robustness
1. **Multiple Attempts**: Retry failed actions 2-3 times
2. **Alternative Paths**: Provide fallback methods
3. **State Validation**: Check system state before proceeding
4. **Recovery Actions**: Handle common failure scenarios

#### Error Prevention
```
Prevention Strategies:
├── Pre-execution Checks: Verify prerequisites
├── Component Validation: Ensure components exist
├── Application State: Check if email client is ready
├── Network Status: Verify internet connectivity
└── Resource Availability: Check system resources
```

### User Experience

#### Add User Feedback
```
User Interaction:
├── Progress Indicators: Show execution progress
├── Status Messages: Inform user of current action
├── Completion Notifications: Alert when finished
├── Error Reporting: Clear error messages
└── Manual Intervention: Allow user corrections
```

## Next Steps

### Immediate Next Steps
1. **Save and Document**: Save your flow and add documentation
2. **Create Variations**: Build flows for different email scenarios
3. **Schedule Execution**: Set up automated scheduling
4. **Share Flow**: Export for use on other devices

### Advanced Techniques to Explore
1. **Variable Input**: Make recipient and subject configurable
2. **Conditional Logic**: Add if/then/else branches
3. **File Attachments**: Add support for email attachments
4. **Template System**: Create reusable email templates
5. **Integration**: Connect with other automation workflows

### Learning Resources
- [Advanced Flow Patterns](advanced-flows.md)
- [Component Management Best Practices](../user-guide/components.md)
- [Troubleshooting Guide](../advanced/troubleshooting.md)
- [Performance Optimization](../advanced/performance.md)

### Practice Exercises
1. **Modify the Flow**: Change the email content and recipient
2. **Add Validation**: Include checks for successful email sending
3. **Handle Errors**: Improve error handling and recovery
4. **Create Variations**: Build flows for different email clients
5. **Advanced Features**: Add attachments, formatting, or scheduling

Congratulations! You've successfully created your first automation flow. This foundation will serve you well as you build more complex automations with Flow Master.