-5

I am very new to Selenium RC. Please, help in my question.

  1. I installed jxl.jar file (Java library for Excel) and added it to Eclipse build path.

  2. I created an Excel file as readdata.xls where I entered some columns like username and password and provided some example inputs for those columns. Something like:

    UserName  Password
    Akshit    Ak12345
    Ashwika   AS12345
    
  3. I wrote the following code

    package seleniumscripts;
    
    import java.io.FileInputStream;
    import jxl.Sheet;
    import jxl.Workbook;
    
    
    public class Excelreaddata {
    
        private static String husername;
        private static String hpassword;
        private static String username;
        private static String password;
    
        public static void main(String[] args) throws Exception {
            FileInputStream fis =new FileInputStream("C:\\Documents and Settings\\Home\\Desktop\\Readdata.xls");
            Workbook wb = Workbook .getWorkbook(fis);
            Sheet s= wb.getSheet(0);
    
            Excelreaddata.husername =s.getCell(0,0).getContents();
            Excelreaddata.hpassword =s.getCell(0,1).getContents();
            System.out.println(Excelreaddata.husername);
            System.out.println(Excelreaddata.hpassword);
    
            Excelreaddata.username =s.getCell(1,0).getContents();
            Excelreaddata.password =s.getCell(1,1).getContents();
            System.out.println(Excelreaddata.username);
            System.out.println(Excelreaddata.password);             
    
            Excelreaddata.username =s.getCell(2,0).getContents();
            Excelreaddata.password =s.getCell(2,1).getContents();
            System.out.println(Excelreaddata.username);
            System.out.println(Excelreaddata.password);     
        }
    } 
    

My question is: when executing the code results are displayed only for Akshit, not for Ashwika. I need both to display.

Result I am getting like this when executing in Eclipse:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
  at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:288)
  at seleniumscripts.Excelreaddata.main(Excelreaddata.java:38)

UserName
Akshit
Password
Ak12345
4
  • 2
    Welcome to SQA, jyotsna. Your problem has nothing to do with Selenium, it is more about Excel library you use. I clarified the title accordingly. – dzieciou Jan 22 '13 at 9:26
  • Down voted as although it has full details it is programming not testing – Phil Kirkham Jan 22 '13 at 13:01
  • @Phil, I agree. Still the question can teach users how to isolate a defect, which is an important skill in testing :-) That's why in my answer I tried to to demonstrate how to read logs, code and API documentation instead of giving a solution only. – dzieciou Jan 22 '13 at 13:17
  • You gave a great answer - but we need to reach users to post questions in the right place ;) – Phil Kirkham Jan 22 '13 at 13:58
3

Defect isolation

The stacktrace

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
  at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:288)
  at seleniumscripts.Excelreaddata.main(Excelreaddata.java:38)

suggests that you are trying to access a cell that does not exist in the given sheet.

I cannot match line numbers in stacktrace wuth your code, but this probably is thrown in

Excelreaddata.username =s.getCell(2,0).getContents();

If you consult JavaDoc of the Sheet class, you will see that first parameter stands for column. You are requesting 3rd column (column/row indexes are 0-based, so 0, 1, 2, ...), whereas you mentioned in Excel file you have only two columns.

For the same reason, when you try to display username and password of the first user:

 Excelreaddata.husername =s.getCell(0,0).getContents();
 Excelreaddata.hpassword =s.getCell(0,1).getContents();
 System.out.println(Excelreaddata.husername);
 System.out.println(Excelreaddata.hpassword);

you get a column name and username of the first user instead:

UserName
Akshit

Solution

You should switch the order of parameters in your code.

// Print 1st user's name and password 
Excelreaddata.husername =s.getCell(0,1).getContents();
Excelreaddata.hpassword =s.getCell(1,1).getContents();
...
// Print 3rd user's name and password
Excelreaddata.husername =s.getCell(0,3).getContents();
Excelreaddata.hpassword =s.getCell(1,3).getContents();

Not the answer you're looking for? Browse other questions tagged or ask your own question.