Repository navigation
Expand file tree
/
Copy pathwebsocket-basic.test.js
More file actions
94 lines (71 loc) · 3.46 KB
/
Copy pathwebsocket-basic.test.js
File metadata and controls
94 lines (71 loc) · 3.46 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
/**
* Basic WebSocket Tests for WebSSH2
*
* Simple tests to verify WebSocket functionality
*/
import { test, expect } from '@playwright/test'
import { TEST_CONFIG, TIMEOUTS, waitForPrompt, executeCommand } from './constants.js'
test.describe('WebSocket Basic Tests', () => {
test('should establish WebSocket connection', async ({ page }) => {
await page.goto(`${TEST_CONFIG.baseUrl}/ssh`)
// Fill connection form
await page.fill('[name="host"]', TEST_CONFIG.sshHost)
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.validUsername)
await page.fill('[name="password"]', TEST_CONFIG.validPassword)
// Connect
await page.click('button:has-text("Connect")')
// Verify connection status
await expect(page.locator('text=Connected')).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
// Wait for terminal to be ready
await waitForPrompt(page)
console.log('✓ WebSocket connection established successfully')
})
test('should execute commands over WebSocket', async ({ page }) => {
await page.goto(`${TEST_CONFIG.baseUrl}/ssh`)
// Connect
await page.fill('[name="host"]', TEST_CONFIG.sshHost)
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.validUsername)
await page.fill('[name="password"]', TEST_CONFIG.validPassword)
await page.click('button:has-text("Connect")')
await expect(page.locator('text=Connected')).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
// Wait for prompt
await waitForPrompt(page)
// Execute a simple command
await executeCommand(page, 'echo "WebSocket Test OK"')
// Wait for output
await page.waitForFunction(() => {
const terminalContent = document.querySelector('.xterm-rows')?.textContent || ''
return terminalContent.includes('WebSocket Test OK')
}, { timeout: TIMEOUTS.CONNECTION })
console.log('✓ Command execution over WebSocket successful')
})
test('should handle disconnection gracefully', async ({ page }) => {
await page.goto(`${TEST_CONFIG.baseUrl}/ssh`)
// Connect
await page.fill('[name="host"]', TEST_CONFIG.sshHost)
await page.fill('[name="port"]', TEST_CONFIG.sshPort)
await page.fill('[name="username"]', TEST_CONFIG.validUsername)
await page.fill('[name="password"]', TEST_CONFIG.validPassword)
await page.click('button:has-text("Connect")')
await expect(page.locator('text=Connected')).toBeVisible({ timeout: TIMEOUTS.CONNECTION })
// Wait for prompt
await waitForPrompt(page)
// Type exit command
await executeCommand(page, 'exit')
// Wait a moment for disconnection to process
await page.waitForTimeout(TIMEOUTS.SHORT_WAIT)
// Check if the connection status changed or if we see logout/exit message
const terminalContent = await page.evaluate(() => document.querySelector('.xterm-screen')?.textContent || '')
const statusElement = await page.locator('#status').textContent().catch(() => '')
// Verify disconnection by checking either terminal content or status
const isDisconnected =
terminalContent.includes('logout') ||
terminalContent.includes('exit') ||
statusElement.includes('Disconnected') ||
statusElement === '' // Status might be cleared on disconnect
expect(isDisconnected).toBeTruthy()
console.log('✓ Disconnection handled gracefully')
})
})