Monday, February 7, 2011

Close all open browser before start execution

Public Function CloseAllBrowsers()


     'Create a description for browser
      Set oBrowser = Description.Create
      oBrowser("micclass").Value = "Browser"
      Set oPage = Description.Create
      oPage("micclass").Value = "Page"
      'Get all browsers
      Set allBrowser = Desktop.ChildObjects(oBrowser)
      Dim i, iCount
      iCount = allBrowser.Count - 1
      For i = 0 To iCount
            'Get the page object from the browser
            Set oPg = allBrowser(i).ChildObjects(oPage)(0)
            allBrowser(i).Close
     Next

End Function

Fucntion to create folder and check if the folder is exists or not

' Fucntion to create folder and check if the folder is exists or not.
'This Function can help to create run time Framework Folder.
Function CreateFolderStruct(FolderPath)

      'Create Folder
      On Error Resume Next
      dim objFSO
      strFldr=FolderPath
      set objFSO=CreateObject("Scripting.FileSystemObject")
      if isFolderExists(FolderPath) = "False" then
              objFSO.CreateFolder(strFldr)
     end if
     set objFSO=Nothing

End Function

Function isFolderExists(FolderPath)
      On Error Resume Next
      dim objFSO
      set objFSO=CreateObject("Scripting.FileSystemObject")
      if objFSO.FolderExists(FolderPath) then
            isFolderExists = "True"
      else
            isFolderExists = "False"
      end if
      set objFSO=Nothing
End Function

Vbscript code to send a mail in QTP


Function SendMail()

       Set objMessage = CreateObject("CDO.Message")
       objMessage.Subject = "This is for Subject text" ' Subject details
       objMessage.From = "abc@gmail.com" ' From Email ids
       objMessage.To = "abc@gmail.com;All@gmail.com" ' To Email ids
       objMessage.CC = "abc@gmail.com;All@gmail.com" ' CC Email ids
       objMessage.BCC = "abc@gmail.com;All@gmail.com" ' BCC Email ids
       'objMessage.TextBody ="Place Normal text body" ' For Text format
       objMessage.HTMLBody = "Place HTML code for body text"  For HTML body
       objMessage.AddAttachment "C:\abc.txt" ' Path for Attachment
       objMessage.Send
 
End Function

Friday, February 4, 2011

XML Web Service Automation Testing

Below code show the web service testing without qtp Add-in

Click here for webService XML file Weather Forecast Web Service Link
For Excel file Mail me.. neerajlad171@gmail.com

Dim counter
counter = 1
Dim CommonfilePath, ExcelSheetName, DataSheetNo, DataSheetName
CommonfilePath = "D:\QTP_Practive\WebServices\SampleData_1.xls"
DataSheetName = "OutputWeatherFC"
DataSheetNo = DataTable.GetSheetCount + 1
ExcelSheetName = "OutputWeatherFC"
Call ImportData(CommonfilePath, ExcelSheetName, DataSheetNo, DataSheetName)

Dim xmldoc
Set xmldoc = CreateObject("msxml2.domdocument")
xmldoc.Load ("D:\QTP_Practive\WebServices\Response_WeatherByPlaceName.xml")
dtTopNode = DataTable.Value("TAGNAME", DataSheetName)
dtTags = DataTable.Value("TAGS", DataSheetName)
dtTagName = GetdtNodeName(dtTopNode)
Set TopNode = xmldoc.documentElement
MsgBox TopNode.nodeName
Call TreeTravel(TopNode, dtTagName, dtTags)
 
Function TreeTravel(TopNode, dtTagName, dtTags)
    If dtTags = "END" Then
       counter = counter + 1
       DataTable.SetCurrentRow (counter)
       dtTopNode = DataTable.Value("TAGNAME", DataSheetName)
      dtTags = DataTable.Value("TAGS", DataSheetName)
      dtTagName = GetdtNodeName(dtTopNode)
    Else
      If TopNode.nodetype = 1 Then
           If TopNode.nodeName = dtTagName And dtTags = "" Then
                  MsgBox TopNode.nodeName & " <=> " & TopNode.Text
           End If
           Set child = TopNode.childNodes
           ' msgbox "TopNode Name: " &TopNode.nodename
          For i = 0 To child.length - 1
                 If child.Item(i).nodetype <> 3 Then
                          counter = counter + 1
                          DataTable.SetCurrentRow (counter)
                          dtTopNode = DataTable.Value("TAGNAME", DataSheetName)
                          dtTags = DataTable.Value("TAGS", DataSheetName)
                          dtTagName = GetdtNodeName(dtTopNode)
                         Call TreeTravel(child.Item(i), dtTagName, dtTags)
                End If
           Next
     End If
  End If
End Function

Generic Function for button, Edit box, Combo box and Radio button

'########## button ##############
Function buttonclick(obutton)
     sobjname = obutton.tostring
     sstepname = "Buttonclick"
     bexists = obutton.Exist(0)

     If bexists Then
              smsg = sobjname&" Exists"
              reporter.ReportEvent micPass,sstepname,smsg
     else
              smsg = sobjname&" does not Exists"
               reporter.ReportEvent micFail,sstepname,smsg
               Exittest
     End If

     bdisabled = obutton.getroproperty("disabled")
     If bdisabled = 0 Then
         smsg = sobjname&" is enabled"
         reporter.ReportEvent micPass,sstepname,smsg
     else
         smsg = sobjname&" is disabled"
         reporter.ReportEvent micFail,sstepname,smsg
         Exittest
     End If

     smsg = "clicking on "&sobjname
     reporter.ReportEvent micPass,sstepname,smsg
     obutton.click
End Function

' ########## Edit box ###########
Function edit_set(oedit,sinputtext)
     sobjname = oedit.tostring
     sstepname = "EditSet"
     bexists = oedit.Exist(0)
     If bexists Then
          smsg = sobjname&" Exists"
          reporter.ReportEvent micPass,sstepname,smsg
    else
          smsg = sobjname&" does not Exists"
          reporter.ReportEvent micFail,sstepname,smsg
          Exittest
    End If

    bdisabled = oedit.getroproperty("disabled")
    If bdisabled = 0 Then
        smsg = sobjname&" is enabled"
        reporter.ReportEvent micPass,sstepname,smsg
    else
        smsg = sobjname&" is disabled"
        reporter.ReportEvent micFail,sstepname,smsg
        Exittest
    End If

    smsg = "Entering "&sinputtext&" into "&sobjname
    reporter.ReportEvent micPass,sstepname,smsg
    oedit.set sinputtext
End Function

' ########## Combo box ###########
Function itemselect(olist,sitemtoselect)
      sobjname = olist.tostring
      sstepname = "itemselect"
      bexists = olist.Exist(0)
      If bexists Then
            smsg = sobjname&" Exists"
            reporter.ReportEvent micPass,sstepname,smsg
      else
            smsg = sobjname&" does not Exists"
            reporter.ReportEvent micFail,sstepname,smsg
            Exittest
      End If
      bdisabled = olist.getroproperty("disabled")
      If bdisabled = 0 Then
           smsg = sobjname&" is enabled"
           reporter.ReportEvent micPass,sstepname,smsg
      else
            smsg = sobjname&" is disabled"
            reporter.ReportEvent micFail,sstepname,smsg
            Exittest
      End If
      bitemfound = false
      sallitems = olist.getroproperty("all items")
      allitems = split(sallitems,";")
      For i = 0 to ubound(allitems)
             av = allitems(i)
             If lcase(av) = lcase(sitemtoselect) Then
                 bitemfound = true
                 Exit for
             End If
      Next
      If bitemfound Then
         smsg = "Item "&sitemtoselect&" is found in "&sobjname
         Reporter.ReportEvent micPass,sstepname,smsg
      Else
           smsg = "Item "&sitemtoselect&" is not found in "&sobjname
           Reporter.ReportEvent micPass,sstepname,smsg
           Exittest
      End If
      smsg = "Selecting "&sitemtoselect&" from "&sobjname
      reporter.ReportEvent micPass,sstepname,smsg
      olist.select sitemtoselect

End Function

' ########## Radio Button ###########
Function selectRadioBtn(orbgroup,srbtoselect)
      sobjname = orbgroup.tostring
      sstepname = "selectRadioBtn"
      bexists = orbgroup.Exist(0)
      If bexists Then
          smsg = sobjname&" Exists"
           reporter.ReportEvent micPass,sstepname,smsg
      else
           smsg = sobjname&" does not Exists"
           reporter.ReportEvent micFail,sstepname,smsg
           Exittest
      End If
      bdisabled = orbgroup.getroproperty("disabled")
      If bdisabled = 0 Then
         smsg = sobjname&" is enabled"
          reporter.ReportEvent micPass,sstepname,smsg
     else
          smsg = sobjname&" is disabled"
          reporter.ReportEvent micFail,sstepname,smsg
          Exittest
      End If
      bitemfound = false
      sallitems = orbgroup.getroproperty("all items")
      allitems = split(sallitems,";")
      For i = 0 to ubound(allitems)
           av = allitems(i)
          If lcase(av) = lcase(srbtoselect) Then
                 bitemfound = true
                 Exit for
         End If
      Next
      If bitemfound Then
           smsg = "Item "&srbtoselect&" is found in "&sobjname
           Reporter.ReportEvent micPass,sstepname,smsg
      Else
           smsg = "Item "&srbtoselect&" is not found in "&sobjname
           Reporter.ReportEvent micPass,sstepname,smsg
           Exittest
      End If
       smsg = "Selecting "&srbtoselect&" from "&sobjname
       reporter.ReportEvent micPass,sstepname,smsg
       orbgroup.select srbtoselect
End Function

Thursday, February 3, 2011

Find the number of lines in text file


Function NoOfLineInTextFile() As Integer
      Dim objFSO, strTextFile, strData, arrLines, LineCount
      Const ForReading = 1
      strTextFile = "d:\StringFile.txt"
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      strData = objFSO.OpenTextFile(strTextFile, ForReading).ReadAll
      arrLines = Split(strData, vbCrLf)
      LineCount = UBound(arrLines) + 1
      Set objFSO = Nothing
      MsgBox "Number of lines available in : " & LineCount
      NoOfLineInTextFile = LineCount
End Function

Read a Flat file and updated specific word.

Problem: Find the ‘QQ’ word and next word should replace with user input

Logic:
  1. Read Line by Line from text file and storing into Array
  2. Updating array if QQ word found
  3. Writing into File again
StringFile.txt contains:

11;12;xxxxxxxx;6666;77777;7777777;uuuu;ayex345;QQ;yyy;w434;ccc;
1q5;9s9;2f; 
' ################ Function call #########################
call ReadFromFile()

' ############### Function defination #####################
Function ReadFromFile()

          Const ForReading = 1
          Set objFSO = CreateObject("Scripting.FileSystemObject")
          Set objFile = objFSO.OpenTextFile("d:\StringFile.txt", ForReading)
          Dim arrFileLines()
          i = 0
          Do Until objFile.AtEndOfStream
                ReDim Preserve arrFileLines(i)
                 arrFileLines(i) = objFile.ReadLine
                i = i + 1
         Loop
         objFile.Close

         Call UpdateArrayValue(arrFileLines) ' Update array
         Call WriteArrIntoFiles(arrFileLines) ' writing file

End Function

Function UpdateArrayValue(arrFileLines)
     
       For K = 0 to Ubound(arrFileLines)
               NewArr = Split(arrFileLines(K), ";")
               For j = 0 To UBound(NewArr)
                        If NewArr(j) = "QQ" Then
                                    NewArr(j + 1) = "xxx"   ' xxx is user input value
                        End If
               Next
               UpdatedNewArr = Join(NewArr, ";")
               arrFileLines(K) = UpdatedNewArr
      Next
End Function

Function WriteArrIntoFiles(Arr)

              Const ForWriting = 2
              Set objFSO = CreateObject("Scripting.FileSystemObject")
              Set f = objFSO.GetFile("d:\StringFile.txt")
              Set Ln = f.OpenAsTextStream(ForWriting)
              For j = 0 To UBound(Arr)
                    Ln.WriteLine (Arr(j))
              Next
              Ln.Close
End Function