# Selenium error in NUnit C#

I got the following error when I try to run this test:

TheNewTest : FailedSelenium.SeleniumException : Timed out after 5000ms at Selenium.HttpCommandProcessor.DoCommand(String command, String[] args) in c:\Projects\WebDriver\trunk\dotnet\src\Selenium.Core\HttpCommandProcessor.cs: line 100 at ClassLibrary1.NewTest.TheNewTest() in Class1.cs: line 66

Here is the code:

using System;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Selenium;

namespace ClassLibrary1
{
[TestFixture]

public class NewTest
{
private ISelenium selenium;

private StringBuilder verificationErrors;

[SetUp]

public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*firefox D:\\Program Files\\Mozilla Firefox\\firefox.exe",

selenium.Start();

verificationErrors = new StringBuilder();
}

[TearDown]

public void TeardownTest()
{
try
{
selenium.Stop();
}

catch (Exception)
{
// Ignore errors if unable to close the browser
}

Assert.AreEqual("", verificationErrors.ToString());
}
[Test]

public void TheNewTest()
{
// Open Google search engine.

// Assert Title of page.

// Provide search term as "Selenium OpenQA"
selenium.Type("q", "Selenium OpenQA");

// Read the keyed search term and assert it.
Assert.AreEqual("Selenium OpenQA", selenium.GetValue("q"));

// Click on Search button.
selenium.Click("btnG");

// Wait for page to load.

// Assert that "www.openqa.org" is available in search results.
Assert.IsTrue(selenium.IsTextPresent("openqa.org"));

// Assert that page title is - "Selenium OpenQA - Google Search"
Assert.AreEqual("Selenium OpenQA - Google Search",
selenium.GetTitle());
}
}
}


I'm suspecting something wrong with the line selenium.WaitForPageToLoad("5000"); but I don't know how to fix it.

You can also download my code here: http://dl.dropbox.com/u/20422001/ClassLibrary1.zip.

Edit 1

I have to MANUALLY refresh the browser in order to get pass the line selenium.WaitForPageToLoad(timeout);. How can I fix this problem?

Edit 2

Things start to get more and more interesting!

I changed my code to search in Altavista search website (www.altavista.com) and it WORKS! Somehow I don't know why, it seems "selenium.WaitForPageToLoad(timeout);" doesn't work for Google search website, but it works for Altavista search website Is it just me? I'm still curious and want to make it work for google website as well, plz help.

Edit 3: August 10th 2011

I am now one step before the final solution of this problem. Thanks "stuartf" for pointing out that the root cause of this problem is because of Google using Ajax call for search results.

Right now, I have a "cheap solution" for this problem, here is what I did to "detect" when the ajax page is loaded:

while (seconds < 30)
{
//Check whether the results page is loaded.
if (selenium.GetTitle() == "Selenium OpenQA - Google Search")
{
break;
}
seconds++;
}
Assert.IsTrue(selenium.IsTextPresent("openqa.org"));


Yes it works by checking the search results page title, BUT It's still not too reliable in my opinion. Is there a better way to check whether the ajax page is loaded? Something like:

if (selenium.isElementPresent("link=ajaxLink"))


The problem is, how do we find "ajaxLink"?

-

Make it

selenium.WaitForPageToLoad("60000");


Or better, make timeout a variable and use it, so you don't have to hard code it all around:

String timeout = "60000";

-
I tried your suggestion but I still got the same error message. I need to MANUALLY refresh the page to get pass "selenium.WaitForPageToLoad("60000")" in order to be successful. –  victorg Aug 7 '11 at 12:10

The WaitForPageToLoad method will not work here since Google now return results in an AJAX call when you start typing. The best practice is to wait for something knowing on the page to exist before you proceed.

For example you could poll the page to check if selenium.IsTextPresent("openqa.org") is true, if it's not loop back around and try again. Just remember to timeout after a specified length of time.

Another idea would be to put a Threading.Sleep(5000) in place of the WaitForPageToLoad method, this will cause the test to pause for 5 seconds. This isn't really good practice as you test will not take a minimum of 5 seconds to run when in reality it the test could have finished much quicker than this.

I hope this helps!

-
Great, your method work. However I need a better/more reliable solution. For example, to be able to "detect" the new ajax page. e.g: check if (IsElementPresent("link=ajaxLink")) is true, if FALSE, then loop back around and try again. if TRUE, we know that the new ajax page is loaded, then we break the loop and then we can assertTrue(selenium.IsTextPresent("openqa.org)". BUT the PROBLEM is how do we know the "ajaxLink" in the first place? Please refer to my edited post (edit #3). –  victorg Aug 10 '11 at 21:57