-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselenium_test.js
More file actions
127 lines (106 loc) · 4.23 KB
/
Copy pathselenium_test.js
File metadata and controls
127 lines (106 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env node
/**
* Node.js Selenium test script to access localhost:3000 from Docker container
*/
const { Builder, By, until } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');
const fs = require('fs');
async function createChromeOptions() {
const options = new chrome.Options();
options.addArguments('--no-sandbox');
options.addArguments('--disable-dev-shm-usage');
options.addArguments('--disable-gpu');
options.addArguments('--window-size=1920,1080');
options.addArguments('--disable-extensions');
options.addArguments('--disable-plugins');
options.addArguments('--disable-images');
return options;
}
async function testLocalhostAccess() {
console.log('🚀 Starting Selenium test...');
const webdriverUrl = 'http://localhost:4444/wd/hub';
const targetUrl = 'http://host.docker.internal:3000';
let driver;
try {
// Create Chrome options
const chromeOptions = await createChromeOptions();
// Connect to remote WebDriver
console.log(`Connecting to WebDriver at ${webdriverUrl}...`);
driver = await new Builder()
.forBrowser('chrome')
.setChromeOptions(chromeOptions)
.usingServer(webdriverUrl)
.build();
console.log(`Navigating to ${targetUrl}...`);
await driver.get(targetUrl);
// Wait for page to load
await driver.wait(until.titleIs(await driver.getTitle()), 10000);
// Get page title
const title = await driver.getTitle();
console.log('✅ Page loaded successfully!');
console.log(`Page title: ${title}`);
// Take a screenshot
const screenshot = await driver.takeScreenshot();
const screenshotPath = '/tmp/selenium_test_screenshot.png';
fs.writeFileSync(screenshotPath, screenshot, 'base64');
console.log(`Screenshot saved to: ${screenshotPath}`);
// Try to find some common elements
try {
const body = await driver.findElement(By.tagName('body'));
const bodyText = await body.getText();
console.log(`Body content length: ${bodyText.length} characters`);
} catch (error) {
console.log(`Could not find body element: ${error.message}`);
}
console.log('✅ Test completed successfully!');
return true;
} catch (error) {
console.log(`❌ Test failed: ${error.message}`);
return false;
} finally {
if (driver) {
await driver.quit();
console.log('WebDriver session closed');
}
}
}
async function checkWebDriverStatus() {
try {
const response = await fetch('http://localhost:4444/wd/hub/status');
if (response.ok) {
console.log('✅ WebDriver is running and accessible');
return true;
} else {
console.log('❌ WebDriver is not responding properly');
return false;
}
} catch (error) {
console.log(`❌ Cannot connect to WebDriver: ${error.message}`);
console.log('Make sure Docker container is running: docker-compose up -d');
return false;
}
}
async function main() {
console.log('🔍 Selenium Test: Accessing localhost:3000 from Docker container');
console.log('='.repeat(70));
// Check if WebDriver is accessible
const webdriverOk = await checkWebDriverStatus();
if (!webdriverOk) {
process.exit(1);
}
// Run the test
const success = await testLocalhostAccess();
if (success) {
console.log('\n🎉 SUCCESS! Your setup is working correctly!');
console.log('You can now run Selenium tests against your localhost:3000 service.');
} else {
console.log('\n💡 Troubleshooting:');
console.log('1. Ensure your dev server is running on localhost:3000');
console.log('2. Check Docker container: docker-compose ps');
console.log('3. View container logs: docker-compose logs selenium-chrome');
console.log('4. Access noVNC desktop: http://localhost:7900');
process.exit(1);
}
}
// Run the main function
main().catch(console.error);