WINIUM Desktop Automation Example – Java

Follow the steps to create automation scripts for Desktop application using Winium.WebDriver and Java.

In this example I am using following technologies – Java, Maven, TestNG, Winium.WebDriver, UISpy, and Eclipse IDE.

Step 1:

Create a Maven project and update the POM.xml with following dependencies.
Dependencies to include – TestNG, Winium WebDriver, Winium Elements Desktop.

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>SimpleWinium</groupId>
	<artifactId>SimpleWinium</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<dependencies>
		<dependency>
			<groupId>org.testng</groupId>
			<artifactId>testng</artifactId>
			<version>6.14.3</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>com.github.2gis.winium</groupId>
			<artifactId>winium-webdriver</artifactId>
			<version>0.1.0-1</version>
		</dependency>
		<dependency>
			<groupId>com.github.2gis.winium</groupId>
			<artifactId>winium-elements-desktop</artifactId>
			<version>0.2.0-1</version>
		</dependency>
	</dependencies>
</project>

Step 2:

Download the Winium.Desktop.Driver.exe from below link and extract from zip and keep in any location.
https://github.com/2gis/Winium.Desktop/releases

Step 3:

Create a Test Class and Write BeforeClass, AfterClass and one more Test Methods.

Most of them say, you need to start the Winium.Desktop.Driver.rexe manually and keep it open. You can do that if you want ( I have posted that code also at the bottom if someone wants to follow that). But in this example, we will start the Desktop driver before test starts and close it after the test is done automatically.

@BeforeTest:

	@BeforeTest
	public static void setupEnvironment(){
		options = new DesktopOptions(); //Instantiate Winium Desktop Options
		options.setApplicationPath("C:\\Windows\\System32\\calc.exe");
		File driverPath = new File("C:\\WiniumDriver\\Winium.Desktop.Driver.exe");
		service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();
		try {
			service.start();
		} catch (IOException e) {
			System.out.println("Exception while starting WINIUM service");
			e.printStackTrace();
		}
		driver = new WiniumDriver(service,options);
	}

Step 4:

@Test:

	@Test
	public void calculatorTest() throws InterruptedException {
		Thread.sleep(3000);
		driver.findElement(By.id("num8Button")).click();
		driver.findElement(By.name("Multiply by")).click();
		driver.findElement(By.id("num9Button")).click();
		driver.findElement(By.id("equalButton")).click();
		String results = driver.findElement(By.id("CalculatorResults")).getAttribute("Name");
		driver.findElement(By.id("Close")).click();
		System.out.println("Result is: "+ results);
	}

After the test is done, close the service intern it will close the driver. If you close/quit the driver (driver.quit()), then it might through an exception, so don’t have to do that. You can check whether the driver is still running or not by opening the Task Manager.

Step 5:

@AfterTest:

@AfterTest
	public void tearDown(){
		service.stop();
	}

Step 6:

Finally, Create the TestNG XML file and run the tests.

<?xml version="1.0" encoding="UTF-8"?>
<suite name="WiniumTests" verbose="10">
  <test name="SimpleWiniumTest" preserve-order="true">
    <classes>
      <class name="SampleTest"></class>
    </classes>
  </test>
</suite>

Results:

===== Invoked methods
SimpleTest.setupEnvironment()[pri:0, instance:CalcTest@fad74ee] 263025902
SimpleTest.calculatorTest()[pri:0, instance:CalcTest@fad74ee] 263025902
SimpleTest.tearDown()[pri:0, instance:CalcTest@fad74ee] 263025902
PASSED: calculatorTest
===============================================
SimpleWiniumTest
Tests run: 1, Failures: 0, Skips: 0
===============================================
WiniumTests
Total tests run: 1, Failures: 0, Skips: 0

If someone wants to start the Winium.Desktop.Driver manually and keep it open, then update the setupEnvironment method.

	@BeforeTest
	public static void setupEnvironment() {
		options = new DesktopOptions();
		options.setApplicationPath("C:\\Windows\\System32\\calc.exe");
		driver = new WiniumDriver(new URL(“http://localhost:9999”),option);
	}

Complete Source Code can be found here:

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.winium.DesktopOptions;
import org.openqa.selenium.winium.WiniumDriver;
import org.openqa.selenium.winium.WiniumDriverService;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SimpleTest {

	static DesktopOptions options;
	static WiniumDriverService service;
	static WiniumDriver driver;

	@BeforeTest
	public static void setupEnvironment(){
		options = new DesktopOptions();
		options.setApplicationPath("C:\\Windows\\System32\\calc.exe");
		File driverPath = new File("C:\\WiniumDriver\\Winium.Desktop.Driver.exe");
		service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();
		try {
			service.start();
		} catch (IOException e) {
			System.out.println("Exception while starting WINIUM service");
			e.printStackTrace();
		}
		driver = new WiniumDriver(service,options);
	}

	@Test
	public void calculatorTest() throws InterruptedException {
		Thread.sleep(3000);
		driver.findElement(By.id("num8Button")).click();
		driver.findElement(By.name("Multiply by")).click();
		driver.findElement(By.id("num9Button")).click();
		driver.findElement(By.id("equalButton")).click();
		String results = driver.findElement(By.id("CalculatorResults")).getAttribute("Name");
		System.out.println("Result is: "+ results);
		driver.findElement(By.id("Close")).click();
	}

	@AfterTest
	public void tearDown(){
		service.stop();
	}
}

2 comments

  1. Latina Schweder

    Greetings! Very helpful advice in this particular post! It is the little changes that will make the greatest changes. Thanks for sharing!

    1. Raj

      Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *