The test should speak to the document the user is interacting with, not imitate a user by clicking guessed pixels.

An HTA is a desktop window with a real HTML document

An HTA runs inside mshta.exe. Its interface is HTML hosted by MSHTML, so the useful thing to test is the document: can the application find an element, read its value, change it, and observe the result of a click? A screenshot can answer whether something looks right, but it cannot by itself prove that a control has the right value or that its event handler changed the page.

The obstacle is the boundary around the embedded browser. A desktop automation inventory can expose the HTA window and an Internet Explorer_Server child while exposing little or none of the HTML element tree as ordinary UI Automation controls. That does not mean the document is inaccessible. MSHTML exposes a window message, WM_HTML_GETOBJECT, that can be sent to the browser pane; oleacc!ObjectFromLresult turns the returned accessibility result into an IHTMLDocument2 interface.

A sequence diagram showing a striped-trousers test reaching an HTA DOM through a LiveScript client, PowerShell bridge, Win32 messages, and MSHTMLA sequence diagram showing a striped-trousers test reaching an HTA DOM through a LiveScript client, PowerShell bridge, Win32 messages, and MSHTML
The framework owns the test-facing contract; PowerShell crosses the Windows native boundary and returns a text result.

Keep the operating-system bridge narrow

The framework module os.shell.tools.HtaAutomation is the public boundary. It locates the PowerShell helper, encodes arguments as UTF-8, starts it through the framework's PowerShell tool, checks the process result, and decodes the response. Its document object offers four operations: get element text, get a control value, set a control value, and click an element. Each operation returns the framework's usual value-and-error pair and wraps failures in the os.shell.tools.HtaAutomation error context.

The helper performs the Windows-specific work: enumerate windows by their exact title, find the Internet Explorer_Server pane, obtain the document interface, then use getElementById and normal MSHTML properties and methods. It returns UTF-8 text as Base64 so console code pages cannot corrupt a ballot or another non-ASCII value. The helper does not decide what a test means, and the framework does not pretend that WSH JScript can call arbitrary native APIs directly.

This division also makes the PowerShell dependency honest. PowerShell is not being used to write the article's test suite or to replace LiveScript. It is the small adapter at the native Windows boundary, where the test needs Win32 and COM interop that the existing WSH layer does not expose.

The LiveScript scenario stays readable

A scenario should describe behavior and assertions, not repeat window enumeration or Base64 plumbing. The actual success leg starts the fixture HTA, attaches by its title, reads the initial value, writes automated ballot ✓, clicks the button, and checks the status text. The code below uses the real framework API; required extracts a value or throws the namespaced error when a setup step cannot proceed.

automation = required (try-create-hta-automation bridge), 'Unable to create client'
document = required (automation.try-attach 'Framework HTA automation probe'), 'Unable to attach'

assert-equal (required (document.try-get-element-value 'message'), 'Unable to read'), 'initial value'
assert-equal (required (document.try-set-element-value 'message', 'automated ballot ✓'), 'Unable to set'), 'automated ballot ✓'
required (document.try-click-element 'action'), 'Unable to click'
assert-equal (required (document.try-get-element-text 'status'), 'Unable to read status'), 'clicked: automated ballot ✓'

PowerShell is syntax, not a cmd.exe variant

The bridge uses PowerShell parameters, variables, type literals, method calls, and a switch over the requested operation. Those forms are not Windows batch syntax. The grammar composer already had a windows-cmd scanner for command examples and a shared lscript grammar for LiveScript; neither one should be stretched to color PowerShell. A new powershell.peg is now registered automatically by its location in the shared grammar catalog, and the code block below selects it with language: powershell.

It is intentionally a lexical highlighter, not a PowerShell parser. It recognizes common strings and here-strings, variables, type literals, numeric forms, comparison operators, parameters, keywords, command-shaped names, and punctuation. It does not prove that a cmdlet exists or that a script is valid for a particular PowerShell version. The existing LiveScript PEG remains the grammar for the LiveScript test example above; the blog maps language: livescript to its shared lscript grammar ID.

$windows = [HtaAutomationNative]::FindWindows($Title)
foreach ($window in $windows) {
  $panes = [HtaAutomationNative]::FindHtmlPanes($window)
  foreach ($pane in $panes) {
    $documents += [HtaAutomationNative]::GetDocument($pane)
  }
}

$element = Get-HtaElement $document $ElementId
$element.value = $Value
$result = [string]$element.value
[Console]::Out.WriteLine([Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($result)))
A flowchart shows a striped-trousers test reaching the DOM and asserting either successful behavior or a complete namespaced errorA flowchart shows a striped-trousers test reaching the DOM and asserting either successful behavior or a complete namespaced error
Both success and failure outcomes are deliberate test legs with observable expectations.

Failure legs are part of the contract

Striped trousers means the suite covers the required success leg and the failure legs. Here the success leg verifies a live interaction. One failure leg attaches to a nonexistent window and checks that the returned error includes the framework namespace and the underlying “No HTA document found” explanation. Another attaches successfully, requests a nonexistent element, and checks for the specific element message. The test does not merely accept “some error”; it inspects the diagnostic the caller will actually receive.

This matters because automation failures are often environmental: the fixture did not start, the title changed, the document pane was not found, or an element ID drifted. Namespaced cause chains preserve which boundary failed. In the successful leg, Unicode passing through the process boundary is also asserted, so a green result covers actual text rather than only ASCII control flow.

What this approach proves, and what it does not

The automation runs in the interactive Windows session that owns the HTA. It locates the window by exact title and works with its MSHTML DOM; it is not a remote browser protocol, a general-purpose UI Automation provider, or a visual layout test. The test proves that named document operations work. A separate screenshot or browser inspection is still useful when the requirement is visual fidelity.

The first implementation starts PowerShell for each document operation. That is a clear process boundary and keeps the client simple, but it adds latency. If a test later needs many rapid DOM calls, a persistent bridge could reduce startup cost while preserving the same public contract. That should be driven by measured test time, not introduced speculatively. The module and its striped-trousers tests now give that future change a stable behavior to preserve.

The broader lesson is that HTA is testable without treating its HTML as a pile of pixels. The framework keeps the reusable client and error contract in one place; the operating-system helper does only the native interop; and each application test says what the document must do. Existing LiveScript PEG highlighting handles the test code, while a separate PowerShell PEG handles the bridge code. The article contains actual rendered PlantUML and Mermaid pictures; they are generated during the site build and are not live diagrams in the page.