@casoon/auditmysite
Version:
Professional website analysis suite with robust accessibility testing, Core Web Vitals performance monitoring, SEO analysis, and content optimization insights. Features isolated browser contexts, retry mechanisms, and comprehensive API endpoints for profe
144 lines (120 loc) ⢠4.52 kB
JavaScript
/**
* š Basic AuditMySite SDK Usage Example
*
* This example demonstrates the simplest way to use the AuditMySite SDK
* for accessibility testing in a Node.js application.
*/
const { AuditSDK } = require('../dist/sdk/audit-sdk');
async function basicExample() {
console.log('šÆ Basic AuditMySite SDK Example\n');
// Initialize the SDK
const audit = new AuditSDK({
verbose: true,
defaultOutputDir: './audit-reports'
});
try {
// Example 1: Quick audit with minimal configuration
console.log('š Running quick audit...');
const result = await audit.quickAudit('https://example.com/sitemap.xml', {
maxPages: 5,
formats: ['html', 'json']
});
console.log('ā
Audit completed!');
console.log(` š Tested: ${result.summary.testedPages} pages`);
console.log(` ā
Passed: ${result.summary.passedPages}`);
console.log(` ā Failed: ${result.summary.failedPages}`);
console.log(` š Success Rate: ${Math.round((result.summary.passedPages / result.summary.testedPages) * 100)}%`);
console.log(` ā±ļø Duration: ${Math.round(result.duration / 1000)}s`);
// Show generated reports
if (result.reports.length > 0) {
console.log('\nš Generated Reports:');
result.reports.forEach(report => {
console.log(` ${report.format.toUpperCase()}: ${report.path} (${Math.round(report.size / 1024)}KB)`);
});
}
} catch (error) {
console.error('ā Audit failed:', error.message);
process.exit(1);
}
}
// Example 2: Using fluent API with event listeners
async function fluentAPIExample() {
console.log('\nš Fluent API Example with Event Listeners\n');
const audit = new AuditSDK();
try {
const result = await audit.audit()
.sitemap('https://example.com/sitemap.xml')
.maxPages(3)
.standard('WCAG2AA')
.formats(['html', 'markdown'])
.includePerformance()
.viewport(1920, 1080)
.on('audit:start', (event) => {
console.log('š Audit started!');
})
.on('audit:progress', (event) => {
const { current, total, percentage, currentUrl } = event.data;
console.log(`š Progress: ${current}/${total} (${percentage}%) - ${currentUrl || 'Processing...'}`);
})
.on('audit:page:complete', (event) => {
const { url, result } = event.data;
const status = result.passed ? 'ā
' : 'ā';
console.log(`${status} ${url} - ${result.errors.length} errors`);
})
.on('report:complete', (event) => {
const { format, size } = event.data;
console.log(`š ${format.toUpperCase()} report generated (${Math.round(size / 1024)}KB)`);
})
.run();
console.log('\nš Fluent API audit completed successfully!');
} catch (error) {
console.error('ā Fluent API audit failed:', error.message);
}
}
// Example 3: Error handling and connection testing
async function errorHandlingExample() {
console.log('\nš”ļø Error Handling & Connection Testing Example\n');
const audit = new AuditSDK();
// Test connection first
const sitemapUrl = 'https://nonexistent-site.com/sitemap.xml';
console.log(`š Testing connection to: ${sitemapUrl}`);
const connectionTest = await audit.testConnection(sitemapUrl);
if (!connectionTest.success) {
console.log(`ā Connection test failed: ${connectionTest.error}`);
console.log('š” Tip: Make sure the sitemap URL is accessible and valid');
return;
}
console.log('ā
Connection test passed!');
// Run audit with proper error handling
try {
await audit.quickAudit(sitemapUrl, {
maxPages: 5,
formats: ['json']
});
} catch (error) {
if (error.code === 'INVALID_SITEMAP') {
console.error('š« Invalid sitemap:', error.message);
console.log('š” Please check the sitemap URL and ensure it\'s accessible');
} else if (error.code === 'TIMEOUT') {
console.error('ā° Audit timed out:', error.message);
console.log('š” Try reducing maxPages or increasing timeout');
} else {
console.error('š„ Unexpected error:', error.message);
}
}
}
// Run all examples
async function runAllExamples() {
await basicExample();
await fluentAPIExample();
await errorHandlingExample();
}
// Only run if this file is executed directly
if (require.main === module) {
runAllExamples().catch(console.error);
}
module.exports = {
basicExample,
fluentAPIExample,
errorHandlingExample
};