Currently Browsing

Author Archive

Capturing screenshots of failed tests in Selenium Webdriver + JUnit4

Beside the exception / failure information, it is almost always helpful for the developers to provide them with additional screenshot of the failed tests.
From JUnit 4.7 the @Rule annotation is introduced which can be used to extend the capabilities of JUnit. With it we can invoke a custom piece of code when a test fails or succeeds.
In this example we will capture a screenshot of a failed test.
Before showing how it is done, we should have a test class at hand. For the following example just download the selenium driver from the page and add the jar files to classpath.

package test;

import static org.junit.Assert.assertTrue;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.thoughtworks.selenium.Selenium;

//This is an example for the usage of ScreenshotTestRule
public class TestWithScreenshotExample {
    static WebDriver driver;
    static Selenium selenium;
    // an annotation is used for defining a new rule for Junit
    public ScreenshotTestRule screenshotTestRule = new ScreenshotTestRule();

    public static void beforeClass() {
        driver = new FirefoxDriver();
        selenium = new WebDriverBackedSelenium(driver, "");

    public static void afterClass() {

    public void testThatSucceeds() {"/");
        // some text code here
        assertTrue(selenium.isTextPresent("This test will pass"));

    public void testThatFails() {"/");
        //some text code here
        assertTrue(selenium.isTextPresent("This test will fail"));

    // the static driver object will be needed for capturing screenshots
	public static WebDriver getDriver() {
		return driver;

The trick above is to define a new JUnit rule with @Rule annotation.
The class which is used to define the new rule needs to implement the MethodRule interface and to override the apply() function.

Solution for – Could not start Selenium Session : Connection timed out

If you get the “Could not start Selenium Session : Connection timed out” while running a test scenario it is usually due to the ports not matching for the Server and the client and results in “Selenium server not able to make a connection with the local client machine”.

a sample error message:

 java.lang.RuntimeException: Could not start Selenium session: Connection timed out: connect
at com.thoughtworks.selenium.DefaultSelenium.start(
at junit.framework.TestCase.runBare(
at com.thoughtworks.selenium.SeleneseTestCase.runBare( 2)
at junit.framework.TestResult$1.protect( 

The solution for it is to make sure to run the on the same ports. The default port where the selenium server starts up is 4444.

How To Make Android Screenshots for testing

When testing Android mobile applications the one of the first things that come up is how to record and document the error that happened.
As in desktop testing there are apps that allow you to record the screen. The only thing that you need to do is to make sure the phone
is rooted before using the screenshot applications. (there are several descriptions out there just Google for them for the specific android
version and phone model).

Here is a list of screenshot applications:

Screenshot V1.4 from (I Suggest this one)

  • You can use the camera button to take screenshots
  • You can also choose to shake the phone to take pictures when you want

Screenshot It Trial from (only 5 screenshots in the trial mode)

  • Hold down the camera button
  • Assign a hot key to the application
  • Use a delayed timer

Solution for Selenium IDE – Your browser doesnt support Xml Http Request

If you get the “Your browser doesnt support Xml Http Request” error while running tests
in Selenium IDE even if its shows that all tests passed below is the cure for it:

This issue only happens on versions of Selenium IDE prior to 1.0.6

So there are two workarounds to this problem:

1) Get the newest version and use that (currently 1.0.7) :

2) Or if for whatever reason you are stuck with the version you have here is a workaround:


Selenium test in Java with Eclipse and JUnit

Its quite easy to run Selenium tests using Eclipse and JUnit. Basically all you need is to get Selenium RC:

and get Eclipse (Eclipse Classic will do if you don’t need the rest, the Helios 3.6 already includes JUnit)

to make things easier also get Selenium IDE (Firefox plugin for recoding tests and exporting to the desired language)

Install Selenium IDE the rest just unpack to a desired dir and you are ready to roll. If you already have Eclipse its most likely

that you have JUnit as well but just in case you need it it can be found here:

Record some test case in Selenium IDE by opening up FireFox and going to Tools->Selenium IDE

For example open up and assert some text (or do whatever test you desire)

While Selenium IDE is running you can easily assert things by right clicking (except on web pages that

have their own right click handling) and looking at the bottom commands on the popup all those come from

Solution for Selenium already running on port problem

When running selenium server often it happens that it can’t start with the following error:

“Selenium is already running on port 4444. Or some other service is.”

This happens because Selenium uses the 4444 port as its default and if you already started selenium or some other application is using that port Selenium will be prevented from using the same port.

There are several ways around this problem:

  • Find the application that is blocking/using the 4444 port and turn it off or change its port. Using the command “netstat -ano” (under Linux use “netstat –tcp –listening –programs”)¬†from command prompt identify the applications PID and then look it up in task manager. If you don’t see the PID in task manager make sure to turn the column on in View->Select Columns:
    Once you have identified the application either kill it or change its port. Now its safe to run “java -jar selenium-server.jar” and Selenium will start up correctly.
  • Change the port Selenium is using. Run “java -jar selenium-server.jar -port XXXX” where XXXX is the desired unused port for example “java -jar selenium-server.jar -port 4448”.
    This fix is also useful if you want several instances of the selenium server running on the same machine.


Solution for Selenium IDE Export to C# problem

When using the Selenium IDE to generate C# tests users get the following error “Suite export not implemented for the cs-rc formatter”:

This happens because there is an error in the code that can’t convert from the default HTML Format to C#. The workaround is to go to Options -> Format and then select C# – Selenium RC. Once converted you don’t need to export anything you can just save it or copy paste it to your C# test.

With this workaround you can also export existing saved selenium test cases from HTML or any of the others to C#. Just open up the saved test case and then switch to C# in Options -> Format