How to start WinAppDriver.exe programmatically using Java ?
Download WindowsApplicationDriver.msi from https://github.com/Microsoft/WinAppDriver/releases
First let’s see how to start it manually
To run the application, simply start WinAppDriver.exe from its installation directory (E.g. C:\Program Files (x86)\Windows Application DriverIt will open a command line window with Press enter to exit messages. By default Windows Application Driver listening for requests at: http://127.0.0.1:4723/
Don’t type that address in browser and press enter WinAppDriver.exe get closed.
So prior to run your desktop automation scripts , you must start it manually.
Is there a way to automatically start it? Do I need admin rights to start it ?
No , you don’t need admin rights ,you can use below Java code sample .
@BeforeSuitepublic void startWinAppDriver() throws IOException {//option 1String command = "C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe";ProcessBuilder builder = new ProcessBuilder(command).inheritIO();Process p= builder.start();//To terminate the process at the end p.destroy(); Better to add this in @AfterSuite//Option 2- Runtime.getRuntime().exec("C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe");}
Please see the full code which I wrote for notepad .
package winappdriver.sam.demo.winappdriver.demo.samjava;import java.io.IOException;import java.net.URL;import java.util.concurrent.TimeUnit;import org.openqa.selenium.WebElement;import org.openqa.selenium.remote.DesiredCapabilities;import org.testng.Assert;import org.testng.annotations.AfterClass;import org.testng.annotations.BeforeClass;import org.testng.annotations.BeforeSuite;import org.testng.annotations.Test;import io.appium.java_client.windows.WindowsDriver;public class NotePadDemoWithtWinAppDriver {private static WindowsDriver driver = null;private static WebElement editBox = null;private static String NotepadAppId = "C:\\Windows\\System32\\notepad.exe";static Process p;@BeforeSuitepublic void startWinAppDriver() throws IOException {String command = "C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe";ProcessBuilder builder = new ProcessBuilder(command).inheritIO();p = builder.start();// The other way is below commented line of code // Runtime.getRuntime().exec("C:\\Program Files (x86)\\Windows Application// Driver\\WinAppDriver.exe");}@BeforeClasspublic static void setup() {try {DesiredCapabilities capabilities = new DesiredCapabilities();capabilities.setCapability("app", NotepadAppId);driver = new WindowsDriver(new URL("http://127.0.0.1:4723"), capabilities);driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);Assert.assertEquals("Untitled - Notepad", driver.getTitle());editBox = driver.findElementByClassName("Edit");Assert.assertNotNull(editBox);} catch (Exception e) {e.printStackTrace();} finally {}}@AfterClasspublic static void TearDown() {editBox = null;// Set the result variable to nullif (driver != null) {// Check the driver not equal to nulldriver.close();// Close the current window, quitting the browser if it's the last window// currently open.// Click Don't save to exitdriver.findElementByName("Don't Save").click();driver.quit(); // quit the session Quits this driver, closing every associated window.// To terminate the process at the endp.destroy();}driver = null; // set the driver to null}@Testpublic void typeText() {System.out.println("olla mundo");editBox.sendKeys("olla mundo");Assert.assertEquals("olla mundo", editBox.getText());}}