Skip to content

Commit cf71b34

Browse files
committed
fix(regression-test): use native Windows controls
Signed-off-by: kangfenmao <kangfenmao@qq.com>
1 parent 07d0cd1 commit cf71b34

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,16 @@ export function openExternalText(platform: Platform, paths: RunPaths, candidateP
100100
)
101101
} else {
102102
const script = [
103-
`Start-Process notepad.exe -ArgumentList '${escapePowerShell(filePath)}'`,
104-
`$filename = '${escapePowerShell(basename(filePath))}'`,
103+
`$expected = (Get-Content -Raw -LiteralPath '${escapePowerShell(filePath)}').Trim()`,
104+
'$notepad = Start-Process notepad.exe -PassThru',
105105
'$deadline = [DateTime]::UtcNow.AddSeconds(10)',
106-
'$notepad = $null',
107-
'do {',
108-
' $notepad = Get-Process -Name notepad -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowTitle.Contains($filename) } | Sort-Object StartTime -Descending | Select-Object -First 1',
109-
' if (-not $notepad) { Start-Sleep -Milliseconds 200 }',
110-
'} while (-not $notepad -and [DateTime]::UtcNow -lt $deadline)',
111-
'if (-not $notepad) { throw "Notepad window was not found" }',
106+
'do { $notepad.Refresh(); if ($notepad.MainWindowHandle -eq 0) { Start-Sleep -Milliseconds 200 } } while ($notepad.MainWindowHandle -eq 0 -and [DateTime]::UtcNow -lt $deadline)',
107+
'if ($notepad.MainWindowHandle -eq 0) { throw "Notepad window was not found" }',
112108
'$shell = New-Object -ComObject WScript.Shell',
113109
'if (-not $shell.AppActivate($notepad.Id)) { throw "Notepad window could not be activated" }',
114110
'Start-Sleep -Milliseconds 300',
111+
'$shell.SendKeys($expected)',
112+
'Start-Sleep -Milliseconds 200',
115113
'$shell.SendKeys("^a")',
116114
'Start-Sleep -Milliseconds 500'
117115
].join('\n')
@@ -154,6 +152,7 @@ export function chooseNativeFile(platform: Platform, paths: RunPaths, candidateP
154152
'Add-Type -AssemblyName UIAutomationClient',
155153
'Add-Type -AssemblyName UIAutomationTypes',
156154
'Add-Type -AssemblyName System.Windows.Forms',
155+
`Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public static class NativeDialog { [DllImport("user32.dll")] public static extern IntPtr GetDlgItem(IntPtr dialog, int id); [DllImport("user32.dll")] public static extern IntPtr SendMessage(IntPtr window, uint message, IntPtr wParam, IntPtr lParam); }'`,
157156
'$root = [System.Windows.Automation.AutomationElement]::RootElement',
158157
`$processCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::ProcessIdProperty, ${electronPid})`,
159158
'$deadline = [DateTime]::UtcNow.AddSeconds(10)',
@@ -177,11 +176,9 @@ export function chooseNativeFile(platform: Platform, paths: RunPaths, candidateP
177176
'$windows = $root.FindAll([System.Windows.Automation.TreeScope]::Children, $processCondition)',
178177
'$dialog = $windows | Where-Object { $_.Current.ClassName -eq "#32770" } | Select-Object -Last 1',
179178
'if (-not $dialog) { throw "Native folder dialog closed before selection" }',
180-
'$acceptCondition = [System.Windows.Automation.PropertyCondition]::new([System.Windows.Automation.AutomationElement]::AutomationIdProperty, "1")',
181-
'$acceptButton = $dialog.FindFirst([System.Windows.Automation.TreeScope]::Descendants, $acceptCondition)',
182-
'if (-not $acceptButton) { throw "Native folder accept button was not found" }',
183-
'$acceptButton.SetFocus()',
184-
'[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")',
179+
'$acceptHandle = [NativeDialog]::GetDlgItem([IntPtr]$dialog.Current.NativeWindowHandle, 1)',
180+
'if ($acceptHandle -eq [IntPtr]::Zero) { throw "Native folder accept button was not found" }',
181+
'$null = [NativeDialog]::SendMessage($acceptHandle, 0x00F5, [IntPtr]::Zero, [IntPtr]::Zero)',
185182
'Start-Sleep -Milliseconds 500'
186183
]
187184
: [

tests/e2e/cherry-regression/09-cherryin-and-images.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ async function generateAndSaveImage(
3434
await selectSidebarApp(page, 'Paintings')
3535
const modelButton = page.locator('[data-selector-shell-root="true"] > button').first()
3636
const modelName = model.split('/').at(-1) ?? model
37-
if (!(await modelButton.textContent())?.includes(modelName)) {
37+
const selectedModelRestored = await modelButton
38+
.filter({ hasText: modelName })
39+
.waitFor({ state: 'visible', timeout: 5_000 })
40+
.then(() => true)
41+
.catch(() => false)
42+
if (!selectedModelRestored) {
3843
await modelButton.click()
3944
await selectVisibleModel(page, model)
4045
}

0 commit comments

Comments
 (0)