Winium example with word automation

I have been exploring the new tools for windows based apps automation, having worked on white API and its lack of documentation and complexity in writing the code, i was searching for good alternative.

Winium seems to be one of the options suggested by selenium group of people, as the syntax is similar to selenium.

Winium uses the winium.desktop.driver to start off the listening of the commands sent by the driver and redirect them to the application under test. There are several dependencies that are required just as selenium web driver for the desktop driver.

The basics need to work on WInium is

  1. Winium.desktop.driver.exe (winium.driver.exe)
  2. Eclipse with maven plugin
  3. Inspect tool  / .Net framework with windows tool kit
  4. Selenium background

Eclips with maven plugin : This is required since there are dependencies that are required to work with Winium, with maven plugin and with the POM file we can download all the plugins.  the POM file is given below for the dependencies.

<project xmls="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>WiniumTests</groupId>
<artifactId>WiniumTests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.2gis.winium</groupId>
<artifactId>winium-webdriver</artifactId>
<version>0.1.0-1</version>
</dependency>
</dependencies>
</project>

Once you have created the project and updated the project with dependencies. You can create a test case and will be able to use the sample code below.

Just like chrome driver options we can initiate the desktop driver with options.

DesktopOptions option = new DesktopOptions();
option.setApplicationPath("C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE");

First line is to create the options object for the desktop driver.

After creation of the object, set the application under test executable path. In the above case it is windows word.

Let us look at how do we identify the windows objects. In case of web elements we can use firebug, firepath or developer mode in browsers to get required css or xpaths.

In the windows based apps, there is tool called Inspect.exe which will be located in below path.

C:\Program Files (x86)\Windows Kits\8.1\bin\x86\Inspect.exe

Or you can search for it in entire computer, just in case of you don’t find it.

2. Inspect tool : This tool is similar to developer tools of browser. It can tell us the windows object properties. We can make use of some of the properties like classname, name, automation id etc to identify and perform operations on those objects.

InspectScreenshot

In the current context we are trying to do word automation, so to go to insert tab, we can use find element by name of it, as you see the properties description it says name is ‘Insert’

Similarly we can identify the object based on their names. Following is the code for the identification of some of the basic word GUI objects.

//Selects the blank document
driver.findElement(By.name("Blank document")).click();
//Goes to insert tab
driver.findElement(By.name("Insert")).click();
//Clicks on shapes
driver.findElement(By.name("Shapes")).click();
//Selects the rectangle
driver.findElement(By.name("Rectangle")).click();
//Clicks on rectangle to get it on the word.
driver.findElement(By.name("Page 1")).click();

The entire code is below.

package com.tests;

import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.winium.DesktopOptions;
import org.openqa.selenium.winium.WiniumDriver;

public class Tests 
{

@Test
public void CalcTest() throws MalformedURLException, InterruptedException
{

DesktopOptions option = new DesktopOptions();
option.setApplicationPath("C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE");

WiniumDriver driver = new WiniumDriver(new URL("http://localhost:9999"), option);
//SLeep is to wait until the word becomes visible
Thread.sleep(5000);
driver.findElement(By.name("Blank document")).click();
driver.findElement(By.name("Insert")).click();
driver.findElement(By.name("Shapes")).click();
driver.findElement(By.name("Rectangle")).click();
driver.findElement(By.name("Page 1")).click();
driver.quit();
}

}

Entire code is given in the below git repo.

Winium_Example

Before we execute any of the test cases, we need to start the winium driver exe as it is the listener to the events provided by the driver. Also, check the port on which it is running , use the same port while creating the driver.

Following is the demo.

 

Thank you. Please write in comments in case you need more information on winium automation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s