> ## Documentation Index
> Fetch the complete documentation index at: https://docs.browsertest.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Basic Usage Examples

> Fundamental examples of using the BrowserTest SDK

## Installation

```bash theme={null}
npm install browsertest-sdk
# or
yarn add browsertest-sdk
```

## Initialization

```javascript theme={null}
import { BrowserTest } from 'browsertest-sdk';

// Initialize with API key
const bt = new BrowserTest({
  apiKey: process.env.BROWSERTEST_API_KEY
});

// Test connection
const isConnected = await bt.testConnection();
console.log('Connected:', isConnected);
```

## Screenshots

### Simple Screenshot

```javascript theme={null}
const screenshot = await bt.screenshot.take({
  url: 'https://example.com'
});

console.log('Screenshot taken:', screenshot.data.screenshot.length, 'bytes');
```

### Full Page Screenshot

```javascript theme={null}
const fullPage = await bt.screenshot.take({
  url: 'https://example.com',
  fullPage: true
});
```

### Custom Viewport

```javascript theme={null}
const mobile = await bt.screenshot.take({
  url: 'https://example.com',
  width: 375,
  height: 667
});

const tablet = await bt.screenshot.take({
  url: 'https://example.com',
  width: 768,
  height: 1024
});

const desktop = await bt.screenshot.take({
  url: 'https://example.com',
  width: 1920,
  height: 1080
});
```

### Element Screenshot

```javascript theme={null}
const element = await bt.screenshot.take({
  url: 'https://example.com',
  selector: '.hero-section'
});

// Or use the convenience method
const hero = await bt.screenshot.element('https://example.com', '.hero-section');
```

## Agentic Testing

### Simple Test

```javascript theme={null}
const result = await bt.testing.execute({
  instructions: 'Navigate to the homepage and verify the main heading is visible',
  url: 'https://example.com'
});

console.log('Test passed:', result.success);
```

### Login Test

```javascript theme={null}
const loginResult = await bt.testing.login(
  'https://example.com/login',
  {
    email: 'user@example.com',
    password: 'password123'
  }
);
```

### Form Filling

```javascript theme={null}
const formResult = await bt.testing.fillForm(
  'https://example.com/contact',
  {
    name: 'John Doe',
    email: 'john@example.com',
    message: 'Hello from BrowserTest!'
  }
);
```

## Batch Operations

### Batch Screenshots

```javascript theme={null}
const batch = await bt.screenshot.takeBatch({
  urls: [
    'https://site1.com',
    'https://site2.com',
    'https://site3.com'
  ],
  fullPage: true
});

console.log(`${batch.meta.successful}/${batch.meta.total} screenshots captured`);
```

### Batch Tests

```javascript theme={null}
const tests = await bt.batch.tests([
  {
    instructions: 'Check if homepage loads correctly',
    url: 'https://example.com'
  },
  {
    instructions: 'Verify login form validation',
    url: 'https://example.com/login'
  }
]);

console.log(`${tests.successful}/${tests.total} tests passed`);
```

## Usage Tracking

```javascript theme={null}
const usage = await bt.getUsage();
console.log('Plan:', usage.plan);
console.log('Screenshots used:', usage.usage.screenshot.used);
console.log('Agentic tests used:', usage.usage.agentic.used);
```

## Error Handling

```javascript theme={null}
try {
  const result = await bt.screenshot.take({ url: 'https://example.com' });
} catch (error) {
  if (error.name === 'BrowserTestAuthError') {
    console.log('Invalid API key');
  } else if (error.name === 'BrowserTestQuotaError') {
    console.log('Quota exceeded');
  } else {
    console.log('Error:', error.message);
  }
}
```

## Complete Example

```javascript theme={null}
import { BrowserTest } from 'browsertest-sdk';

async function example() {
  // Initialize
  const bt = new BrowserTest({
    apiKey: process.env.BROWSERTEST_API_KEY
  });

  try {
    // Test connection
    const connected = await bt.testConnection();
    if (!connected) throw new Error('Connection failed');

    // Take a screenshot
    const screenshot = await bt.screenshot.take({
      url: 'https://example.com',
      fullPage: true
    });

    // Run a test
    const testResult = await bt.testing.execute({
      instructions: 'Verify the page title and main navigation',
      url: 'https://example.com'
    });

    // Check usage
    const usage = await bt.getUsage();

    console.log('✅ All operations completed successfully!');
    console.log('Screenshot size:', screenshot.meta.size, 'bytes');
    console.log('Test passed:', testResult.success);
    console.log('Usage:', usage.usage.screenshot.used, 'screenshots,', usage.usage.agentic.used, 'tests');

  } catch (error) {
    console.error('❌ Error:', error.message);
  }
}

example();
```
