Friday, April 1, 2011

Selenium Automated Test: Close native window "Security Alert"

Facing this "Security Alert" box in the selenium test when try to access a https site.


No admin right so no way to change the security setting. Seems no way to work around this.

Search the selenium board, found that this is Windows native box, therefore out of Selenium's power.

Looking around find this site using python seems solved this issue.

http://datadebrief.blogspot.com/2010/09/automatically-click-yes-on-internet.html

However, no pure Java solution.

Decided to give it a shot myself. Eventually comes out the following code that successfully closed this "Security Alert" box.

Used library: JNative.jar


public class RunSeleniumNativeHelper {
/**
*
* @param windowTitle
* @return Integer in String represents window handler for the window.
*/
public static String findNativeWindow(String windowTitle) {
String hwnd = "0";
int i=0;

try {
JNative FindWindow = new JNative("User32.dll", "FindWindowA");
while ("0".equals(hwnd) && i++<2) {
FindWindow.setRetVal(Type.INT);
FindWindow.setParameter(0, Type.INT, "0");
FindWindow.setParameter(1, Type.STRING, windowTitle);
FindWindow.invoke();

hwnd = FindWindow.getRetVal();
System.out.println("win:" + i + " " + hwnd);
}
} catch (NativeException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

if ("0".equals(hwnd))
throw new NoSuchElementException("No window found with a title of \"" +
windowTitle + "\"");
else
return hwnd;
}

/**
*
* @param parentWinowHandler
* @param buttonLabel
* @return Integer in String represents window handler for the button.
*/
public static String findButtonInWindow(String parentWinowHandler,
String buttonLabel) {
String hb = "0";
int i=0;

try {
JNative FindButton = new JNative("User32.dll", "FindWindowExA");
while ("0".equals(hb) && i++<2) {
FindButton.setRetVal(Type.INT);
FindButton.setParameter(0, Type.INT, parentWinowHandler);
FindButton.setParameter(1, Type.INT, "0");
FindButton.setParameter(2, Type.STRING, "Button");
FindButton.setParameter(3, Type.STRING, "&" + buttonLabel);
FindButton.invoke();

hb = FindButton.getRetVal();
System.out.println("button:" + i + " " + hb);
}
} catch (NativeException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

if ("0".equals(hb))
throw new NoSuchElementException("No button found with a label of \"" +
buttonLabel + "\" in window with a title of \"" +
parentWinowHandler +"\"");
else
return hb;
}

/**
*
* @param buttonHandler
*/
public static void clickButton(String buttonHandler) {
try {
int i=0;
while (!"0".equals(buttonHandler) && i++<2) {
HWND hbHandler = new HWND(Integer.parseInt(buttonHandler));
UINT unit = new UINT(0x201); //native code for mouse event: left button down
WPARAM p1 = new WPARAM(0);
LPARAM p2 = new LPARAM(0);
User32.SendMessage(hbHandler, unit, p1, p2);

unit = new UINT(0x202); //native code for mouse event: left button up
p1 = new WPARAM(0);
p2 = new LPARAM(0);
User32.SendMessage(hbHandler, unit, p1, p2);

System.out.println("click button:" + i + " " + buttonHandler);
}
} catch (NativeException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

We integrated Selenium with Fitnesse so the above code was called in the fixture. Therefore, not like the python sample to have it running as a demon process, but rather one step in the Fitness Script.

In our practice we have to pause before and after this "Security Alert" box to make sure it does showeup before we run the closing code and give it enough time to accept the "Yes" button click.

Below is the sample to run the above code to close the box.

//wait enough second so the security box shows
String winHandler = RunSeleniumNativeHelper.findNativeWindow("Security Alert");
String yesButton = RunSeleniumNativeHelper.findButtonInWindow(winHandler, "Yes");
RunSeleniumNativeHelper.clickButton(yesButton);
//wait enough time so the security box closes and the process completes.

1 Comments:

At December 10, 2012 at 10:02 AM , Blogger Unknown said...

This comment has been removed by a blog administrator.

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home