Winappdriver: Send command prompt command

Created on 5 Aug 2019  路  3Comments  路  Source: microsoft/WinAppDriver

Hello all,

I am trying to send a command to clean up some running .exe processes that might be "residual" from previous test cases that didn't clear properly, so needed to run a cleanup TASKKILL cmd, but not sure how I can execute this using WAD (if it makes any difference, using Appium and Java bindings).

Any ideas?

Many thanks

Community Input Welcome Question

Most helpful comment

For this to work you need to create a new session with cmd.exe. I wrote myself this helper function below:

protected static void RunDosCommand(string command)
{
    DesiredCapabilities appCapabilities = new DesiredCapabilities();
    appCapabilities.SetCapability("app", "cmd.exe");
    appCapabilities.SetCapability("appArguments", $"/c {command}");
    appCapabilities.SetCapability("deviceName", "WindowsPC");
    try
    {
        // this will always fail, but launches our cmd.exe
        var unused = new WindowsDriver<WindowsElement>(new Uri("http://172.20.1.93:4723/wd/hub"), appCapabilities);
    }
    catch (Exception)
    {
        // ignored
    }
}

and now simply call it: RunDosCommand(@"taskkill /F /IM application.exe");

All 3 comments

no, you cannot kill a another process with WAD, you can kill using:
Runtime rt = Runtime.getRuntime();
if (System.getProperty("os.name").toLowerCase().indexOf("windows") > -1)
rt.exec("taskkill " +....);
else
rt.exec("kill -9 " +....);

But this will execute on the VM where the test is being executed rather than the node?

I need to send the command to the remote node that WAD is on and commands are being sent to...

For this to work you need to create a new session with cmd.exe. I wrote myself this helper function below:

protected static void RunDosCommand(string command)
{
    DesiredCapabilities appCapabilities = new DesiredCapabilities();
    appCapabilities.SetCapability("app", "cmd.exe");
    appCapabilities.SetCapability("appArguments", $"/c {command}");
    appCapabilities.SetCapability("deviceName", "WindowsPC");
    try
    {
        // this will always fail, but launches our cmd.exe
        var unused = new WindowsDriver<WindowsElement>(new Uri("http://172.20.1.93:4723/wd/hub"), appCapabilities);
    }
    catch (Exception)
    {
        // ignored
    }
}

and now simply call it: RunDosCommand(@"taskkill /F /IM application.exe");

Was this page helpful?
0 / 5 - 0 ratings