Code Development

CukeTest supports remote automation, allowing you to develop, debug, and execute automation scripts for remote devices locally. This article will detail how to write remote automation scripts and provide a complete Windows Notepad automation example.

Remote Connection API

CukeTest.connect() / sync_auto()

To perform remote automation, you first need to connect to the remote CukeTest Worker service. The CukeTest.connect() (JavaScript) and sync_auto() (Python) methods are designed for this purpose.

JavaScript
Python
interface CukeTest {
    connect(options: ClientConnectOptions): Promise<IAuto>;
}
def sync_auto(options: ClientConnectOptions) -> AutoContextManager

Both methods return a remote automation instance (Auto), through which you can access all automation functions on the remote device, such as Mouse, Keyboard, Screen, Qt Automation, etc. These remote objects provided interfaces identical to local objects, allowing you to write remote scripts just like writing local scripts.

Auto

The remote automation instance contains all available automation libraries:

JavaScript
Python
export interface IAuto {
    capabilities(): Promise<ICapabilities>;
    close(): void;
    readonly runSettings: IRunSettings;
    readonly mouse: IMouse;
    readonly screen: IScreen;
    readonly keyboard: IKeyboard;
    readonly winAuto: IWinAuto;
    readonly qtAuto: IQtAuto;
    readonly util: IUtil;
}
class Auto(SyncContextManager):
    def mouse() -> Mouse
    def keyboard() -> Keyboard
    def screen() -> Screen
    def util() -> Util
    def runSettings() -> RunSettings
    def winAuto() -> WinAuto
    def qtAuto() -> QtAuto
    def cuketest() -> CukeTest
    def visual() -> Visual
    def capabilities() -> "any"

RunSettings

The RunSettings object is used to configure the runtime behavior of remote scripts, such as setting operation timeout or slow motion.

JavaScript
Python
interface IRunSettingsOptions {
    defaultTimeout?: number;
    slowMo?: number;
}
interface IRunSettings {
    set(setting: IRunSettingsOptions): Promise<void>;
    get(): Promise<IRunSettingsOptions>;
}
class RunSettingOptions:
    defaultTimeout: int
    slowMo: int
    reportSteps: bool

class RunSettings(SyncBase):
    defaultTimeout: int
    slowMo: int
    def set(options: TypedDict) -> "None"
    def get() -> "any"

For example, the following code sets the slow motion for each operation to 1 second:

JavaScript
Python
auto.runSettings.set({slowMo: 1000});
auto.runSettings.set({"slowMo": 1000, "reportSteps": True})

Remote Automation in Action: Windows Notepad

The following example demonstrates how to connect to a remote Windows 10 device and automate its built-in "Notepad" application to complete tasks such as text input and file saving.

Script Operation Flow:

  1. Connect to remote device;
  2. Launch Notepad;
  3. Enter text;
  4. Click menu to save file;
  5. Set save path and save;
  6. Close Notepad;
  7. Disconnect remote connection.

Sample Code:

JavaScript
Python
const { CukeTest } = require("cuketest");

(async () => {
    // 1. Connect to remote device
    let auto = await CukeTest.connect({
        wsEndpoint: 'ws://192.168.1.10:3131/ws'
    });

    // Get automation libraries from remote instance
    const { runSettings: RunSettings, winAuto: Auto, util: Util } = auto;

    // Configure run settings
    await RunSettings.set({ slowMo: 1000, reportSteps: true });
    let modelWin = await Auto.loadModel(__dirname + "/recording.tmodel");

    // 2. Launch Notepad
    let proc = await Util.launchProcess("C:\\Windows\\System32\\notepad.exe");
    
    // 3. Enter text
    await modelWin.getDocument("Text Editor").set("Hello World");
    
    // 4. Click menu to save file
    await modelWin.getMenuItem("File(F)").click(20, 5);
    await modelWin.getMenuItem("Save(S)").click(45, 5);
    
    // 5. Set save path and save
    await modelWin.getEdit("File Name:1").set("C:\\temp\\helloworld.txt");
    await modelWin.getButton("Save(S)1").click();
    
    // 6. Close Notepad
    await modelWin.getButton("Close").click();

    // 7. Stop remote process and disconnect
    await Util.stopProcess(proc);
    auto.close();
})();
import os
from auto.sync_api import sync_auto

def recording():
    # 1. Connect to remote device
    with sync_auto('ws://192.168.1.10:3131/ws') as auto:
        # Get automation libraries from remote instance
        RunSettings = auto.runSettings
        Auto = auto.winAuto
        Util = auto.util

        # Configure run settings
        RunSettings.set({"slowMo": 1000, "reportSteps": True})
        modelWin = Auto.loadModel(os.path.dirname(os.path.realpath(__file__)) + "/recording_1.tmodel")

        # 2. Launch Notepad
        proc = Util.launchProcess("C:\\Windows\\System32\\notepad.exe")
        
        # 3. Enter text
        modelWin.getDocument("Text Editor").set("Hello World")
        
        # 4. Click menu to save file
        modelWin.getMenuItem("File(F)").click(33, 2)
        modelWin.getMenuItem("Save(S)").click(50, 15)
        
        # 5. Set save path and save
        modelWin.getEdit("File Name:1").set("C:\\temp\\helloworld.txt")
        modelWin.getButton("Save(S)1").click()
        
        # 6. Close Notepad
        modelWin.getButton("Close").click()

        # 7. Stop remote process
        Util.stopProcess(proc)

if __name__ == "__main__":
    recording()

Precautions in Script Development

When writing remote automation scripts, please be sure to pay attention to the following points to ensure smooth execution of the script:

1. Automation libraries must be obtained from the auto instance

In remote scripts, all automation libraries (such as winAuto, util, keyboard, mouse, etc.) must be obtained from the auto instance returned by the remote connection. Importing directly from local will cause the code to execute locally instead of remotely.

  • Correct Way (Get from auto instance):
    Python
    Auto = auto.winAuto
    Keyboard = auto.keyboard
  • Incorrect Way (Local import):
    Python
    from leanproAuto import Util  # ❌ Error, this causes code to run locally

2. Only supports automation types on the remote target platform

The auto instance returned by remote connection will only contain automation capabilities supported by the target platform. For example, if you connect to a remote Windows device, you cannot access Linux-specific atkAuto. Attempting to use unsupported libraries (such as auto.atkAuto) in the script will cause an error.

Before writing scripts, please verify the automation types supported by the target remote platform.