Showing posts with label selenium automation learning. Show all posts
Showing posts with label selenium automation learning. Show all posts

Tuesday, 17 April 2012

Integrating Selenium tests with Hudson using ant [Selenium + Hudson + Ant]


For most folks the challenge is that their CI system lives in some Unix environment and Selenium tests need to run on a Windows machine.To get this working I did the following
My goal was to
a)Have the test drive and report selenium tests from a single interface
b)Ability to run it on different machines with various browsers
I picked Hudson for a) for the simple reason that it’s widely used in our organization as well as it had a Selenium Grid plugin which will probably be used for accomplishing b).It’s easily available and can display JUnit reports in graphical format with drilldown capabilities.In short the works and then some:)
Tools used are :Hudson,Selenium RC 1.0.1,FF 3.5,Ant 1.7,Selenium Grid plugin for hudson,Java,TestNG,SVN,Windows XP,Hudson JUnit plugin
Note:Selenium Grid is only introduced to run the tests on multiple machines and different browsers.If this is what you don’t need,you can avoid one more layer of complexity
Step 1
This step is crucial and can differ from org to org,but if your hudson master and slave are in same subnet this should generally work.Make sure of this by pinging each host to other.If not you might need to get help from your IT such that the master hudson can ping the slave hudson and viceversa.
Step 2 (skip if not running on multiple machines)
Install Selenium Grid plugin from Manage Hudson->Plugins->Install
Hudson restart will be required
Step 3
Create Hudson slave using following method
a)Go to Manage Hudson->Manage Nodes->Add Node(Dumb Slave)
b) Enter in Name field anything you wish.However hudson recommends giving same name as the host name of slave.So give the computer name of your windows machine where the tests will run
c)Num of executors is directly corresponding to number of RC’s you will run.For now keep it 1.If you want to run two browsers(e.g FF 3.5 and IE 7) this number needs to be bumped upto 2
d)Remote FS Root:c:\hudson (this is where hudson Workspace artifacts will be stored if needed).If you are going to use Selenium Grid plugin to run,you won’t need this
e)Labels:Give a unique label e.g(windowsxp_1).You will see it’s use later with Selenium Grid
f)Usage:Leave this machine for tied jobs only
g)Launch slave agents via JNLP
h)Availability keep this machines as much online as possible
Rest of the options keep blank.
Repeat the process for each Slave machine,but to keep things simple let’s just configure 1 machine at a time
Step 4
Go to the Slave machine and
a)Open your browser and point to Hudson Master
b)Go to the Manage Nodes page and you should see a launch JNLP jar icon.Click on it and it should start the process of downloading “remoting.jar” from Hudson Master(I had trouble with this and found out that somehow hudson master didn’t have the jar file at a specific location.With the help of my Hudson admin,I was able to copy this jar correctly).So if you repeatedly fail to launch the slave agent,first thing to check would be the location of remoting.jar.Also look at the java exception and it will give you clues in what could have gone wrong.
c)If all goes well you should see a small window with Hudson icon showing the message “Connected”.
The above step is very important and needs to happen in order to Hudson run selenium tests.If this doesn’t work look into hudson logs,slave logs found on hudson server and there should be some clue on what’s going on.
Step 5
Create a new job on hudson and the configure the option to “run it on the slave node” that you just created.
Publish the Junit report by pointing to the output directory xml file.This will be something like c:\hudson\workspace\xx\xx.xml
Depending on what your hudson job is doing the slave machine should have appropriate software.E.g if you are going to run ANT files,the slave machine needs to have ANT properly configured.Same thing goes with SVN.Essentially everything that your job would need to run on master should be available on slave as well.
Step 6
Start a selenium server on the slave machine as you would if it was your local machine.For this testing make sure that versions of browser that you used for your environment to run selenium test locally is same.This will eliminate unnecessary issues that are not due to this setup but browser configuration issues.
Step 7
Run the hudson job from your master and you should see the browser window popping up on the slave and running the tests.
The above should accomplish the task of running your Selenium tests on a single Windows machine.However many times you would need to run your tests in parallel as well as to optimize hardware use,make sure that Hudson can trigger test on same machine with different browser and report results back.In order to accomplish this we will use the Selenium Grid plugin which I will cover in next part if folks are interested.
Some resources that were helpful to me in this exercise were
Amit Easow’s writeup on Hudson integration

Friday, 23 March 2012

Use Java code to zip a folder for selenium automation testing

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FolderZiper {
  public static void main(String[] a) throws Exception {
    zipFolder("c:\\a", "c:\\a.zip");
  }

  static public void zipFolder(String srcFolder, String destZipFile) throws Exception {
    ZipOutputStream zip = null;
    FileOutputStream fileWriter = null;

    fileWriter = new FileOutputStream(destZipFile);
    zip = new ZipOutputStream(fileWriter);

    addFolderToZip("", srcFolder, zip);
    zip.flush();
    zip.close();
  }

  static private void addFileToZip(String path, String srcFile, ZipOutputStream zip)
      throws Exception {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
      addFolderToZip(path, srcFile, zip);
    } else {
      byte[] buf = new byte[1024];
      int len;
      FileInputStream in = new FileInputStream(srcFile);
      zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
      while ((len = in.read(buf)) > 0) {
        zip.write(buf, 0, len);
      }
    }
  }

  static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
      throws Exception {
    File folder = new File(srcFolder);

    for (String fileName : folder.list()) {
      if (path.equals("")) {
        addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
      } else {
        addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
      }
    }
  }
}

Reading writing from Excel files for selenium automation testing framework design

http://www.sqaforums.com/attachments/700551-xls_reader.txt


getCellData(String sheetName,String colName,int rowNum)
getRowCount(String sheetName)
getCellData(String sheetName,String colName,int rowNum)
getCellData(String sheetName,int colNum,int rowNum)
setCellData(String sheetName,String colName,int rowNum, String data)
setCellData(String sheetName,String colName,int rowNum, String data,String url)
addSheet(String  sheetname)
addColumn(String sheetName,String colName)
removeColumn(String sheetName, int colNum) 
isSheetExist(String sheetName)
getColumnCount(String sheetName)







package datatable;








import
org.apache.poi.hssf.usermodel.HSSFCellStyle;


import
org.apache.poi.hssf.usermodel.HSSFDateUtil;


import
org.apache.poi.hssf.util.HSSFColor;


import
org.apache.poi.ss.usermodel.Cell;


import
org.apache.poi.ss.usermodel.CellStyle;





import
org.apache.poi.ss.usermodel.IndexedColors;


import
org.apache.poi.xssf.usermodel.*;








import java.io.*;


import java.util.Calendar;








public class Xls_Reader {


       public static String filename = System.getProperty("user.dir")+"\\src\\config\\testcases\\TestData.xls";


       public  String path;


       public  FileInputStream fis = null;


       public  FileOutputStream fileOut =null;


       private XSSFWorkbook workbook = null;


       private XSSFSheet sheet = null;


       private XSSFRow row   =null;


       private XSSFCell cell = null;


      


       public Xls_Reader(String
path) {


             


              this.path=path;


              try {


                     fis = new
FileInputStream(path);


                     workbook = new XSSFWorkbook(fis);


                     sheet = workbook.getSheetAt(0);


                     fis.close();


              }
catch (Exception e) {


                     // TODO Auto-generated
catch block


                     e.printStackTrace();


              }


             


       }


       // returns the row
count in a sheet


       public int getRowCount(String
sheetName){


              int index = workbook.getSheetIndex(sheetName);


              if(index==-1)


                     return 0;


              else{


              sheet = workbook.getSheetAt(index);


              int number=sheet.getLastRowNum()+1;


              return number;


              }


             


       }


      


       // returns the data
from a cell


       public String
getCellData(String sheetName,String colName,
int rowNum){


              try{


                     if(rowNum <=0)


                           return "";


             


              int index = workbook.getSheetIndex(sheetName);


              int col_Num=-1;


              if(index==-1)


                     return "";


             


              sheet = workbook.getSheetAt(index);


              row=sheet.getRow(0);


              for(int i=0;i<row.getLastCellNum();i++){


                     //System.out.println(row.getCell(i).getStringCellValue().trim());


                     if(row.getCell(i).getStringCellValue().trim().equals(colName.trim()))


                           col_Num=i;


              }


              if(col_Num==-1)


                     return "";


             


              sheet = workbook.getSheetAt(index);


              row = sheet.getRow(rowNum-1);


              if(row==null)


                     return "";


              cell = row.getCell(col_Num);


             


              if(cell==null)


                     return "";


              //System.out.println(cell.getCellType());


              if(cell.getCellType()==Cell.CELL_TYPE_STRING)


                       return cell.getStringCellValue();


              else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC || cell.getCellType()==Cell.CELL_TYPE_FORMULA ){


                      


                       String cellText  = String.valueOf(cell.getNumericCellValue());


                       if (HSSFDateUtil.isCellDateFormatted(cell)) {


                         // format in form of M/D/YY


                             double d = cell.getNumericCellValue();





                             Calendar cal =Calendar.getInstance();


                             cal.setTime(HSSFDateUtil.getJavaDate(d));


                          cellText =


                           (String.valueOf(cal.get(Calendar.YEAR))).substring(2);


                         cellText = cal.get(Calendar.DAY_OF_MONTH) + "/" +


                                    cal.get(Calendar.MONTH)+1 + "/" +


                                    cellText;


                        


                         //System.out.println(cellText);





                       }





              return cellText;


              }else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)


                    return "";


                else


                       return String.valueOf(cell.getBooleanCellValue());


             


              }


              catch(Exception e){


                    


                     e.printStackTrace();


                     return "row "+rowNum+" or column
"
+colName
+
"
does not exist in xls"
;


              }


       }


      


       // returns the data
from a cell


       public String
getCellData(String sheetName,
int colNum,int rowNum){


              try{


                     if(rowNum <=0)


                           return "";


             


              int index = workbook.getSheetIndex(sheetName);





              if(index==-1)


                     return "";


             


      


              sheet = workbook.getSheetAt(index);


              row = sheet.getRow(rowNum-1);


              if(row==null)


                     return "";


              cell = row.getCell(colNum);


              if(cell==null)


                     return "";


             


         if(cell.getCellType()==Cell.CELL_TYPE_STRING)


                return cell.getStringCellValue();


         else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC || cell.getCellType()==Cell.CELL_TYPE_FORMULA ){


               


                String cellText  = String.valueOf(cell.getNumericCellValue());


                if (HSSFDateUtil.isCellDateFormatted(cell)) {


                  // format in form of M/D/YY


                       double d = cell.getNumericCellValue();





                       Calendar cal =Calendar.getInstance();


                       cal.setTime(HSSFDateUtil.getJavaDate(d));


                   cellText =


                    (String.valueOf(cal.get(Calendar.YEAR))).substring(2);


                  cellText = cal.get(Calendar.MONTH)+1 + "/" +


                             cal.get(Calendar.DAY_OF_MONTH) + "/" +


                             cellText;


                 


                 // System.out.println(cellText);





                }





               


               


                return cellText;


         }else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)


             return "";


         else


                return String.valueOf(cell.getBooleanCellValue());


              }


              catch(Exception e){


                    


                     e.printStackTrace();


                     return "row "+rowNum+" or column
"
+colNum
+
"
does not exist  in xls"
;


              }


       }


      


       // returns true if
data is set successfully else false


       public boolean setCellData(String
sheetName,String colName,
int rowNum, String data){


              try{


              fis = new FileInputStream(path);


              workbook = new XSSFWorkbook(fis);





              if(rowNum<=0)


                     return false;


             


              int index = workbook.getSheetIndex(sheetName);


              int colNum=-1;


              if(index==-1)


                     return false;


             


             


              sheet = workbook.getSheetAt(index);


             





              row=sheet.getRow(0);


              for(int i=0;i<row.getLastCellNum();i++){


                     //System.out.println(row.getCell(i).getStringCellValue().trim());


                     if(row.getCell(i).getStringCellValue().trim().equals(colName))


                           colNum=i;


              }


              if(colNum==-1)


                     return false;





              sheet.autoSizeColumn(colNum);


              row = sheet.getRow(rowNum-1);


              if (row == null)


                     row = sheet.createRow(rowNum-1);


             


              cell = row.getCell(colNum);


              if (cell == null)


               cell = row.createCell(colNum);





           // cell style


           //CellStyle cs =
workbook.createCellStyle();


           //cs.setWrapText(true);


           //cell.setCellStyle(cs);


           cell.setCellValue(data);





           fileOut = new FileOutputStream(path);





              workbook.write(fileOut);





           fileOut.close();





              }


              catch(Exception e){


                     e.printStackTrace();


                     return false;


              }


              return true;


       }


      


      


       // returns true if
data is set successfully else false


       public boolean setCellData(String
sheetName,String colName,
int rowNum, String data,String url){


              //System.out.println("setCellData
setCellData******************");


              try{


              fis = new FileInputStream(path);


              workbook = new XSSFWorkbook(fis);





              if(rowNum<=0)


                     return false;


             


              int index = workbook.getSheetIndex(sheetName);


              int colNum=-1;


              if(index==-1)


                     return false;


             


             


              sheet = workbook.getSheetAt(index);


              //System.out.println("A");


              row=sheet.getRow(0);


              for(int i=0;i<row.getLastCellNum();i++){


                     //System.out.println(row.getCell(i).getStringCellValue().trim());


                     if(row.getCell(i).getStringCellValue().trim().equalsIgnoreCase(colName))


                           colNum=i;


              }


             


              if(colNum==-1)


                     return false;


              sheet.autoSizeColumn(colNum);
//ashish


              row = sheet.getRow(rowNum-1);


              if (row == null)


                     row = sheet.createRow(rowNum-1);


             


              cell = row.getCell(colNum);


              if (cell == null)


               cell = row.createCell(colNum);


                    


           cell.setCellValue(data);


           XSSFCreationHelper createHelper = workbook.getCreationHelper();





           //cell style for hyperlinks


           //by default hypelrinks are blue and
underlined


           CellStyle hlink_style = workbook.createCellStyle();


           XSSFFont hlink_font = workbook.createFont();


           hlink_font.setUnderline(XSSFFont.U_SINGLE);


           hlink_font.setColor(IndexedColors.BLUE.getIndex());


           hlink_style.setFont(hlink_font);


           //hlink_style.setWrapText(true);





           XSSFHyperlink link =
createHelper.createHyperlink(XSSFHyperlink.
LINK_FILE);


           link.setAddress(url);


           cell.setHyperlink(link);


           cell.setCellStyle(hlink_style);


            


           fileOut = new FileOutputStream(path);


              workbook.write(fileOut);





           fileOut.close();





              }


              catch(Exception e){


                     e.printStackTrace();


                     return false;


              }


              return true;


       }


      


      


      


       // returns true if
sheet is created successfully else false


       public boolean addSheet(String  sheetname){           


             


              FileOutputStream
fileOut;


              try {


                      workbook.createSheet(sheetname);


                      fileOut = new FileOutputStream(path);


                      workbook.write(fileOut);


                   fileOut.close();                


              }
catch (Exception e) {                 


                     e.printStackTrace();


                     return false;


              }


              return true;


       }


      


       // returns true if
sheet is removed successfully else false if sheet does not exist


       public boolean removeSheet(String
sheetName){         


              int index = workbook.getSheetIndex(sheetName);


              if(index==-1)


                     return false;


             


              FileOutputStream
fileOut;


              try {


                     workbook.removeSheetAt(index);


                     fileOut
=
new FileOutputStream(path);


                     workbook.write(fileOut);


                  fileOut.close();          


              }
catch (Exception e) {                 


                     e.printStackTrace();


                     return false;


              }


              return true;


       }


       // returns true if
column is created successfully


       public boolean addColumn(String
sheetName,String colName){


              //System.out.println("**************addColumn*********************");


             


              try{                      


                     fis = new FileInputStream(path);


                     workbook = new XSSFWorkbook(fis);


                     int index = workbook.getSheetIndex(sheetName);


                     if(index==-1)


                           return false;


                    


              XSSFCellStyle
style =
workbook.createCellStyle();


              style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);


              style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);


             


              sheet=workbook.getSheetAt(index);


             


              row = sheet.getRow(0);


              if (row == null)


                     row = sheet.createRow(0);


             


              //cell =
row.getCell();   


              //if (cell == null)


              //System.out.println(row.getLastCellNum());


              if(row.getLastCellNum() ==
-1)


                     cell = row.createCell(0);


              else


                     cell = row.createCell(row.getLastCellNum());


              


               cell.setCellValue(colName);


               cell.setCellStyle(style);


              


               fileOut = new FileOutputStream(path);


                     workbook.write(fileOut);


                  fileOut.close();          





              }catch(Exception e){


                     e.printStackTrace();


                     return false;


              }


             


              return true;


             


             


       }


       // removes a column
and all the contents


       public boolean removeColumn(String
sheetName,
int colNum) {


              try{


              if(!isSheetExist(sheetName))


                     return false;


              fis = new FileInputStream(path);


              workbook = new XSSFWorkbook(fis);


              sheet=workbook.getSheet(sheetName);


              XSSFCellStyle
style =
workbook.createCellStyle();


              style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);


              XSSFCreationHelper
createHelper =
workbook.getCreationHelper();


              style.setFillPattern(HSSFCellStyle.NO_FILL);


             


          


      


              for(int i
=0;i


                     row=sheet.getRow(i);


                     if(row!=null){


                           cell=row.getCell(colNum);


                           if(cell!=null){


                                  cell.setCellStyle(style);


                                  row.removeCell(cell);


                           }


                     }


              }


              fileOut = new FileOutputStream(path);


              workbook.write(fileOut);


           fileOut.close();


              }


              catch(Exception e){


                     e.printStackTrace();


                     return false;


              }


              return true;


             


       }


 
//
find whether sheets exists  


       public boolean isSheetExist(String
sheetName){


              int index = workbook.getSheetIndex(sheetName);


              if(index==-1){


                     index=workbook.getSheetIndex(sheetName.toUpperCase());


                           if(index==-1)


                                  return false;


                           else


                                  return true;


              }


              else


                     return true;


       }


      


       // returns number
of columns in a sheet 


       public int
getColumnCount(String sheetName){


              // check if sheet
exists


              if(!isSheetExist(sheetName))


               return -1;


             


              sheet = workbook.getSheet(sheetName);


              row = sheet.getRow(0);


             


              if(row==null)


                     return -1;


             


              return row.getLastCellNum();


             


             


             


       }


       //String sheetName,
String testCaseName,String keyword ,String URL,String message


       public boolean addHyperLink(String
sheetName,String screenShotColName,String testCaseName,
int index,String
url,String message){


              //System.out.println("ADDING
addHyperLink******************");


             


              url=url.replace('\\', '/');


              if(!isSheetExist(sheetName))


                      return false;


             


           sheet = workbook.getSheet(sheetName);


          


           for(int i=2;i<=getRowCount(sheetName);i++){


              if(getCellData(sheetName,
0, i).equalsIgnoreCase(testCaseName)){


                     //System.out.println("**caught
"+(i+index));


                     setCellData(sheetName,
screenShotColName, i+index, message,url);


                     break;


              }


           }








              return true;


       }


       public int getCellRowNum(String
sheetName,String colName,String cellValue){


             


              for(int
i=2;i<=getRowCount(sheetName);i++){


              if(getCellData(sheetName,colName
, i).equalsIgnoreCase(cellValue)){


                     return i;


              }


           }


              return -1;


             


       }


             


       // to run this on
stand alone


       public static void main(String arg[]) throws IOException{


             


              //System.out.println(filename);


              Xls_Reader
datatable =
null;


             





                      datatable = new Xls_Reader("D:\\WorkSpace\\Vitera_automation
_Script\\src\\config\\vitera_controller.xlsx"
);


                           for(int col=0 ;col<
datatable.getColumnCount(
"TC5"); col++){


                                  System.out.println(datatable.getCellData("TC5", col, 1));


                           }


       }


      


      


}