Selenium + Excel

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import jxl.*;
import jxl.read.biff.BiffException;

import jxl.write.*;
import jxl.write.biff.RowsExceededException;


public class Medical {

      
      
        /**
         * @param args
         * @throws InterruptedException
         * @throws IOException
         * @throws BiffException
         * @throws WriteException
         * @throws RowsExceededException
         */
        public static void main(String[] args) throws InterruptedException, BiffException, IOException, RowsExceededException, WriteException {
              
//read data from config file
                Workbook workbook = Workbook.getWorkbook(new File("D:\\AJ\\XYZ.xls"));
                Sheet sheet = workbook.getSheet("Configuration");
              
                String URL = sheet.getCell(0,1).getContents();
                String UserName = sheet.getCell(1,1).getContents();
                String Pwd = sheet.getCell(2,1).getContents();
              
                Sheet shtUser = workbook.getSheet("User");
                String strUserLastName = shtUser.getCell(0,1).getContents();
              
                Sheet shtSummaryTable = workbook.getSheet("SummaryTable");
                //int cntCol=shtSummaryTable.getColumns();
                //int rowCnt=shtSummaryTable.getRows();
              
      
               
//Write result

                WritableWorkbook result = Workbook.createWorkbook(new File("D:\\AJ\\Result.xls"));
                WritableSheet shtResult = result.createSheet("TestCase",0);
              
                Label Header1 = new Label(0, 0, "TestCaseNo");
                Label Header2 = new Label(1,0, "TestCaseDescription");
                Label Header3 = new Label(2, 0, "Result");
                shtResult.addCell(Header1);
                shtResult.addCell(Header2);
                shtResult.addCell(Header3);          
                          
              
              

              
              
//Invoke firefox and login into the application
WebDriver f1=new FirefoxDriver();
f1.get(URL);
//f1.get("http://www.caisis.org/demo/Login.aspx");
f1.findElement(By.xpath(".//*[@id='userName']")).sendKeys(UserName);
f1.findElement(By.xpath(".//*[@id='password']")).sendKeys(Pwd);
f1.findElement(By.xpath(".//*[@id='enterButton']")).click();

f1.findElement(By.xpath(".//*[@id='PageHeader1_searchWords']")).sendKeys(strUserLastName);
f1.findElement(By.xpath(".//*[@id='searchBtn']")).click();
f1.findElement(By.xpath(".//*[@id='PageHeader1_rptHeaderTabs__ctl1_TabCenterTD']")).click();

Label row11 = new Label(0, 1, "Caisis_TC_01");
Label row12 = new Label(1, 1, "Login");
Label row13 = new Label(2, 1, "Pass");
shtResult.addCell(row11);
shtResult.addCell(row12);
shtResult.addCell(row13);


String whandle = f1.getWindowHandle();
 //Go to Operation room details and submit details for the room
f1.findElement(By.xpath(".//*[@id='menu']/li[3]/a")).click();
f1.findElement(By.xpath(".//*[@id='menu']/li[3]/ul/li[1]/a")).click();

WebElement theFrame = f1.findElement(By.name("DataEntryFrame"));
f1.switchTo().frame(theFrame);
WebElement chkBox= f1.findElement(By.xpath(".//*[@id='_ctl3__ctl0_OperatingRoomDetails_OpPending']"));
chkBox.click();

f1.findElement(By.xpath(".//*[@id='_ctl3__ctl0_OperatingRoomDetails_OpDateText']")).sendKeys("11/30/2012");

Select droplist = new Select(f1.findElement(By.xpath(".//*[@id='_ctl3__ctl0_OperatingRoomDetails_OpService']"))); 
droplist.selectByVisibleText("GMT");


f1.findElement(By.xpath(".//*[@id='_ctl3__ctl0_OperatingRoomDetails_OpCaseSurgeon']")).sendKeys("Ceaser Johnson");
f1.findElement(By.xpath(".//*[@id='_ctl3__ctl0_OperatingRoomDetails_OpAdmitDateText']")).sendKeys("11/30/2012");
f1.findElement(By.xpath(".//*[@id='_ctl3__ctl0_OperatingRoomDetails_OpDischargeDateText']")).sendKeys("12/01/2012");
f1.findElement(By.xpath(".//*[@id='SaveBtn']")).click();
f1.switchTo().window(whandle);

Label row21 = new Label(0, 2, "Caisis_TC_02");
Label row22 = new Label(1, 2, "Enter Operating Room Details ");
Label row23 = new Label(2, 2, "Pass");
shtResult.addCell(row21);
shtResult.addCell(row22);
shtResult.addCell(row23);

//Go to Patients summary page and verify the summary is populated

f1.findElement(By.xpath(".//*[@id='menu']/li[1]/a")).click();
f1.findElement(By.xpath(".//*[@id='menu']/li[1]/ul/li[1]/a")).click();


                //Switch to Summary
WebElement SummaryFrame = f1.findElement(By.name("ChronListFrame"));
f1.switchTo().frame(SummaryFrame);


                // read data from the table
WebElement table_element = f1.findElement(By.xpath(".//*[@id='chronListTable']"));
List tr_collection=table_element.findElements(By.xpath("id('chronListTable')/tbody/tr"));

                 //for(int rowCounter = 2;rowCounter<=rowCnt ; rowCounter++)
                 //{
                        // String strDate=shtSummaryTable.getCell(0,rowCounter-1).getContents();
                         //String strVariable=shtSummaryTable.getCell(1,rowCounter-1).getContents();
                         //String strValue=shtSummaryTable.getCell(2,rowCounter-1).getContents();
                         //String strQuality=shtSummaryTable.getCell(3,rowCounter-1).getContents();                     
                 //}



//System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());
int row_num,col_num;
row_num=1;
col_num=0;
for(WebElement trElement : tr_collection)
{
    List td_collection=trElement.findElements(By.xpath("td"));
  //  System.out.println("NUMBER OF COLUMNS="+td_collection.size());
    col_num=0;
    for(WebElement tdElement : td_collection)
    {
         if (shtSummaryTable.getCell(col_num,row_num).getContents().trim().equals(tdElement.getText().trim()))
                         {
                                         
                         System.out.println("Matched");
                         }
         else
         {
                 System.out.println("UnMatched");
                 System.out.println (tdElement.getText().trim());
                        System.out.println(shtSummaryTable.getCell(col_num,row_num).getContents().trim());
         }
        //System.out.println("row # "+row_num+", col # "+col_num+ "text="+tdElement.getText());
        col_num++;
    }
    if (row_num==4)
                 {
                         break;
                 }
    row_num++;
}             
              
Label row31 = new Label(0, 3, "Caisis_TC_03");
Label row32 = new Label(1, 3, "Summary Table Matching ");
Label row33 = new Label(2, 3, "Pass");
shtResult.addCell(row31);
shtResult.addCell(row32);
shtResult.addCell(row33);

                workbook.close();
                result.write();
                result.close();
              
//f1.quit();
        }

}

Comments

Popular posts from this blog

Arrays

What is the difference between eval, execute and executeglobal.

Sample Test Strategy for a MicroService Project with APIs only