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?

Solution

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 {
	@Override
	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).

Request to JetBrains Santa

Dear Santa,

It’s getting closer and closer. Christmas is comming!

I’m proud to say I was kind all 2012, both at home and at work. I’m even happier because, when working with code, I had to use IDEs other than Idea, which according to my colleagues is awesome… I made it nevertheless! Knowing the power of TeamCity and Resharper, I can only guess that my friends know what they are recommending icon smile Request to JetBrains Santa That makes me believe it would be perfect gift under my Christmas tree!

So, Santa, I hope you are reading this letter and agree it would be great to provide me with the license. I promise, I’d be even better next year!

Jarosław Dobrzański

There’s always one f#$%!@ thing you haven’t done…

Let’s relax and spend 7 minutes on watching Eddie Izzard’s Encore on Computers.

How many times you said so after spending hours on chasing a bug? There’s always one f#$%!@ thing you haven’t done…

Interesting introduction to C++11

Wanna check what’s the whole buzz about C++11? Are you interested in what has changed comparing to the previous standards?

Herb Sutter wrote a great abstract with list of features of C++11 that give impression it’s a new language. In the article, there’s a link to the other useful resource, C++11 – the recently approved new ISO C++ standard (by Bjarne Stroustrup).

Norvegian Developers Conference 2011 – videos

ndc2011 logo Norvegian Developers Conference 2011   videosOk, I haven’t attended either of editions of Norvegian Developers Conference, but I stumbled upon a link to one of the videos from that conference somewhere at Software Testing & Quality Assurance Videos& Tutorials.

I watched one session and followed it by the other one. I’ve only seen two of last year’s session for now but I don’t regret and will definitely come back and watch more. Having looked at the speakers and agenda, it seems it was a decent conference. If you have a moment, visit the the agenda of NDS2011, with links to videos, and look at the sessions. Probably, there are a few you might be interested in. Watch it/them and be happy to safe some amount icon smile Norvegian Developers Conference 2011   videos

2011 in review

It’s end of January 2012 by now, but I decided to share some stats on what was going on with this blog in 2011. If you fancy reading my stuff (is there anybody? icon smile 2011 in review ) you might find it interesting.

Let’s get down to the business:

  • only 3 posts that year icon neutral 2011 in review busy? lazy? …?
  • nevertheless, I was observing constant interest growth (more and more vistst, by Google Analytics):
    • 196,717 Visits (+45% comparing to 2010)
    • 178,770 Unique Visitors (+46% comparing to 2010)
    • 222,258 Pageviews (+42% comparing to 2010)
  • most popular post: C#, decimal.toString(), and how to get rid of trailing zeros – 17,302 pageviews

Visual Studio TFS: How to undelete file(s)

What if you need to undelete a file or bunch of files that at some point have been deleted from TFS? Naive option would be to get the content of the file, copy it, create a new file, and paste the copied content to just created file. Of course, this is not a good option because history of changes to the file(s) will be lost.

Continue reading ‘Visual Studio TFS: How to undelete file(s)’

C#: How to get service name listening at specific port number?

How to get service name listening at specific port in C#? What you have as input is only two pieces of information: host name and the port number the service is listening at.

Solution

Apparently, .NET does not provide such feature so one needs to stretch a bit to get the answer. What I can suggest (I’m far from saying it’s good approach, though) is to get the name in two steps:

  1. Use netstat -a -o and parse the output (ouch!) to get ID of the process (PID) that is listening at given port number
  2. Perform a WMI call to get the name of the service: SELECT Name FROM Win32_Service where ProcessId = PID

Following this will give you what you want, but to be honest any time I need to parse output to get some information I feel anxious… This is the first place in the code where errors can be introduced.

If there is/are better/safer way(s) to retrieve service name having the host name and port it’s listening at, please share it.

Visual Studio: Improved navigation through the files with RockScroll

If you have never used RockScroll you are probably most comfortable with standard scrollbar Visual Studio offers. I guarantee you, however, that the moment you install RockScroll and work with it for a while, you will miss it a lot if you switch to Visual Studio that’s not extended with it. I’ve experienced that many times when kneeled at a teammate’s desk trying to help him move on with their task. This is probably best moment when you will realize that Visual Studio misses a thing without RockScroll icon smile Visual Studio: Improved navigation through the files with RockScroll

Here are most important pros that make me think RockScroll is must-have plugin for Visual Studio:

Continue reading ‘Visual Studio: Improved navigation through the files with RockScroll’

C++: Overriding methods – problem with hiding overloads in the base class

What is the outcome of the simple program below?

#include <iostream>
#include <conio.h>

using namespace std;

class Base 
{
public:
   void DoSth(int tmp) 
   {
	   cout < < "Base::DoSth(int)\n";
   }

   void DoSth(char tmp) 
   {
	   cout << "Base::DoSth(char)\n";
   }
};

class Derived : public Base
{
public:
   void DoSth(int tmp) 
   {
	   cout << "Derived:DoSth(int)\n";
   }
};

int main()
{
	Derived d;
	d.DoSth(5);
	d.DoSth('c');
   
	getch();
    return 0;
}

Continue reading 'C++: Overriding methods – problem with hiding overloads in the base class'