# WebDriver Protocol Usage

WebDriver protocol provides standard W3C-compliant automation for WebViews that don't expose CDP ports.

## Supported Platforms

- Tauri2 (macOS/Linux with tauri-plugin-webdriver)
- Safari (macOS)
- Any WebDriver-compatible browser or WebView

## WebDriver Endpoints

```
http://localhost:4445
```

### Endpoint Discovery

```bash
# Check WebDriver status
curl http://localhost:4445/status

# Response:
# {"value":{"ready":true,"message":"Ready."}}
```

## WebDriver Connection

```bash
# Connect to WebDriver endpoint
nomo-e2e open --driver webdriver http://localhost:4445
```

## Available Commands (WebDriver)

### Basic Interactions

These commands work with both CDP and WebDriver:

```bash
# Click element
nomo-e2e click <selector>

# Type text (character by character)
nomo-e2e type-text <text>

# Fill input (clear + type)
nomo-e2e fill <selector> <text>
```

### Navigation

```bash
# Navigate to URL
nomo-e2e goto <url>

# Get current URL
nomo-e2e url

# Get page title
nomo-e2e title

# Reload page
nomo-e2e reload

# Go back/forward
nomo-e2e go-back
nomo-e2e go-forward
```

### Screenshots & DOM

```bash
# Take screenshot
nomo-e2e screenshot [--filename path] [--selector element]

# Capture snapshot
nomo-e2e snapshot [--filename path]
```

### Script Execution

```bash
# Evaluate JavaScript
nomo-e2e eval <script>
```

### Storage (JavaScript-based)

These commands work via JavaScript evaluation:

```bash
# Cookies (via document.cookie)
nomo-e2e cookie-get [name]
nomo-e2e cookie-set <name> <value>
nomo-e2e cookie-delete <name>
nomo-e2e cookie-clear

# localStorage
nomo-e2e localstorage-list
nomo-e2e localstorage-set <key> <value>
nomo-e2e localstorage-delete <key>
nomo-e2e localstorage-clear

# sessionStorage
nomo-e2e sessionstorage-list
nomo-e2e sessionstorage-set <key> <value>
nomo-e2e sessionstorage-delete <key>
nomo-e2e sessionstorage-clear
```

### Session Management

```bash
# Save/load session state
nomo-e2e state-save [filename]
nomo-e2e state-load <filename>

# Configuration
nomo-e2e config-list
nomo-e2e config-get <key>
nomo-e2e config-set <key> <value>
```

## Not Available in WebDriver

The following commands require CDP protocol and will show a friendly error:

| Command | Alternative |
|---------|-------------|
| hover | Use click instead |
| dblclick | Use click twice |
| press | Use type-text or click |
| keydown/keyup | Limited support |
| select | Use JavaScript or click |
| check/uncheck | Use click |
| mousemove | Use hover |
| mousedown/mouseup | Limited support |
| mousewheel | Use JavaScript scroll |
| upload | Use JavaScript |
| drag | Use JavaScript |
| console | Not available |
| console-clear | Not available |
| network | Not available |
| pdf | Not available |

### Example: Error Message

```bash
$ nomo-e2e hover "#dropdown"

{
  "status": "error",
  "error": "Advanced interactions (hover, dblclick, press, select, check, etc.) are not supported by WebDriver protocol. Use CDP protocol (Electron, Edge) for these features."
}
```

## WebDriver-Specific Workflows

### Basic Login Flow

```bash
#!/bin/bash
# WebDriver-compatible workflow

# Connect
nomo-e2e open --driver webdriver http://localhost:4445

# Take snapshot
nomo-e2e snapshot

# Fill form (basic commands only)
nomo-e2e fill "#username" "admin"
nomo-e2e fill "#password" "secret"

# Click submit
nomo-e2e click "#submit"

# Verify
nomo-e2e snapshot
nomo-e2e screenshot --filename result.png

# Disconnect
nomo-e2e close
```

### Session Persistence

```bash
#!/bin/bash
# Save and restore WebDriver session

# Login and save
nomo-e2e open --driver webdriver http://localhost:4445
nomo-e2e click "#login"
nomo-e2e fill "#username" "admin"
nomo-e2e fill "#password" "secret"
nomo-e2e click "#submit"
nomo-e2e state-save ./auth.json
nomo-e2e close

# Restore in new session
nomo-e2e open --driver webdriver http://localhost:4445
nomo-e2e state-load ./auth.json
```

## CDP vs WebDriver Comparison

| Feature | CDP | WebDriver | WebDriver Alternative |
|---------|-----|-----------|----------------------|
| **Protocol** | Chrome DevTools | W3C WebDriver | - |
| **Advanced Interactions** | ✅ | ❌ | Basic commands |
| **Console Access** | ✅ | ❌ | Not available |
| **Network Monitoring** | ✅ | ❌ | Not available |
| **PDF Generation** | ✅ | ❌ | Not available |
| **File Upload** | ✅ | ❌ | JavaScript |
| **Drag and Drop** | ✅ | ❌ | JavaScript |
| **Hover** | ✅ | ❌ | click |
| **Double-click** | ✅ | ❌ | click twice |
| **Key Press** | ✅ | ❌ | type-text |
| **Basic Actions** | ✅ | ✅ | - |

## When to Use WebDriver

Use WebDriver protocol when:

1. **Platform doesn't support CDP** - Tauri2 without CDP extension
2. **Only need basic actions** - click, fill, navigation
3. **Need W3C compliance** - Standard automation
4. **Safari testing** - macOS Safari only

## When to Use CDP Instead

Switch to CDP (Electron, Edge) when you need:

1. Advanced interactions (hover, drag, etc.)
2. Console monitoring
3. Network analysis
4. PDF generation
5. Precise mouse/keyboard control

## Troubleshooting WebDriver

### Connection Refused

```bash
# Check WebDriver status
curl http://localhost:4445/status

# Ensure Tauri2 dev mode is running
cargo tauri dev
```

### Element Not Found

```bash
# Take snapshot to see elements
nomo-e2e snapshot

# Try longer timeout
nomo-e2e click "#element" --timeout 10000
```

### Protocol Error

If you see:
```
Advanced interactions not supported by WebDriver protocol
```

The command requires CDP. Either:
1. Use CDP protocol (Electron, Edge)
2. Use alternative basic commands

## See Also

- [Getting Started](getting-started.md)
- [CDP Protocol](cdp-protocol.md)
- [Session Management](session-management.md)
- [Test Generation](test-generation.md)
