Sunday, December 27, 2009

Coping with Issues using Selenium

Have been trying to work with some real Selenium projects using Java as the programming language. It is interesting to work on this and have been trying to come up to terms with a  new language for use with Selenium. The main was the use of Eclipse as a IDE to build the test suite has been a great learning experience.

So the basic thing to do is to record the actions of the web application using the Selenium IDE, then convert the test case into Java/JUnit test case. Copy the code from the Selenium IDE window to the Eclipse IDE. Make some changes so that the code looks beautiful, and Eclipse gives some great pointers to correct your mistakes. And Voila! you have a test case written in Java and ready to be run with/as a JUnit component :-)

A generalized code is given below:

/**
 * @author: gagneet
 *
 */

package com.cookie.selenium; //this can be made on your own as a package into which you place all your code. Java requirement.


// some library functions that I am making use in my code.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

// The Selenium packages from ThoughtWorks
import com.thoughtworks.selenium.*;

// The test case class. The name of the class has to be the same as the file name with .java
// You do need to extend the SeleneseTestCase base class.
public class ReadURL extends SeleneseTestCase {

    /**
     * To open the first instance of the browser and initialize the same.
     */
    public void setUp() throws Exception {
        setUp("http://www.yahoo.com/", "*chrome");
    }
   
    /**
     * @param args
     */   
    public static void main(String args[]) {
       
        Selenium selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://ad1318.rm.sp1.yahoo.com");
        selenium.start();
        selenium.setTimeout("30000000");
       
        try {
            BufferedReader inputfile = new BufferedReader(new FileReader("C:\\out.txt"));
            BufferedWriter cookiefile = new BufferedWriter(new FileWriter("C:\\cookie.txt"));
           
            String str;
            // I am trying to read a file which has multiple lines, one line at a time.
            while ((str = inputfile.readLine()) != null) {
                selenium.createCookie("ABCdefGHI",""); // creating a cookie which will be sent along with the browser open
                selenium.open( str );
                selenium.waitForPageToLoad("120000");
                String urlcookie = selenium.getCookie();
                System.out.println( "URL  :" + str );
                System.out.println( "Cookie  :" + urlcookie );
                cookiefile.write( urlcookie );
                cookiefile.newLine();
            }
            inputfile.close();
            selenium.stop();
        } catch (IOException e) {
        }

    }
}

In the above code, I am trying to read a file, which has multiple URL's. I read each line and then open the same in a browser window. Once it opens, I store the cookie value for that page into a file and also display it in the Eclipse log window (or the Standard Out - STDOUT).

No comments: