powershellui-automationflaui

How to set value in field using FlaUI?


How do I use FlaUI to set the value in a specific field after clicking the "Save As" button in Paint?

I'm trying to use FlaUI to automate filling a field in Paint after clicking "Save As". The field in question is named "Name:". Here is the code I have so far:

Add-Type -Path C:\Users\sergi\assemblies\bin\Release\net48\publish\FlaUI.UIA3.dll

$windowTitle = 'Untitled - Paint'
$control = 'Name:'
$automation = [FlaUI.UIA3.UIA3Automation]::new()
$process = get-process | Where-Object {$_.MainWindowTitle -match $windowTitle}
$app = [FlaUI.Core.Application]::Attach( $process )

foreach( $wnd in $app.GetAllTopLevelWindows( $automation ) ) {
    $myinput = $wnd.FindAllDescendants() | Where-Object { $_.Name -eq $control }
    $myinput[0].SetValue('Value Test')
}

After running the code it gives this error:

Method invocation failed because [FlaUI.Core.AutomationElements.AutomationElement] does not contain a method named 'SetValue'.
On line: 11 character: 5
+ $saveButton[0].SetValue('Value Test')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

However, I'm having a hard time finding a way to access this particular field and set its value using FlaUI. Any suggestions on how I can do this?


Solution

  • After pressing the "save" button, you have to search for the "save" dialog window. There might be a delay before the dialog and its child elements have been created, so for reliability a loop should be used.

    I recommend to install Accessibility Insights for Windows to get an idea what you need to search for. It shows you the hierarchy of automation objects and their properties.

    Here is working code to automate the "save" dialog.

    $automation = [FlaUI.UIA3.UIA3Automation]::new()
    
    foreach( $process in Get-Process mspaint ) {
        $app = [FlaUI.Core.Application]::Attach( $process )
    
        foreach( $wnd in $app.GetAllTopLevelWindows( $automation ) ) {
    
            $saveButton = $wnd.FindAllDescendants() | 
                          Where-Object { $_.ControlType -eq 'Button' -and $_.Name -eq 'Save' }         
    
            Write-Host "Found 'save' button"
    
            $saveButton.Patterns.Invoke.Pattern.Invoke()
    
            Write-Host 'Waiting for "save" dialog...'
    
            $saveDialog = $fileNameField = $null
    
            # Loop until the "save" dialog has been shown and accepts input
            while( $true ) {
                $saveDialog = $wnd.FindAllChildren() | Where-Object Name -eq 'Save as'
                if( $saveDialog -and $saveDialog.IsEnabled ) {
                    Write-Host 'Found "save" dialog'
    
                    # Search for the file name edit control
                    $fileNameField = $saveDialog.FindAllDescendants() | 
                                     Where-Object { $_.ControlType -eq 'Edit' -and $_.AutomationId -eq '1001' -and $_.IsEnabled }
                    if( $fileNameField ) {
                        Write-Host 'Found file name text field'
                        break
                    }
                }
                Start-Sleep 1
            }
    
            $savePath = 'C:\test\paint.png'
    
            # Delete any existing file so we can check if mspaint has saved the file
            $savePath | Where-Object { Test-Path $_ -PathType Leaf } | Remove-Item -Force
    
            # Enter the path into the file name edit control
            $fileNameField.Patterns.Value.Pattern.SetValue( $savePath )
    
            # Find and press the OK button
            $okButton = $saveDialog.FindAllChildren() | 
                        Where-Object { $_.ControlType -eq 'Button' -and $_.AutomationId -eq '1' }
            $okButton.Patterns.Invoke.Pattern.Invoke()
    
            # Wait until file has been saved by mspaint
            while( -not ( Test-Path $savePath ) ) {
                Start-Sleep 1
            }
            Write-Host "File has been saved!"
        }
    }