Friday, September 11, 2009

Selenium - Testing AJAX Web Applications

In AJAX applications, we do not reload the whole page, instead we update part of the page based on the response. To test these applications we cannot use wait_for_page_to_load() or wait_for_frame_to_load() or alert_is().

In these cases, we have to use the wait_for_condition() selenium API.
Key thing to observe here is that the code that you write in the condition is in javascript.
In the following example, I am waiting for the alert for 60000 milliseconds. This alert is poped up after updating the page based on the AJAX response.

my $cond = "try{ if(selenium.getAlert().indexOf('Package successfully deployed') > -1){true;} } catch(ex) {}";
$Common::sel->wait_for_condition($cond, 60000);

Wednesday, September 09, 2009

Selenium - alert and confirm in the page's onload() event

Selenium does NOT support _javascript_ alerts that are generated in a page's onload() event handler. Selenium does NOT support _javascript_ confirmations that are generated in a page's onload() event handler.

You may have some alert in the login page to display some alert on invalid username and password. When you want to automate the test cases on those pages, you need to add some javascript code on those pages before displaying the alert or confirm. These following lines makes the selenium to capture the alert or confirm messages.
var browserbot = parent.selenium.browserbot;
if (browserbot) {
browserbot.modifyWindowToRecordPopUpDialogs(window, browserbot);
alert("Alert message after those selenium workaround js snippet");

Sunday, September 06, 2009

Selenium - How to check the disabled button?

If you are using the Selenium IDE, you cannot record a test to check the disabled button. Instead of recording you can use the selenium perl driver API and code the test.
If I am testing the disabled state of a button with an id="pingBtn" in the HTML page, use the is_editable API and check the disabled state.
API doc:
Determines whether the specified input element is editable, ie hasn't been disabled.This method will fail if the specified element isn't an input element.

$locator is an element locator
Returns true if the input element is editable, false otherwise
Compare the return value to false (0) to check the disabled state and add a comment which will be printed on the test console.
ok($sel->is_editable("pingBtn") == 0, "is_not_editable, pingBtn");