Friday, April 19, 2013

Write testcase result into Excel using Web Driver



Write testcase result into Excel: The below code writes the result of the test case in the 8th column of the excel sheet by creating a new excel file test_results.xls

public void excelwrite(int row) throws BiffException, IOException,
                  RowsExceededException, WriteException {

Workbook existingWorkbook = Workbook.getWorkbook(new File(
                        "D:\\Test cases.xls"));
WritableWorkbook workbookCopy = Workbook.createWorkbook(new File(
                        "D:\\Test_Results.xls"), existingWorkbook);
WritableSheet sheetToEdit = workbookCopy.getSheet("Sheetname");
            WritableCell cell;
            Label l = new Label(8, row, "Pass");
            cell = (WritableCell) l;
            sheetToEdit.addCell(cell);
            workbookCopy.write();
            workbookCopy.close();
            existingWorkbook.close();
            File firstFile = new File(
                        "D:\\Test cases.xls");
            firstFile.delete();

            File oldFile = new File("D:\\Test_Results.xls");

            oldFile.renameTo(new File(
                        "D:\\Test cases.xls"));
      }     

Contribute by Ch.Suma

Read data from excel using Web driver



Read data from Excel:

      public String readExcel(int row, int col, int sno) throws Exception {
            File file = new File(
                        "D:\\TestData\\Test cases.xls");
            Workbook wb = Workbook.getWorkbook(file);
            Sheet st = wb.getSheet(sno);
            Cell cl = st.getCell(row, col);
            String sr = cl.getContents();
            return sr;
      }

Contributed by Ch. Suma

Write a value to text box using Web Driver



The below code writes the value in the textbox with property ele

public void Textbox(String ele, String value) {
            WebElement element = driver.findElement(By.id(ele));

            if (element != null) {
                  element.clear();
                  element.sendKeys(value);
            }
      }

Contributed by Ch. Suma

Thursday, April 18, 2013

Report pass or fail status in excel sheet in run time

The below code writes pass or fail status for a particular test case in the excel sheet.

The procedure takes 3 parameters.

shname is sheet name
tid is the test case id mentioned in the first column of the excel sheet
res is the result. it can be "pass" or "fail"
This result will be placed in the 5th column as the template will be (Testcase id, test case title, description, test data,Result)
If the test cases are more than 3000, change the value in the for loop

Public sub report_result(shname,tid,res)
    Set oexcel = CreateObject("Excel.Application")
    Set obook = oexcel.Workbooks.Open("c:\TF_Automation\Results\TF_TestScripts.xls")
    obook.Sheets(shname).select
'    oexcel.Visible=true
    For i=1 to 3000
        if tid=oexcel.Cells(i,1).value Then
            oexcel.Cells(i,5)=res
            Exit for
        End If
    Next
    obook.Save
    obook.Close
End Sub

Killng a process with specific size

The below code kills the javaw.exe process less than 40k size
 
Public sub close_processes
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process where name = 'javaw.exe'")
    npr=colProcess.count
    ReDim arrOld(npr)
    i=0
    For Each objProcess in colProcess
        If (objProcess.WorkingSetSize/1000000)<40 Then
            arrOld(i)=objProcess.ProcessId
            i=i+1
        End If
    Next
   
    For i=0 to ubound(arrOld)-1
        Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process where processID='" & arrOld(i) & "'")
        For Each objWMIProcess In colProcess
            intRC = objWMIProcess.Terminate()
         Next
    Next

    Set objWMIService = Nothing
    Set colProcess = Nothing

End Sub

Capturing and attaching screenshot for failed step


The below code capture screenshot and places the file in a specific location.

fCnt is a public variables to be declared in function library and initialized to 0

Public function capture_scrshot
       fCnt=fCnt+1
    desktop.CaptureBitmap"d:\Screenshots\" & fCnt & ".png",True
    capture_scrshot="d:\Screenshots\" & fCnt & ".png"
end  Function


This file can be attached to the failed step by calling this function as below

reporter.ReportEvent  micFail,"Step description","Step details",capture_scrshot

Handling Ajax dropdown - Webdriver



Below code types the value in the given dropdown   

  public void Dropdown1(String eleproperty, String value) {
            WebElement element = driver.findElement(By.id(eleproperty));
            if (element.isEnabled()) {
                  element.sendKeys(value);
                  Sleep();

            } else {
                  System.out.println("object is disable");
            }

            String actval = driver.findElement(By.id(eleproperty)).getAttribute("value");
            if (value.equals(actval)) {
                  System.out.println(actval);
            }
      }

Contributed by: Ch. Suma

AI in Software Testing: How Artificial Intelligence Is Transforming QA

For years, software testing has lived under pressure: more features, faster releases, fewer bugs, smaller teams. Traditional QA has done her...