Skip to content

Commit 4046478

Browse files
committed
fix(regression-test): drag Windows text selection
Signed-off-by: kangfenmao <kangfenmao@qq.com>
1 parent affc9d3 commit 4046478

2 files changed

Lines changed: 50 additions & 5 deletions

File tree

scripts/cherry-regression-test/system-automation.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,49 @@ export function openExternalText(platform: Platform, paths: RunPaths, candidateP
142142
return filePath
143143
}
144144

145+
export function selectExternalText(platform: Platform): void {
146+
if (platform === 'macos') {
147+
runMacHotkey(['Meta', 'a'])
148+
return
149+
}
150+
if (!activeWindowsExternalTextPid) throw new Error('No external text window is active')
151+
152+
const script = [
153+
'Add-Type -AssemblyName UIAutomationClient',
154+
'Add-Type -AssemblyName UIAutomationTypes',
155+
`Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public static class NativeMouse { [DllImport("user32.dll")] public static extern bool SetCursorPos(int x, int y); [DllImport("user32.dll")] public static extern void mouse_event(uint flags, uint x, uint y, uint data, UIntPtr extra); }'`,
156+
`$notepad = Get-Process -Id ${activeWindowsExternalTextPid} -ErrorAction Stop`,
157+
'$shell = New-Object -ComObject WScript.Shell',
158+
'if (-not $shell.AppActivate($notepad.Id)) { throw "Notepad window could not be activated" }',
159+
'Start-Sleep -Milliseconds 200',
160+
'$root = [System.Windows.Automation.AutomationElement]::RootElement',
161+
`$process = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::ProcessIdProperty, ${activeWindowsExternalTextPid})`,
162+
'$window = $root.FindFirst([System.Windows.Automation.TreeScope]::Children, $process)',
163+
'if (-not $window) { throw "Notepad automation window was not found" }',
164+
'$documentType = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::ControlTypeProperty, [System.Windows.Automation.ControlType]::Document)',
165+
'$editType = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::ControlTypeProperty, [System.Windows.Automation.ControlType]::Edit)',
166+
'$textArea = $window.FindFirst([System.Windows.Automation.TreeScope]::Descendants, [System.Windows.Automation.OrCondition]::new($documentType, $editType))',
167+
'if (-not $textArea) { throw "Notepad text area was not found" }',
168+
'$rect = $textArea.Current.BoundingRectangle',
169+
'$startX = [int]($rect.Left + 8)',
170+
'$endX = [int]($rect.Right - 8)',
171+
'$y = [int]($rect.Top + 18)',
172+
'$null = [NativeMouse]::SetCursorPos($startX, $y)',
173+
'[NativeMouse]::mouse_event(0x0002, 0, 0, 0, [UIntPtr]::Zero)',
174+
'for ($step = 1; $step -le 12; $step += 1) {',
175+
' $x = [int]($startX + (($endX - $startX) * $step / 12))',
176+
' $null = [NativeMouse]::SetCursorPos($x, $y)',
177+
' Start-Sleep -Milliseconds 25',
178+
'}',
179+
'[NativeMouse]::mouse_event(0x0004, 0, 0, 0, [UIntPtr]::Zero)',
180+
'Start-Sleep -Milliseconds 500'
181+
].join('; ')
182+
execFileSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], {
183+
stdio: 'ignore',
184+
timeout: 10_000
185+
})
186+
}
187+
145188
export function chooseNativeFile(platform: Platform, paths: RunPaths, candidatePath: string): string {
146189
const filePath = resolveAllowedPath(candidatePath, [paths.fixtures, paths.workspace, paths.evidence])
147190
if (!existsSync(filePath)) throw new Error(`Selected file does not exist: ${filePath}`)

tests/e2e/cherry-regression/05-desktop-assistants.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { join } from 'node:path'
33
import { expect, test } from './fixture'
44
import { dismissOnboarding } from './helpers'
55
import { closeSettings, ensureCustomChatProvider, openSettingsSection } from './models'
6-
import { openExternalText, sendSystemHotkey } from '../../../scripts/cherry-regression-test/system-automation'
6+
import {
7+
openExternalText,
8+
selectExternalText,
9+
sendSystemHotkey
10+
} from '../../../scripts/cherry-regression-test/system-automation'
711

812
async function configureQuickAssistant(
913
page: Parameters<typeof dismissOnboarding>[0],
@@ -127,10 +131,8 @@ test('[C-03] 使用划词助手处理跨应用选中文本 @selection-assistant'
127131
await expect
128132
.poll(
129133
async () => {
130-
sendSystemHotkey(
131-
app.record.platform,
132-
app.record.platform === 'macos' ? ['Meta', 'Shift', 'k'] : ['Control', 'a']
133-
)
134+
if (app.record.platform === 'macos') sendSystemHotkey(app.record.platform, ['Meta', 'Shift', 'k'])
135+
else selectExternalText(app.record.platform)
134136
await selection.waitForTimeout(1_000)
135137
return selection.locator('body').getAttribute('data-selected-text')
136138
},

0 commit comments

Comments
 (0)