Thursday, April 8, 2010

Firefox 3.6.3 and Selenium issue with FirefoxChromeLauncher$FileLockRemainedException: Lock file still present!

As part of development, we tend to use and write functional test cases using Selenium. The Selenium IDE integration with Firefox is seamless and it's very easy to record the user actions and just get the functional test case code out of it. With the latest version of Firefox 3.6.3, I've faced issue of "parent.lock" file on my Windows box and "parentlock" file on linux box with error
org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher$FileLockRemainedException: Lock file still present!

The internet search was not proving to be useful and the Selenium Test cases are refusing to launch the Firefox browser because of this parentlock files. These files are getting created under the custom profile being prepared by firefox for each browser session and remains in that folder, if firefox is not properly closed. Also, on windows, I've found problem of Firefox process still running even if browser is closed and not getting closed automatically. You've to manually go to the Task Manager and kill the process of Firefox.

So, overall the situation was not good for the Selenium Test cases to run. The log was clearly indicating issue with Custom Profile and server jar. So, there must be some issue with it. If it'd have run on Linux and not on Windows, it'd have been easy for me to crib about Microsoft, but that was not the case here.

I had Selenium Server 1.0.1 jar, so I decided to digg into this jar.

Explode the jar in a location and digg through it, you'll find the restriction placed in this jar as...





{ec8030f7-c20a-464f-9b0e-13a3a9e97384}
1.4.1
3.5.*




Ahhh.... that seems to be the problem. I've gone ahead and edited "install.rdf" files under selenium-server-1.0.1-standalone.jar\customProfileDirCUSTFFCHROME to support max version as 3.6.*. The updated install.rdf file have contents as below...





{ec8030f7-c20a-464f-9b0e-13a3a9e97384}
1.4.1
3.6.*





Then just re-build the jar file and then just use the newly build jar file.

And whoilla !!! it seems to have worked smoothly on both Linux and Windows.

I'm sure this will be fixed in next releases of Selenium Server, but as of now, who are facing the issues with it, they can solve it by above method.

Friday, January 29, 2010

Java Large File Transfer using NIO

Most of the applications require large data transfer from one location to other. If we use normal Java IO, it will take a lot of time and resources. Also, we may end up not utilizing the available resources properly.

In one of my current development, I came across such issue and we also require non blockable I/O. So, going through the documentation available by Sun (now Oracle), I've written a simple class and tested it's performance on a local desktop machine.

The basic version of the File Transfer code is given here. I can't paste the code that we're using, because of IP and blah blah blah !!!



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;


/**
* This class is used for Large File Transfer.
*
* @author swapnil
*
*/
public class LargeFileTransfer
{
/**
* @param args
* @throws IOException
*/
public static void main (String [] args) throws IOException
{
File inFile = new File("c:/test/src/src.rar");
File outFile = new File("c:/test/dest/dest.rar");
copyFile (inFile, outFile);
}

/**
* This method is used to copy the Large file from source specified by in to the
* destination specified by out.
*
* @param in
* @param out
* @throws IOException
*/
public static void copyFile (File in, File out) throws IOException
{
FileChannel inChannel = new FileInputStream(in).getChannel ();
FileChannel outChannel = new FileOutputStream(out).getChannel ();

try
{
// For Windows, 64Mb is limit
int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = inChannel.size ();
long position = 0;

//copy the File, buffer by buffer
while (position < size)
{
position += inChannel.transferTo (position, maxCount, outChannel);
}
}
catch (IOException e)
{
throw e;
}
finally
{
if (inChannel != null)
{
//close the Input Stream
inChannel.close ();
}

if (outChannel != null)
{
//close the Output Stream
outChannel.close ();
}
}
}
}





Whoilla!!! It's too simple and having really good performance. Check it out yourself. I'm trying to fine tune it for Linux....

Wednesday, January 27, 2010

Spring Testing with multiple data sources

Spring Testing with multiple data sources and errors:

We all are required to use multiple data sources connecting to different databases as part of our development. So, we tend to use different dataSource and declare them in our applicationContext.xml a.k.a. Spring Application Context configuration file.

Then, we also use Spring testing framework to test the functionality that we write. In most of the cases after Spring 2.5, we leverage upon use of AbstractAnnotationAwareTransactionalTests classes.

Error:

But, if we have defined multiple data sources / multiple persistance units in our application, these test cases fails. To be specific, the loading of application context fails, since it tries to find only one bean with type DataSource, but found multiple.

Unsatisfied dependency expressed through bean property 'dataSource': No unique bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: [dataSource,myCustomDataSource]; ...

Solution:

To solve such kind of issues, there is not clear documentation provided and one has to do lots of search over the internet. I've spent almost 2-3 hours today to solve such issue and the solution turns out to be pretty simple !!!

You just need to override some of the methods as shown below.


import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;

/**
* Base Test class for Services. It will be extended and used by all the Service Test
* classes.
*
* @author swapnil
*
*/
public class BaseServiceTest extends AbstractAnnotationAwareTransactionalTests
{
protected String [] getConfigLocations ()
{
return new String [] { "classpath:/myApplicationContext.xml" };
}

public void setDataSource (@Qualifier("myCustomDataSource")
DataSource dataSource)
{
super.setDataSource (dataSource);
}

public void setTransactionManager (@Qualifier("myCustomTransactionManager")
PlatformTransactionManager transactionManager)
{
super.setTransactionManager (transactionManager);
}

Sometimes, with lots of things cropping up in your brain, you try out various complex things, rather than think simple and try simple. Just an IDE checking override would have done ;)

Tuesday, January 12, 2010

Java Web UI Functional Testing with Selenium

All of us required to write Functional test cases for the Web UI. As Java developers, we are not used to write Web UI automated test cases and it is really a cumbersome job to write those test cases. It requires a lot of learning curve and lot of experience to write really good UI test cases, that are automated.
But wait, here comes the simple and free to use rescue. The Selenium. It comes with an easy to use and interactive IDE, that we can leverage to record our Web UI workflow and it will generate Java Test Cases for us. Moreover, it has support for not only Java, but different technologies like C#, Perl, PHP, Ruby, Groovy etc.

Selenium IDE is a great tool to quickly write the automated test cases for web UI. for Selenium tests.It can be downloaded and installed as a Firefox extension, and it allows you to record, edit, and debug tests. More information is at Selenium Site

Simply download the Selenium IDE to your firefox. Then start the application server, where you are running your web app and open web UI in firefox.
  1. Go to Tools -> Selenium IDE and it will open up interactive IDE.
  2. You can click on record icon and start recording your web actions.
  3. Once it is over, go to IDE and stop recording and see your actions got recorded.
  4. You can edit the code using wonderful facilities of Selenium IDE using Xpath and other simple syntax.
  5. Run the test through Selenium and see the test results.

You can also get the Java code of the Selenium recording.
In Selenium IDE, go to Options -> Format -> Java. And you will get the Java Junit / TestNG
code for your WebUI functional testing.

Ajax and Selenium Issues:
Now, if you're using Ajax based Web UI, it may happen that Selenium tries to test the component before Ajax request actually getting processed.
  • So, if you are running from Selenium IDE, slow down the speed of test.
And in Java code, just add one line as per the requirement.


//This is important step to set the speed as per test case
//It requires the Selenium to wait for Ajax components to load and available
//If you're setting low speed, you may encounter element not found, because
//Selenium will try to access components, before they are loaded.
selenium.setSpeed("1000");


Windows Vista and Selenium Issues:
In Windows Vista, due to User Account Control, when you run from normal command prompt, you may encounter Firefox crash or firefox chrome registration issues. In order to solve these issues, just run command prompt as an administrator and run your Selenium test cases through it.

Selenium and Maven Integration:
Selenium has very good support with Maven Integration. Just do some google search and you'd get lots of references about how to integrate and run through.
A simple and good example is present at: Selenium-Maven Integration