Upload File in Selenium using WinAppDriver

Profile picture for user arilio666

This article will use winappdriver to upload a file in selenium Similar to Selenium, Windows Application Driver is a service to support windows application tools.

Like Selenium, WinAppDriver is a set of libraries we can integrate into any Test Runner that supports Appium. WinAppDriver is often abbreviated as "WAD." WAD is bundled with Appium; you do not need to install it separately.

For this article, we will be using the autopract upload site.

Upload File in Selenium using WinAppDriver
  • So after clicking add files, we will use inspect.exe and get the locators of the file path box and open button.
 System.setProperty("webdriver.edge.driver",
   "C:\\Users\\arili\\git\\Assignments\\Selenium\\target\\msedgedriver.exe");

 // Creating an object of ChromeDriver
 WebDriver driver = new EdgeDriver();
 driver.get("http://autopract.com/selenium/upload1/");
 driver.manage().window().maximize();

 driver.findElement(By.xpath("//span[@class=\"btn btn-success fileinput-button\"]")).click();
 Thread.sleep(5000);
 Runtime.getRuntime().exec("C:\\Program Files (x86)\\Windows Application Driver\\WinAppDriver.exe");

 DesiredCapabilities desktopCapabilities = new DesiredCapabilities();
 desktopCapabilities.setCapability("app", "Root");

 WindowsDriver wd = new WindowsDriver(new URL("http://127.0.0.1:4723/"), desktopCapabilities);

 wd.findElement(By.name("File name:")).sendKeys("C:\\Users\\arili\\git\\Assignments\\Selenium\\target\\F.png");
 wd.findElement(By.name("Open")).click();
  • After clicking the add files button using selenium, we will use the runtime to open the winappdriver.exe to keep it open and then pass in the desired capabilities as root argument because its scope should be on the whole desktop.
  • Then the argument is passed, and we can automate the window GUI and upload a file onto the site with the locators we took from the windows popup.