Handle Ajax with Selenium RC

Posted: December 15th, 2009 | Author: | Filed under: Java Ruled, Selenium | Tags: , | 7 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.

1
2
public void waitForCondition(java.lang.String script,
                             java.lang.String timeout)

Our application uses Prototype ajax library. so I used

1
waitForCondition()

from selenium api, and implement the following method.

1
2
3
4
    public void waitForAjax() {
        super.waitForCondition("selenium.browserbot.getCurrentWindow().Ajax.activeRequestCount == 0;",
                SeleniumDefaultProperties.getResourceAsStream("default.pageload.timeout"));
    }

I have added

1
 .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.


Java File System Api getting a make over

Posted: November 15th, 2009 | Author: | Filed under: Java Ruled | Tags: | No Comments »

I was just reading upcoming changes in Java 7, and I was very excited. One thing I found out that java.io.File getting a make over.

Here are the things that going to be added to the Java 7 release,

  • FileRef – represents file object in system
  • Path – extends FileRef, binds a file to a system-dependent location
  • FileSystem – interface to file system FileStore – underlying storage system

I am planning to put a post once I find an example using the new api.


ZFS now has built-in deduplication, & ZFS available on Snow Leopard

Posted: November 4th, 2009 | Author: | Filed under: Java Ruled, My MacBook Pro, Technology | Tags: , | No Comments »

I was doing my regular daily doze of technical reading and stumble upon Jeff Bonwick’s blog. Sounds like deduplication is a great concept. I think this might be very helpful reducing the size of the repositories. But more than any thing I was thrilled about his previous post. ZFS is available on Snow Leopard. Wow!!!, as you may know from my previous posts, I switch to mac just about a month ago. And I must say somehow my mac book pro able fascinate me in daily basis. And this was another example.


Best use of ThreadLocal class to handle Selenium Session

Posted: August 12th, 2009 | Author: | Filed under: Java Ruled, Selenium | Tags: , | 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.
    1. Lets call our Class, SeleniumSession make sense right we are dealing with selenium session.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    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

    1
    <strong>initialValue </strong>

    method. We actually subclass ThreadLocal and override

    1
    initialValue()

    to provide an appropriate object each time a new one is required (i.e. when

    1
    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

    1
    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
    1
    ThreadLocal.initialValue()

    automatically handles this logic and makes our code a bit neater– especially if we’re calling

    1
    get()

    in multiple places.

    1. In this implementation I also have methods to set the session and end the selenium session.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
        /**
         * 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();
        }

    Data Driven Selenium Test

    Posted: July 2nd, 2009 | Author: | Filed under: Java Ruled, Selenium | Tags: , , | 7 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

    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.

    HashMap



    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,

    testMultiUser

    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!


    Pulling my hair out on FB Data Extraction

    Posted: February 24th, 2009 | Author: | Filed under: Research Work | Tags: , , | No Comments »

    I am trying it put together java base data extraction module for Facebook data. I am doing this to get large enough data set for my data mining project. I am thankful for all the developers who are trying to keep up the java-facebook-api upto date, but I had enormous amount of issues with facebook-java-api. Including my other research work this is the latest enemy of sleep. So, to give you an idea, for the last four days my total number of sleep hours are in single digit.


    GWT complains on @override

    Posted: January 12th, 2009 | Author: | Filed under: Java Ruled | Tags: | No Comments »

    I recently update my java version to build 11 and first thing I found out that GWT SDK doesn’t have the latest addition from java build to reflect @override.

    If you are like me who use eclipse, when you create a method like this.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class Ex1 implements EntryPoint {

    @override

        public void onModuleLoad() {

            GWT.log("Out Put", null);

            final Label myLabel = new Label("Hello World");

            RootPanel.get().add(myLabel);

        }

    }

    Eclipse will automatically will add @override which help the compiler to understand the behavior of the method. This is a addition in java 1.6 build 11, But GWT SDK doesn’t have this update so your compiler will cough on you.


    Get Adobe Flash playerPlugin by wpburn.com wordpress themes