Selenium: Waiting For AJAX

01 Jul 2009 | AJAX, PHP, Selenium, Testing, WorkXpress

Selenium RC I was playing around with Selenium RC a little while ago and ran into a little problem that I thought I would share with everyone. I was writing a test that bounced around WorkXpress to give me some performance benchmarks. My test added a new item that had a list layout of related items on it. I wanted to be able to follow a link to add items to that list.  Seems easy enough right?

The difficult part came when I needed to wait for the layout to be reloaded after adding the Items to it.  I knew that when the layout reloaded there was going to be a specific element on the page that was not there before. I tried using Testing_Selenium::waitForCondition() but that always returned right away and let the processing continue. After a little help from the documentation and Stack Overflow, I decided to give Testing_Selenium::getAttribute()

When I tried calling getAttribute() after waitForCondition(), I received a string back informing me that the element did not exist. This confirmed my suspicions that waitForConidtion() was coming back too early. I could have added a call to sleep() before continuing, but that would have defeated the purpose of my test.  Without a better solution, I resorted to a while loop that exited when the element finally existed. The code I used is included below:

$selenium = new Testing_Selenium($browser, $url, $host, $port, $timeout);

// Body of test.

 * waiForCondition() doesn't seem to be working for detecting if the content
 * has been loaded in the div, but this gets around it.
while (strpos($attr, 'not found') !== false) {
	$attr = $selenium->getAttribute('element_id@attribute');
} // end while

// Remainder of test.

Be very careful when attempting to do anything like this. If the element never appears on screen, you will find yourself with an infinite loop. Let me know what you think, or if you have a better solution.

comments powered by Disqus

Older · View Archive (34)

Who's Who Of Who's Who Scams

I received a letter in the mail today from Cambridge Who’s Who. The letter stated that I had been “appointed as a biographical candidate to represent Duncannon, PA in the 2009/2010 online Cambridge Who’s Who Registry among Executives and Professionals.” The letter goes on to say that I have been chosen based on my current position and that inclusion in the registry is free. In order to complete my listing, It stated I could fill out a form online or send in an application that was provided with the letter. Needless to say, I was very skeptical from the start.


Introducing the WorkXpress API

WorkXpress I recently finished documenting the WorkXpress API and thought I would share some of the details. First of all, the documentation can be downloaded at (PDF). For PHP, there is a PEAR package that makes working with the API easier. Look for that in a later post.