Windows application Test Automation (Part 3)
In my previous articles, I explained, how to prepare a Windows PC for Test automation and invoke the required software. If you have not read my previous articles, I would highly recommend you to go through them since they are linked to this article. You could find those links at the bottom of this article.
In this article I attempt to cover the following;
- Preparing JAVA test script for execution
- Pre-launch check and launch
Pre-requisites
Please make sure you have installed following applications;
1. Java version 8+
2. Appium /WinAppDriver
3. Java IDE (Eclipse /IntelliJ IDEA)
if not , please refer how to configure Windows application Test Automation (Part 1)
If everything above is installed, Let’s jump right into it !!!
Preparing JAVA test script for execution
In this section, I attempt to cover, what to include in your programming code, in order to execute the tests on windows. I’m going to use JAVA as the programming language for this.
It is important to know what are the Desired Capabilities and how they work if you do not know what is Desired Capabilities please refer: https://appium.io/docs/en/writing-running-appium/caps/
In the simple terms, what we are setting up the desired configuration to communicate between server (check my previous article), in the way that we need to control the Application Under Test (AUT).
Following desired capabilities are sufficient for most of the cases, however, it totally depends on your requirement which capabilities to include in your tests. To find out what are the supported capabilities and how to use them, refer:https://github.com/appium/appium-windows-driver#readme
Other than the above, Desired Capabilities there are two more mandatory parameters that must be included in the Java test script;
- IP address: this will be the IP address of the WinAppDriver/Appium server, use the IP address which I explained in my previous article.
- Port: this will be the port of the WinAppDriver/Appium server
If you do not recall which IP and Port you set, refer to Final Note in Windows application Test Automation (Part 2)
Now it’s time to apply theory into Java coding;
You need to create a DesiredCapabilities object and set the recommended configuration using setCapability function. I do not intend to explain each line of code, but few key points which you need to thorough when you writing the first code. Otherwise, it’s most unlikely to run the test script successfully.
- Ensure the corresponding value to “app” is on the correct path and must be pointed to an application (.exe).
- IP address and port must be pointed to WinAppDriver/Appium server.
You may follow the below sample code snippet;
Complete code can be found on: github.com/KasunWicky
@Test
public void launchApplication() {DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("app","C:\\WINDOWS\\system32\\notepad.exe");
capabilities.setCapability("platformName", "WINDOWS");
capabilities.setCapability("deviceName", "WINDOWS");try {
WindowsDriver<WebElement> driver = new WindowsDriver<>(
new URL("http://127.0.0.1:4723"),capabilities);} catch (MalformedURLException malformedEx) {
Assert.fail("Notepad is not launched.\t" + malformedEx.getMessage() );} catch (Exception exception) {
Assert.fail("Notepad is not launched.\t" + exception.getMessage());
}
}
Pre-launch check and launch
In the previous section, we created our first Java code to automate Windows-based applications. Now it’s time to launch it, but before we move forward, we need to perform a final check before the launch.
- Check the WinAppDriver/Appium is launched.
Once the test script is executed, if everything is successfully executed, configured application for the test (in our case notepad) should pop up.
Another way to identify is that sessionId is created on WinAppDriver/Appium, and its log will be displayed in server logs.
So how do write test cases for free ???
So far I have explained, what to install, how to configure, and how to launch a test on Windows Application, but there is a missing piece of the puzzle, I have not declared yet, how to use it for free.
If you have not noticed, when I was setting up the test environment, I set up everything in order to comply with the Selenium ecosystem. Once the Desired Capabilities are assigned to the driver and set the element type as WebElement, it will start behaving as a command for a Selenium Web Application.
Most of the Selenium APIs which are used in Web automation tests are supported for Window test automation also, except a few. So, the exact same way how APIs are used for web automation now they can be executed on Windows also with no effort. So without any extra learning curve or extremely expensive software now you could use Selenium APIs for Windows test execution.
What about the element (object) capturing ???
Of course, since it’s freeware, you will not get the whole package together, so what is the freely available software we could use for element capturing.
You need to set the Desired Capabilities in Appium Desktop in the same way you set in the test script before you inspect the element of the application about to automate (in our case notepad). Appium Desktop inspect tool will only be pointed to a single particular application and you must have an application to use this tool.
Once the Desired Capabilities are set, click the ‘Start Session’ button.
After few seconds Appium application inspect window Appears with
2. Windows Accessibility Tools -Inspect (formerly known as inspect.exe)
Special configuration is not needed, once you install the ‘Accessibility tools’ just hover over the mouse on the required element. Unlike Appium desktop inspector, whatever the element you point to, will obtain the element.
More info on using inspect.exe: Accessibility Tools- inspect.exe
This tool is kind of similar to Selenium IDE. Also, it has a recording feature. No special configuration is needed before the start, other than the software installation. Just Click the ‘Record’ button, then it’s ready to inspect any element in the Windows.
I hope the above element capturing tools above, are more than enough for your Windows automation requirement. If you feel one tool is not working, there are two more tools to give you input on which element to choose.
Here I showed only a simple application to understand the concept, but you could automate the much matured Windows applications to test using the same way. Of course, it will not be easy as, Web App test automation, but rest assured it’ll be an amazing exploring experience.
That will be the end of my Windows Test Automation article series. I hope to meet you in another series of articles. Please share your questions and comments below, I will try to address them all as possible.
The complete project can be found on: github.com/KasunWicky
To visit my previous articles click the below links;