How to type text with WinAppDriver and Node?

Viewed 490

Here is my code:

import { spawn } from 'child_process';
import { dirname } from 'path';
import { WindowsDriver } from 'appium-windows-driver';


async function main() {

    const program = "C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe";
    spawn(program, null, { cwd: dirname(program) });

    const driver = new WindowsDriver();
    await driver.createSession({
        app: 'C:\\Windows\\System32\\notepad.exe',
        deviceName: 'WindowsPC',
        platformName: 'Windows'
    });

    let element = await driver.findElement('xpath', '//*[@ClassName="Edit"]');
    await element.type('Some text here...');

    await driver.deleteSession();
    process.exit();

}

await main();

And here is my dependencies, maybe I missed to install something?

"dependencies": {
  "appium-windows-driver": "^1.19.0"
}

I don't understand why I have this TypeError:

TypeError: element.type is not a function
1 Answers

Finally, I found how to do it.
But with the selenium-appium and selenium-webdriver packages.

import { dirname } from 'path';
import { spawn } from 'child_process';
import { driver, By2, windowsAppDriverCapabilities } from 'selenium-appium'
import { Key } from 'selenium-webdriver';


async function main() {

    const program = 'C:/Program Files (x86)/Windows Application Driver/WinAppDriver.exe';
    spawn(program, [], { cwd: dirname(program) });

    const appExe = 'C:/Windows/System32/notepad.exe';
    await driver.startWithCapabilities(windowsAppDriverCapabilities(appExe));

    const element = By2.nativeXpath('//*[@ClassName="Edit"]');
    await element.click();
    await element.sendKeys('Some text here...');
    await element.sendKeys(Key.CONTROL, 'a');
    await element.sendKeys(Key.DELETE);

    await driver.quit();
    process.exit();

}

await main();
Related