Header Ads

How To Automate JNLP Based Swing Application In Java

Last few days I was trying some research on JNLP based application and I was about to automate the JNLP application by using java.

Is it simple to to automate a JNLP based application?  Let us see ..
JNLP is a java file that runs on the JVM of the operating system but again it got registered as a process of operating system. Some javaw if you open and check task manager. So memory allocation for such application is bit complex. When a JNLP based application is running it is using JRE of JVM. For this Operating system treats it a process and allocate separate area in the memory. Again when I try to develop something I will be using local SDK of java. And Operating system treat this as a separate process and allocate memory to JVM which in term assign memory to my automated script./programing.

Due to pointer unavailability the second JVM can not access the first program running on first JVM. Operating system treat them differently.   

While googling about the same I got some reference about JNA [Java Native Access] which is helpful to automate JNLP based application.By using JNA we can access all the native method present in operating system.
From stackoverflow i got some reference about Window and how to find child window.
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.examples.win32.W32API.HWND;
import com.sun.jna.examples.win32.W32API.LPARAM;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public class IterateChildWindows {
public static final int WM_LBUTTONUP = 514;
public static final int WM_LBUTTONDOWN = 513;
public static final int WM_LBUTTONDBLCLK = 0x203;
static int WM_CLOSE = 0x10;
static String wText;
public interface User32 extends StdCallLibrary {
User32 INSTANCE = (User32) Native.
loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
HWND FindWindow(String lpClassName, String lpWindowName);
int GetWindowRect(HWND handle, int[] rect);
int SendMessage(HWND hWnd, int msg, int wParam, byte[] lParam); 
HWND FindWindowEx(HWND parent, HWND child, String className, String window);
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
boolean EnumChildWindows(HWND parent, WNDENUMPROC callback, LPARAM info);
interface WNDENUMPROC extends StdCallCallback {
boolean callback(HWND hWnd, Pointer arg);
int GetWindowTextA(HWND hWnd, byte[] lpString, int nMaxCount);
long GetWindowLong(HWND hWnd, int index);
int GetClassNameA(HWND in, byte[] lpString, int size);
void SetForegroundWindow(HWND hWnd);
public static void main(String[] args) {    
User32.INSTANCE.EnumWindows(new User32.WNDENUMPROC() {
int count;
public boolean callback(HWND hWnd, Pointer userData) {
// this will be called for each parent window found by EnumWindows(). 
//the hWnd parameter is the HWND of the window that was found.
byte[] textBuffer = new byte[512];
User32.INSTANCE.GetWindowTextA(hWnd, textBuffer, 512);
wText = Native.toString(textBuffer);
wText = (wText.isEmpty()) ? "" : "; text: " + wText;
if (wText.contains("Notepad")){
System.out.println("Found window " + hWnd + ", total " + ++count + wText);
// System.out.println("Window found: " + wText);
// now call EnumChildWindows() giving the previously found parent 
//window as the first parameter
User32.INSTANCE.EnumChildWindows(hWnd, new User32.WNDENUMPROC() {
public boolean callback(HWND hWnd, Pointer userData) { 
// this is called for each child window that EnumChildWindows() 
//finds - just like before with EnumWindows().
byte[] textBuffer = new byte[512];
User32.INSTANCE.GetClassNameA(hWnd, textBuffer, 512);
System.out.println(" - Found sub window /+
 control class: " + new String(textBuffer).trim());
return true;
                }, null);
                return true;
        }, null);

But this useful to find the window open ,activate putting in the foreground and to find all child window of the activated/parent window.But still i am struggling to get access the child element of the window.

Powered by Blogger.