Posted: December 15th, 2009 | Author: Anuradha Uduwage | Filed under: Java Ruled, Selenium | Tags: Java, Selenium | 4 Comments »
Lately I have been adding improvements to the framework that I have build using selenium rc, JUnit, Java, and Xpath. While adding more functions I wanted to add a method to handle ajax. The idea was to wait until all the ajax action complete in a page. Selenium api has a waitForCondition method. Method accept a script, and time out.
public void waitForCondition(java.lang.String script,
java.lang.String timeout)
Our application uses Prototype ajax library. so I used waitForCondition() from selenium api, and implement the following method.
public void waitForAjax() {
super.waitForCondition("selenium.browserbot.getCurrentWindow().Ajax.activeRequestCount == 0;",
SeleniumDefaultProperties.getResourceAsStream("default.pageload.timeout"));
}
I have added
.properties file to handle all the server settings and timings related to our test automation framework. In above method timing is invoked from that properties file.
Posted: August 12th, 2009 | Author: Anuradha Uduwage | Filed under: Java Ruled, Selenium | Tags: Java, Selenium | No Comments »
Dealing with threads in Java is not the easiest but I was in the process of implementing a custom Java Selenium Framework and I decided “Why not use the help of the ThreadLocal class to ease my pain”. So, this is my attempt to take stab at ThreadLocal class.
Why Did I need to use the ThreadLocal class?
In nutshell, The Selenium Framework I was building designed based upon pages of the web application. ex: Lets say we have login page and then once we login, use get navigated to about page. The framework I built have two separate Java classes to handle function in login page and about page. Since both these classes live from selenium session, I wanted to save the selenium session where each class can freely access the session without session getting passed around back and forth between those class.
Only thing you need to know about my implementation.
As I mentioned, I have created class that extends DefualtSelenium.java (lets call it OurDefaultSelenium.java, this class has method that start the selenium session)
So, just because I think I should use the ThreadLocal class does it mean is it the best mechanism to use..?
Most typical scenario of using ThreadLocal would be as an alternative to an object or resource pool, when we don’t mind creating one object per thread.
So When should I really use the Thread Local
The objects are non-trivial to construct;
An instance of the object is frequently needed by a given thread;
The application pools threads, such as in a typical server (if every time the thread-local is used it is from a new thread, then a new object will still be created on each call!);
Ok enough of lecturing, lets get to the bottom of it, shall we.
I am a big fan of Java generics so this example uses Java 5, Java generics.
Lets call our Class, SeleniumSession make sense right we are dealing with selenium session.
public class SeleniumSession {
private static final Logger log = Logger.getLogger(SeleniumSession.class.getName());
private static ThreadLocal<HashMap<String, OurDefaultSelenium>> session=
new ThreadLocal<HashMap<String, OurDefaultSelenium >>() {
protected synchronized HashMap<String, OurDefaultSelenium > initialvalue() {
return new HashMap<String, OurDefaultSelenium >();
}
};
private static ThreadLocal< OurDefaultSelenium > selenium = new ThreadLocal< OurDefaultSelenium>();
/**
* Get the current selenium session.
* @return
*/
public static OurDefaultSelenium get() {
return selenium.get();
}
Note that there is still a single, static instance of SeleniumSession shared by all threads. But that single instance uses the ThreadLocal variable session, which has a per-thread value. Inside get(), the call to selenium.get() will always operate on our thread-private “instance” of the variable, with synchronization.
Since we are using Java generics, subsequent get() method doesn’t need an explicit cast. (That is, the cast is inserted automatically by the compiler.)
But lets focus our eyes on to <strong>initialValue </strong>method. We actually subclass ThreadLocal and override initialValue() to provide an appropriate object each time a new one is required (i.e. when get() is called for the first time on a particular thread). I know what you thinking, You could add a logic to check the null inside the get() method. Like this,
OurDefaultSelenium ourSelenium = session.get();
if (ourSelenium == null) {
session.set(cal = new DefaultSelenium());
}
return ourSelenium;
But You don’t need to do this since we are overriding ThreadLocal.initialValue() automatically handles this logic and makes our code a bit neater– especially if we’re calling get() in multiple places.
In this implementation I also have methods to set the session and end the selenium session.
/**
* Set the current selenium session.
* @param classInstance
*/
public static void set(OurDefaultSelenium classInstance) {
selenium.set(classInstance);
}
/**
* Method to end the session, should use after the test.
*/
public static void endSession() {
selenium.get().stop();
}
Posted: July 2nd, 2009 | Author: Anuradha Uduwage | Filed under: Java Ruled, Selenium | Tags: Data Driven, Java, Selenium | 6 Comments »
Over the past year or two Selenium has taken over many developers hearts. Let me put it in this way, It won most of the Java developers hearts simply because if you are a java developer using Selenium now you can write a test without learning a new language nor using another stupid application developed by HP or any other 3rd party software company.
But some criticize saying that Selenium can not do Data Driven testing. I think its a bogus statement. Prior to this implementation I have used Selenium but I never had the oppertunitity to explore how I can used Dynamic data from csv file to execute in a selenium test.
In my development I decided to use csv file as the input but it doesn’t have to be csv file, I am more comfortable on dealing CSV Files with Java than any other file type so I picked CSV. My target is to create a Selenium Test that takes Username and Password from CSV file and login to web application. Its better you separate Selenium Test to one class and reading data from csv to another class. If you building as a framework I recommend to package is separate. Lets say we have a class call ReadInData.java and my file looks like this,

CSV File with Username & Password
lets write few methods to parse this data. Keep in mind that we need to use this data in the test and any method that does the manipulation should return an Array, a Map, or List with user name and password.I decided to use a HashMap implementation and return a HashMap with username and password.

Next thing is to create two methods, one is that take a single username and password and click the login button to login and other to extract username and password from HashMap and put them in to our previous method.
Use XPath to get respective text boxes and create a simple method as shown below, keep in mind that I am returning an instance of the class. Developers have mix feelings about the chain effect. But here I have used simply because I was planning on developing a framework and who ever use this framework to generate selenium test will have a very easy time on putting a test together with the help of the chain effect.
<a rel="attachment wp-att-304" href="http://anublog.colombounplug.com/?attachment_id=304"><img class="aligncenter size-full wp-image-304" title="MultiUser" src="http://anublog.colombounplug.com/wp-content/uploads/2009/07/MultiUser.jpg" alt="MultiUser" width="1114" height="398" /></a>
<a rel="attachment wp-att-305" href="http://anublog.colombounplug.com/?attachment_id=305"><img class="aligncenter size-full wp-image-305" title="SingleUser" src="http://anublog.colombounplug.com/wp-content/uploads/2009/07/SingleUser.jpg" alt="SingleUser" width="788" height="286" /></a>
Now lets talk about creating a Selenium Test using above class and its methods. You should extend the class that has above methods to create the Junit Base selenium test cases,

So, This clearly shows that Selenium Test can be data driven and its all in the person’s hand who is writing the test. Enjoy!
Recent Comments