# Test Generation

This document describes how to manually convert CLI commands to test code.

## Manual Conversion

Each CLI command can be converted to Python code using the WebViewController API:

### Basic Commands

```bash
# CLI
nomo-e2e open http://localhost:9222
nomo-e2e click "#login"
nomo-e2e fill "#username" "admin"
nomo-e2e screenshot --filename result.png
```

```python
# Python
from nomo_e2e import WebViewController

controller = WebViewController.connect("http://localhost:9222", driver="cdp")

controller.click("#login")
controller.fill("#username", "admin")
controller.screenshot("./result.png")

controller.disconnect()
```

## Example Test Files

### Basic Test

```python
import pytest
from nomo_e2e import WebViewController

def test_login_flow():
    """Test login flow with Electron app."""
    controller = WebViewController.connect(
        "http://localhost:9222",
        driver="cdp"
    )

    try:
        # Navigate to login page
        controller.navigate("https://example.com/login")

        # Fill form
        controller.fill("#username", "admin")
        controller.fill("#password", "secret")

        # Submit
        controller.click("#submit")

        # Verify
        controller.wait_for_selector("#dashboard")
        assert "dashboard" in controller.get_url()

    finally:
        controller.disconnect()
```

### With Context Manager

```python
from nomo_e2e import WebViewController

def test_with_context():
    """Test with automatic cleanup."""
    with WebViewController.connect("http://localhost:9222") as controller:
        controller.click("#login")
        controller.type_text("#username", "admin")
        controller.screenshot("./login.png")
```

### Multi-Step Test

```python
from nomo_e2e import WebViewController

def test_multi_page_flow():
    """Test multi-page workflow."""
    with WebViewController.connect("http://localhost:9222") as controller:
        # Step 1: Home
        controller.navigate("https://example.com")
        controller.screenshot("./home.png")

        # Step 2: Products
        controller.click("a.products")
        controller.wait_for_selector("#products-list")
        controller.screenshot("./products.png")

        # Step 3: Product Detail
        controller.click(".product:first-child")
        controller.wait_for_selector("#product-detail")
        controller.screenshot("./detail.png")

        # Step 4: Add to Cart
        controller.click("#add-to-cart")
        controller.wait_for_selector("#cart-count")
```

### Session Persistence Test

```python
from nomo_e2e import WebViewController

def test_session_persistence():
    """Test saving and loading session state."""
    # Login and save state
    controller = WebViewController.connect("http://localhost:9222")
    controller.fill("#username", "admin")
    controller.fill("#password", "secret")
    controller.click("#login")
    controller.save_state("./logged-in.json")
    controller.disconnect()

    # Restore state in new session
    controller = WebViewController.connect("http://localhost:9222")
    controller.load_state("./logged-in.json")
    # Already logged in!
```

## Command Mapping

| CLI Command | Python API |
|-------------|------------|
| open | WebViewController.connect() |
| close | controller.disconnect() |
| goto | controller.navigate(url) |
| click | controller.click(selector) |
| type-text | controller.type_text(text) |
| fill | controller.fill(selector, text) |
| screenshot | controller.screenshot(path) |
| snapshot | controller.get_snapshot() |
| eval | controller.execute_script(script) |
| hover | controller.hover(selector) |
| press | controller.press_key(key) |
| select | controller.select_option(selector, value) |
| check | controller.check(selector) |
| uncheck | controller.uncheck(selector) |
| reload | controller.reload() |
| go-back | controller.go_back() |
| go-forward | controller.go_forward() |
| url | controller.get_url() |
| title | controller.get_title() |

## Pytest Integration

### pytest.ini

```ini
[pytest]
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
```

### conftest.py

```python
import pytest
from nomo_e2e import WebViewController

@pytest.fixture
def controller():
    """Fixture that provides a connected controller."""
    ctrl = WebViewController.connect(
        "http://localhost:9222",
        driver="cdp"
    )
    yield ctrl
    ctrl.disconnect()
```

### Usage

```python
def test_example(controller):
    """Example test using fixture."""
    controller.click("#button")
    controller.fill("#input", "value")
    controller.screenshot("./result.png")
```

## Best Practices

### 1. Use Context Managers

```python
# Good
with WebViewController.connect("http://localhost:9222") as controller:
    controller.click("#button")

# Avoid
controller = WebViewController.connect("http://localhost:9222")
controller.click("#button")
controller.disconnect()  # Easy to forget
```

### 2. Add Waits Explicitly

```python
# Good
controller.click("#load-data")
controller.wait_for_selector("#data-table")
controller.screenshot("./data.png")

# Avoid
controller.click("#load-data")
controller.screenshot("./data.png")  # May capture loading state
```

### 3. Take Screenshots for Debugging

```python
# Take screenshot after key actions
controller.click("#submit")
controller.screenshot("./result.png")

# Check for errors
if controller.get_url().startswith("https://error"):
    controller.screenshot("./error.png")
```

### 4. Use Semantic Selectors

```python
# Good
controller.click("button.submit")
controller.fill("input[name=email]")

# Avoid
controller.click("#btn_123")
controller.fill("#input-456")
```

## See Also

- [Getting Started](references/getting-started.md)
- [CDP Protocol](references/cdp-protocol.md)
- [WebDriver Protocol](references/webdriver-protocol.md)
- [Session Management](references/session-management.md)
