undetected-chromedriver-js
Version:
Node.js wrapper for undetected-chromedriver with automatic setup and cross-platform support
114 lines (90 loc) โข 3.33 kB
JavaScript
const UndetectedChrome = require('../index');
async function basicTest() {
console.log('๐งช Running basic test...');
let driver;
try {
// Create undetected Chrome instance
const undetectedChrome = new UndetectedChrome({
headless: true // Run in headless mode for testing
});
driver = await undetectedChrome.build();
console.log('โ Driver created successfully');
// Test navigation
await driver.get('https://httpbin.org/user-agent');
console.log('โ Navigation successful');
// Get page title
const title = await driver.getTitle();
console.log(`โ Page title: ${title}`);
// Get page source to verify user agent
const pageSource = await driver.getPageSource();
console.log('โ Page source retrieved');
// Clean up
await undetectedChrome.quit();
console.log('โ Driver closed successfully');
console.log('๐ Basic test passed!');
} catch (error) {
console.error('โ Test failed:', error.message);
if (driver) {
try {
await driver.quit();
} catch (closeError) {
console.error('Error closing driver:', closeError.message);
}
}
process.exit(1);
}
}
async function antiDetectionTest() {
console.log('๐ต๏ธ Running anti-detection test...');
let undetectedChrome;
try {
// Create instance for testing detection
undetectedChrome = new UndetectedChrome({
headless: false // Show browser for visual verification
});
await undetectedChrome.build();
console.log('โ Driver created for anti-detection test');
// Test against a site that checks for automation
await undetectedChrome.get('https://nowsecure.nl/');
console.log('โ Navigated to nowsecure.nl');
// Wait a moment for page to load
await new Promise(resolve => setTimeout(resolve, 3000));
const driver = undetectedChrome.getDriver();
const pageTitle = await driver.getTitle();
console.log(`โ Page loaded: ${pageTitle}`);
// Check if we passed the detection
const pageSource = await driver.getPageSource();
if (pageSource.includes('blocked') || pageSource.includes('Access Denied')) {
console.log('โ ๏ธ May have been detected - check browser window');
} else {
console.log('โ Successfully bypassed detection');
}
// Keep browser open for 5 seconds for manual verification
console.log('Browser will close in 5 seconds...');
await new Promise(resolve => setTimeout(resolve, 5000));
await undetectedChrome.quit();
console.log('โ Anti-detection test completed');
} catch (error) {
console.error('โ Anti-detection test failed:', error.message);
if (undetectedChrome) {
try {
await undetectedChrome.quit();
} catch (closeError) {
console.error('Error closing driver:', closeError.message);
}
}
process.exit(1);
}
}
async function runTests() {
console.log('๐ Starting undetected-chromedriver-js tests\n');
await basicTest();
console.log('');
await antiDetectionTest();
console.log('\n๐ All tests completed successfully!');
}
// Run tests if this file is executed directly
if (require.main === module) {
runTests().catch(console.error);
}
module.exports = { basicTest, antiDetectionTest, runTests };