- Tutorial: Windows App Automation using WinAppDriver and Java
- WinAppDriver: The Basics
- Preparing our environment
- Preparing the Java project for Windows Automation
- Writing Windows Automation Test
- Locating elements for Windows Desktop Tests
- Assert or not Assert?
- Conclusion
- How to create a windows service from java app
- 19 Answers 19
- Running JAR file on Windows
- 25 Answers 25
Tutorial: Windows App Automation using WinAppDriver and Java
W hen we talk about Test Automation, the first thing that comes to mind is usually Mobile and Web Automation. However, as testers we should be aware of the latest technologies in Desktop Automation as well.
I was always curious about automating Desktop applications, but there seems to be less data online about Desktop Automation.
Let’s fill this knowledge gap by trying an interesting tool for Windows Automation — WinAppDriver. You could also follow along with a video tutorial here.
Also, if you are interested in Web Testing, consider watching my video tutorial about TestCafe and WebDriver + POM .
WinAppDriver: The Basics
Essentially, this is a Selenium-like UI Automation tool. It’s an Appium — compatible WebDriver server for Windows applications. It supports testing Universal Windows Platform (UWP) and Classic Windows (Win 32) applications.
What’s great about it is that it’s a standalone tool and there is no need to install Appium on your machine to run automation scripts. You just need to install WinAppDriver through the installer and use it with your favorite programming language and IDE.
Of course, you can also install Appium directly and it will include the WinAppDriver, but for our tutorial we will be installing WinAppDriver separately.
Since it has Selenium-like language, we can use syntax like this for our tests:
Preparing our environment
Here is the list of components that we need for our Tests to function:
Please download all the components using the links above (except Java JDK if you want IntelliJ IDEA to install it automatically during the project configuration) and install those. There should be nothing complicated about installing those, just follow the installation wizards.
Once we have those installed, there are a couple of things that we need to configure.
3. Start WinAppDriver. This is our key component that will be used for automating our tests.
By default, the WinAppDriver is located here:
Also, notice that like any other Selenium — like tool it also runs as a server on a specific IP address and port. By default, it’s http://127.0.0.1:4723, and that’s what we will use in our tests.
Start the WinAppDriver session and minimize it. It’s ready to accept our test requests.
Preparing the Java project for Windows Automation
We are ready to write our Windows Tests. After creating the project, let’s configure the pom.xml located in the root of our test project and make it look similar to this:
After adding those lines, press the Maven “ sync” button on the right side. We are making our test configuration similar to what we have done in this article (except adding Maven SureFire).
We are adding those three libraries to our project:
Also, we are specifying that we use Java 1.8 by specifying “compiler.source” and “compile.target” values. Better to have everything explicitly specified.
We are ready to write our tests. In this case, things will be simple and we are not going to use the PageObject model. We need to create a class that will be our automation script.
Go ahead and create the “ NotepadTest” class in “ [project name]\src\test\java\”.
Writing Windows Automation Test
Here is where the fun part comes. We have discussed what application we are going to automate. Now let’s decide what Test Cases are going to look like:
It’s always a good idea to write and execute those test cases manually first to understand the test flow. Here is how our code will look like for “ NotepadTest” class:
Alright, what’s happening here? Let’s review the code starting from the beginning:
This function is easy to understand. We are using it to generate a current local date. Then we are converting it to a String. We will use this function further to enter this text into our Notepad window.
Next, we are setting our environment for executing our tests. We are setting our executable file and specifying the IP Address and Port to run our tests on.
Those are the default parameters, but please make sure that it matches the address of the already running session of WinAppDriver that we started earlier. Those lines perform the start of the Notepad application.
There is also a 2 second timeout that we are performing before executing our tests:
Afterwards, all that’s left to perform is pressing the buttons and typing the values. Here is the example of the button click action:
Similarly, this how the text is sent to a specific element. We are taking the output of getDate() function and sending the output to a Notepad:
If you are ready to execute the tests right-click and press the “ Run NotepadTest” button:
Is this everything that we need to understand how to perform tests? Almost, but not exactly. Here is the question that you probably still have in mind: “ How to locate those elements in the application?”.
Locating elements for Windows Desktop Tests
To be able to click buttons and type text into the fields we need to be able to locate those elements first. We need to find “l ocators” for each element that we use in our tests. There are multiple ways of locating elements for our Windows Tests:
As discussed above in the article, we will be using the last tool from this list. Since we have already installed the Windows 10 SDK we should have this tool ready for use. Feel free to try other tools as well, if you wish.
The “ inspect.exe” tool is located in this directory on my system:
It might a bit different in your case (if you run a 32-bit Windows system), but by default it should be in your Program Files\Windows Kits\10\bin directory.
Let’s launch the inspect.exe application and see how it could assist us.
The idea is simple. You open an application (such as “ Notepad” in our case) and you point to an element that you would like to use in your tests. The Inspect app shows the corresponding information about the selected object.
How do we use it in our tests? Let’s take the example above. The use has pointed to “ Time/Date” element and the app shows two locator strategies available to us:
Both of the locators point to the same object (“Time/Date” menu item), but if you try accessing it by “ Name”:
It won’t work in such a scenario. That’s why we are using an “ AutomationId”:
As you can see, the corresponding name of “ AutomationId” in WinAppDriver is “ AccessibilityId ”. One thing to note is that the best locators are the ones that have a unique ID that is unlikely to change in the future.
There are more options available. For example, when we are typing the information inside the Notepad we are using the “ ClassName” element locator:
Assert or not Assert?
There is one case in the code that we haven’t examined yet:
An assertion is a powerful tool that is used primarily in Unit Testing. As you can see in this specific example, we are checking that the text is not empty in our Notepad.
Since we have text typed in our Notepad session, this test is going to pass. If you are curious, you could try changing the “ assertNotNull” to “ assertNull” and see how the test is going to perform afterward.
Conclusion
I hope that this tutorial was useful and allowed you to jump-start into Windows Application Testing. We have not touched the PageObject/PageFactory concept in this tutorial, but if you are willing to learn more about Web Automation feel free to learn more about it in this tutorial. You can apply many concepts from that tutorial and improve our tests.
If you are looking for more Java-samples of how to use the WinAppDriver you can see samples from Microsoft. If you are a Mac user there is also an Appium Mac Driver available.
There is so much more to explore in the exciting world of Test Automation !
How to create a windows service from java app
I’ve just inherited a java application that needs to be installed as a service on XP and vista. It’s been about 8 years since I’ve used windows in any form and I’ve never had to create a service, let alone from something like a java app (I’ve got a jar for the app and a single dependency jar — log4j). What is the magic necessary to make this run as a service? I’ve got the source, so code modifications, though preferably avoided, are possible.
19 Answers 19
Apache Commons Daemon is a good alternative. It has Procrun for windows services, and Jsvc for unix daemons. It uses less restrictive Apache license, and Apache Tomcat uses it as a part of itself to run on Windows and Linux! To get it work is a bit tricky, but there is an exhaustive article with working example.
Besides that, you may look at the bin\service.bat in Apache Tomcat to get an idea how to setup the service. In Tomcat they rename the Procrun binaries (prunsrv.exe -> tomcat6.exe, prunmgr.exe -> tomcat6w.exe).
Something I struggled with using Procrun, your start and stop methods must accept the parameters (String[] argv). For example «start(String[] argv)» and «stop(String[] argv)» would work, but «start()» and «stop()» would cause errors. If you can’t modify those calls, consider making a bootstrapper class that can massage those calls to fit your needs.
With Apache Commons Daemon you can now have a custom executable name and icon! You can also get a custom Windows tray monitor with your own name and icon!
I now have my service running with my own name and icon (prunsrv.exe), and the system tray monitor (prunmgr.exe) also has my own custom name and icon!
Download the Apache Commons Daemon binaries (you will need prunsrv.exe and prunmgr.exe).
Rename them to be MyServiceName.exe and MyServiceNamew.exe respectively.
Download WinRun4J and use the RCEDIT.exe program that comes with it to modify the Apache executable to embed your own custom icon like this:
Now install your Windows service like this (see documentation for more details and options):
Now you have a Windows service of your Jar that will run with your own icon and name! You can also launch the monitor file and it will run in the system tray with your own icon and name.
One more option is WinRun4J. This is a configurable java launcher that doubles as a windows service host (both 32 and 64 bit versions). It is open source and there are no restrictions on its use.
(full disclosure: I work on this project).
Yet another answer is Yet Another Java Service Wrapper, this seems like a good alternative to Java Service Wrapper as has better licensing. It is also intended to be easy to move from JSW to YAJSW. Certainly for me, brand new to windows servers and trying to get a Java app running as a service, it was very easy to use.
Some others I found, but didn’t end up using:
- Java Service Launcher I didn’t use this because it looked more complicated to get working than YAJSW. I don’t think this is a wrapper.
- JSmooth Creating Window’s services isn’t its primary goal, but can be done. I didn’t use this because there’s been no activity since 2007.
If you use Gradle Build Tool you can try my windows-service-plugin, which facilitates using of Apache Commons Daemon Procrun.
To create a java windows service application with the plugin you need to go through several simple steps.
Create a main service class with the appropriate method.
Include the plugin into your build.gradle file.
The same script snippet for new, incubating, plugin mechanism introduced in Gradle 2.1:
Configure the plugin.
Run createWindowsService gradle task to create a windows service distribution.
That’s all you need to do to create a simple windows service. The plugin will automatically download Apache Commons Daemon Procrun binaries, extract this binaries to the service distribution directory and create batch files for installation/uninstallation of the service.
In $
-install.bat and if you want to uninstall the service run
-uninstall.bat . To start and stop the service use
Note that the method handling service start should create and start a separate thread to carry out the processing, and then return. The main method is called from different threads when you start and stop the service.
For more information, please read about the plugin and Apache Commons Daemon Procrun.
Running JAR file on Windows
I have a JAR file named helloworld.jar. In order to run it, I’m executing the following command in a command-line window:
This works fine, but how do I execute it with double-click instead? Do I need to install any software?
25 Answers 25
Easiest route is probably upgrading or re-installing the Java Runtime Environment (JRE).
- Open the Windows Explorer, from the Tools select ‘Folder Options. ‘
- Click the File Types tab, scroll down and select JAR File type.
- Press the Advanced button.
- In the Edit File Type dialog box, select open in Actions box and click Edit.
- Press the Browse button and navigate to the location the Java interpreter javaw.exe.
- In the Application used to perform action field, needs to display something similar to C:\Program Files\Java\j2re1.4.2_04\bin\javaw.exe» -jar «%1» % (Note: the part starting with ‘javaw’ must be exactly like that; the other part of the path name can vary depending on which version of Java you’re using) then press the OK buttons until all the dialogs are closed.
In Windows Vista or Windows 7, the manual file association editor has been removed.
The easiest way is to run Jarfix, a tiny but powerful freeware tool. Just run it and your Java apps is back. double-clickable again.
If you need to distribute your .jar file and make it runnable at other people’s Windows computers, you can make a simple .bat file like this in the command prompt:
and place the .bat file in the same directory as your .jar file.
If you have a jar file called Example.jar, follow these rules:
- Open a notepad.exe
- Write : java -jar Example.jar
- Save it with the extension .bat
- Copy it to the directory which has the .jar file
- Double click it to run your .jar file
An interesting side effect of this causes a problem when starting runnable jar files in the command prompt.
If you try (in a command prompt):
No joy, because this is being translated to the following (which doesn’t work):
However, the following command does work:
If you change the association in file manager as described above to:
Then you can type:
in the command prompt and it will now work!
EDIT:(However you then get a black console window when you run a form based (non console) Java app, so this is not an ideal solution)
If you run these jar files by double clicking them in windows, no parameters will be passed so your Java code needs to handle the stack overflow exception and include a «press a key» function at the end or the window will just disappear.
In order to pass a parameter in windows you have to create a shortcut to the jar file, which includes the parameter in the target line (right click on the shortcut and select properties) you can not add parameters to the jar file icon itself in this way.
There isn’t a single, consistent solution here, but you would have the same problem with any other console application.
There is a windows freeware application called «bat to exe» which you can use to create an exe file from a .bat file with the apropriate command line in it. you can also embed the jar file in the exe with this application, and make it clean it up when it has finished running, so this may be a more elegant solution.