Tag Archive for 'web testing'

How to wait until AJAX call completes in Selenium web tests

If you introduce AJAX to the application that is web tested with Selenium your tests might fail from time to time. In my case the tests were failing randomly, not that often but it made their quality decreased. So, how can you fix this?


Ideally, what you need is to stop the test execution (e.g. do not perform checks for a little bit) until the AJAX call completes. Typical scenario is there’s AJAX activity indicator – a text or an animated image that is displayed when the request is sent and hidden when response from the server is obtained. In that case, you need to wait until the indicator disappears.

In order to do this, WebDriverEventListener inteface can be implemented. This will enable to listen and react to several popular web test events, one of which is a click.

Below is the example (in Java) of the implementation of the method that is fired when the web test code clicks HTML element. BY_LOCATOR is a placeholder of code that should somehow locate the AJAX indicator (e.g. by ID, class name). What happens is documented in the code.

public class AjaxActivityIndicatorEventListener implements WebDriverEventListener {
	public void afterClickOn(WebElement element, WebDriver driver) {
		try {
			// Waiting for AJAX indicator to disappear...
			new WebDriverWait(driver, 10).until(invisibilityOfElementLocated(BY_LOCATOR));
		} catch (StaleElementReferenceException e) {
			// Clicking element must have not called AJAX request. Skipping.

Now that you have the event listener, it needs to be registered for the web driver. In order to achieve this, the web driver instance that is prepared for the tests should be created as below:

new EventFiringWebDriver(driver).register(new AjaxActivityIndicatorEventListener());

Driver here is the (configured) instance of a web driver (e.g. HtmlUnitDriver, ChromeDriver, InternetExplorerDriver).

LoadTest: MethodAccessException Microsoft.VisualStudio.TestTools.WebTesting.WebTest.set_Outcome

I had a load test that used a coded web test. At some point the coded web test changed, i.e. this line was added: Outcome = Outcome.Fail;.

After this change the test stopped working – it always ended with error message: ‘User aborted test run’. Apart from that each iteration of the web test produced MethodAccessException: Microsoft.VisualStudio.TestTools.WebTesting.WebTest.set_Outcome(Microsoft.VisualStudio.TestTools.WebTesting.Outcome).


The reason for the problem was I was using Visual Studio TS 2008 without SP1. Once I installed the SP1, which must have updated mstest, the test started running successfully again.

The key point here is before SP1 Outcome was read-only property, which I learned there.

How to invoke a common coded web test method from GetRequestEnumerator()?

It’s a fact that coded web test methods give more flexibility to the developer, i.e. common code reuse. So let’s create a coded web test in whose GetRequestEnumerator() method you want to call a common method which tests some other requests. Let’s make it look as GetCommonRequests() in the example below:

public class AWebTest : WebTest
    private IEnumerator<WebTestRequest> GetCommonRequests()
        WebTestRequest req1 = new WebTestRequest("http://google.com");
        yield return req1;

        WebTestRequest req2 = new WebTestRequest("http://google.com");
        yield return req2;

    public override IEnumerator<WebTestRequest> GetRequestEnumerator()
        WebTestRequest req = new WebTestRequest("http://google.com");
        yield return req;


You would expect to see three requests in the test result. You will see only one though…

Continue reading ‘How to invoke a common coded web test method from GetRequestEnumerator()?’