Like I mentioned in the previous posts, I have this requirement where I run tests using SWT Browser Widget on thousands of URLs. And I am start the test and leave the lab. But then for some URL, the SWT Browser just pops up a dialog either asking for password or warning about a bad certificate. This has been my biggest problem off late and has delayed my tests because unless you close the dialogs the program, the execution cannot move forward.(Like the one shown in the image)
I tried out various options like configuring the settings of Mozilla which is used by SWT Browser when the widget runs on Linux (It uses IE when is running on Windows). But this hasnt worked out.
Then I thought, in Linux if a window is created, there is some way that we can be notified about this. Like some kind of GNOME or a X window Events. So initially I thought I would write some program in C to capture these global events and then for those windows I want, I would close them as I get notified. But then unlike getting this idea, implementing it is not that simple. Even now I have no clue if I ever want to write such a program, then what should I start.
Anyways I browsed for a long time and found out about this wonderful new tool called "wmctrl" which does exactly what I want. With
wmctrl I can get a list of open windows, their X Window ID, their title, their process IDs. And not only that we can manage a specific window using its ID or title and this includes opening, closing, resize, move, change title, etc. Isnt that great?
You can download it from "http://www.sweb.cz/tripie/utils/wmctrl/". Just install the tool on your machine using ./configure, make, make install and you are good to go.
Getting a list of open windows on LinuxIn the command line, type in
wmctrl -l to get a list of all open windows and their information. Example shown in the image.
The other options are also shown. You can close a window by passing its ID (
-i option) and a
-c option.
You can close a window by just passing the title along with
-c option.
But then the next problem is how am I going to execute this from a Java Program!
Executing Linux Commands from Java Program
You can run a Unix command from a Java Program, as shown below
try {
Process p = Runtime.getRuntime().exec("wmctrl -l -p");
int i = p.waitFor(); // very important. You wait for the command to be executed.
// i would be the return value of the process you are running.
if(i==0) //if successfully terminated
{
BufferedReader buff = new BufferedReader(new InputStreamReader(p.getInputStream()));
String temp;
while((temp=buff.readLine())!=null)
{
System.out.println(temp);
}
}
else //wmctrl returns 1 if no options passed
System.out.println("error : "+i);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The above program executes
wmctrl and displays the listing on the console. Isn't Java interesting to work and great?
I am just left with implementing this idea on my lab test machine and collect great results!!!!