Wednesday, November 6, 2019

Test Automation for Windows Desktop Application using Winium with Java

Prerequisites to work with Winium:

1. Microsoft .NET Framework
2. Eclipse (as IDE)
3. Java/JDK
4. TestNG
5. Maven
6. Inspection Element tool (such as Inspect.exe)

Step 1: Creating a Maven project

Open Eclipse IDE and create a Maven project. Update the pom.xml with the necessary dependencies. Dependencies to include – Winium WebDriver, Winium Elements Desktop, Selenium Java, TestNG.

<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>WiniumWithJava</groupId>
  <artifactId>WiniumWithJava</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
  <!-- https://mvnrepository.com/artifact/com.github.2gis.winium/winium-webdriver -->
<dependency>
    <groupId>com.github.2gis.winium</groupId>
    <artifactId>winium-webdriver</artifactId>
    <version>0.1.0-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.2gis.winium/winium-elements-desktop -->
<dependency>
    <groupId>com.github.2gis.winium</groupId>
    <artifactId>winium-elements-desktop</artifactId>
    <version>0.1.0-1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.0.0</version>
    <scope>test</scope>
</dependency>
  </dependencies>
</project>

Step 2: Downloading Winium.Desktop.Driver.exe

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

Step 3: Writing Winium Java code

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.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class NotepadExample{
    WiniumDriver driver;
    DesktopOptions option;
    WiniumDriverService service;

@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {
option = new DesktopOptions();
option.setApplicationPath("C:\\Windows\\System32\\notepad.exe");
File driverPath = new File("E:\\QA_720\\Winium\\Winium.Desktop.Driver\\Winium.Desktop.Driver.exe");
service = new WiniumDriverService.Builder().usingDriverExecutable(driverPath).usingPort(9999).withVerbose(true).withSilent(false).buildDesktopService();
try{
service.start();
}catch(IOException ioe){
System.out.println("Exception while starting WINIUM service");
ioe.printStackTrace();
}
driver = new WiniumDriver(service, option);
}
@AfterClass(alwaysRun = true)
public void tearDown() throws Exception {
service.stop();
}

@Test
public void testNotepad() throws Exception{
Thread.sleep(1000);
driver.findElement(By.name("Text Editor")).sendKeys("This is sample Winium test");
driver.findElement(By.id("Close")).click();
driver.findElement(By.name("Don't Save")).click();
}
}

Step 4: Running the test using TestNG

In Eclipse project, right click on the class "NotepadExample.java" -> Mouse over Run As -> Click TestNG Test

Enjoy test execution of Notepad. The following steps would do by running this:
1. Open Notepad
2. Writing some text on Notepad
3. Close the Notepad without saving it