Using Selenium in a Ajax web application

On an Ajax web application or Ajax feature on web page small sections of the page will update and often the entire webpage will not load. It’s necessary to wait until this ajax process is completed before interacting with sections of the page where content or controls are yet to be rendered.

The Ajax application architecture

If you are using Selenium a simple sleep or wait is a bad idea. The Ajax calls do not the same time to perform an operation every time. If the wait period is too short, you’ll have random test failures since a control you want to interact with is missing. If the period is too long, your tests take too long since you end up waiting, very often, far longer than you need to.

The best approach is to use the selenium waitForElementPresent(locator) command.
for (int second = 0;; second++)
if (second >= 60) Assert.Fail("timeout");
if (selenium.IsElementPresent("//div[@id='locator']")) break;
catch (Exception)

The Selenium Command waitForElementPresent waits till a particular element shows up on the page. The other option is waitForCondition(locator) command, which takes a javascript function and waits till it evaluates as true. See here.
selenium.WaitForCondition(“//div[@id=’locator’]”, “”);

You can also use the selenium.browserbot.getCurrentWindow() selenium.browserbot.getUserWindow().$.active == 0. The browser tells us how many active connections to the server are present. Then we sit and wait for that number to become 0. At this point we know that all Ajax requests went through and done, and we can move on in the test.

Here is the code in C# using the selenium.browserbot.getUserWindow().$.active == 0 command with selenium.WaitForCondition command.

selenium.WaitForCondition("selenium.browserbot.getCurrentWindow() == 0", "5000");
selenium.WaitForCondition("selenium.browserbot.getCurrentWindow().$.active == 0", "5000");

I have also used the selenium.WaitForCondition(“selenium.browserbot.getCurrentWindow() == 0”, “15000”); but the issue is that I keep getting timeout issue. I am yet to resolve the issue. I will update once I get the issue resolved.

If you found this solution helpful or have something extra to add, feel free to share it here by commenting below.

Selenium RC Coding using C Sharp #region

When you are writing a piece of selenium test script you can use the #region a feature of the Visual Studio Code Editor. This will let you specify a block of code that you can expand or collapse. Here is an example that shows the use of #region in the test method Typeall
#region Typeall
public void Typeall(string[] path, string[] value)
int len = path.Length;
for (int i = 0; i <= (len - 1); i++) { if (selenium.IsElementPresent(path[i])) { selenium.Type(path[i], value[i]); } } } #endregion

Using Array to execute Selenium commands in C#

Here is an example of how you could you an array to execute repetitive selenium commands like selenium.IsElementPresent or selenium.Click.

string[] footerLinks = new string[] { link1, link2, link3, link4, link5, link6, link7, link8, link9};

foreach (string link in footerLinks)

Submitted by: Horgan Barry