Option Explicit 'Version documentation '================================== 'v1.0.0 - Initial release 'v1.0.1 -- Moved functions to framework 'v.1.0.2 -- Changed link html tag search object ' Added improved search regular expression 'v1.0.3 -- Added text by name search ' Split out objarray building for object search 'v1.0.4 -- Added $ replacement litteral to SetSearchRegExp ' Added micclass to dropdownlist for faster searching and less issues 'v1.0.5 -- Added WebGroup select ' Change "no results" verfication to Warning. ' Added Set file upload 'v1.0.6 -- Added capture and bitmap storage to QC 'v1.0.7 -- Changed CheckForObject to sync page before search Dim Browserobj, Pageobj, capturepath Set Browserobj = Description.Create() Browserobj("micclass").Value = "Browser" '***** set the title value to the title of your web page if needed. Browserobj("title").Value = "" Set Pageobj = Description.Create() Pageobj("micclass").Value = "Page" capturepath = "c:\qtpcapture\" Dim version version = "1.0.7" Reporter.ReportEvent micDone, "Web Framework Version", "Version: " & version Public function NetError() End Function '~~~~~~~~~~~~~~~~~~~~~~ CheckForObject ~~~~~~~~~~~~~~~~~ 'Purpose: ' - Use DP to find object on page 'Input: ' strobject - A value used to identify the object on a page. can be ID, Text, or other unique value ' strtype - the type of object to find (ie. Link, Text, Image) '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Public function CheckForObject(strobject, strtype) On error resume next Dim blnreturn, myobj dim linkarray, objdescription, objarray, numofobjects 'Create the descriptive array based on object and type objarray = SetObjectArray(strobject, strtype) 'Create Descriptive Object set objdescription = CreateDescriptionObject(objarray) 'Sync page Set myobj = Browser(Browserobj).Page(Pageobj) myobj.sync 'Search for objects set linkarray = myobj.ChildObjects(objdescription) numofobjects = linkarray.Count 'For debugging 'msgbox linkarray.count 'For i = 0 To numofobjects - 1 ' msgbox linkarray(i).GetROProperty("name") 'Next select case numofobjects 'case 1 ' set CheckForObject = linkarray(0) case 0 Case else Set CheckForObject = linkarray end select End Function '~~~~~~~~~~~~~~~~~~~~~~ SetObjectArray ~~~~~~~~~~~~~~~~~ 'Purpose: ' - Define the object array to use for DP searching based on most observed criteria 'Input: ' strobject - A value used to identify the object on a page. can be ID, Text, or other unique value ' strtype - the type of object to find (ie. Link, Text, Image) '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Public Function SetObjectArray(strobject, strtype) Dim objarray(5) Select Case lcase(strtype) Case "text" objarray(0)="html id||" & SetSearchRegExp(strobject) objarray(1)="html tag||input" objarray(2)="type||text" objarray(3)="micclass||WebEdit" Case "fileupload" objarray(0)="html id||" & SetSearchRegExp(strobject) objarray(1)="html tag||input" objarray(2)="type||file" Case "textbyname" objarray(0)="name||" & SetSearchRegExp(strobject) objarray(1)="html tag||input" objarray(2)="type||text" objarray(3)="micclass||WebEdit" Case "textarea" objarray(0)="name||" & SetSearchRegExp(strobject) objarray(1)="html tag||textarea" objarray(2)="micclass||WebEdit" Case "errordiv" objarray(0)="html tag||div|DIV" objarray(1)="innertext||^.*(?:" & strobject & ").*$" objarray(2)="micclass||WebElement" Case "alltd" objarray(1)="html tag||td" Case "table" objarray(0) = "html id||" & SetSearchRegExp(strobject) objarray(1)= "html tag||table" Case "link" objarray(0)="text||" & SetSearchRegExp(strobject) 'strobject & "|" & strobject & "[\s]+" <=== added better search regexpression to handle text 1.0.2 objarray(1)="html tag||a" objarray(2)="micclass||Link" Case "linkbyid" objarray(0)="html id||" & SetSearchRegExp(strobject) 'strobject & "|" & strobject & "[\s]+" <=== added better search regexpression to handle text 1.0.2 objarray(1)="html tag||a" objarray(2)="micclass||WebElement" Case "radio" objarray(0)="name||" & SetSearchRegExp(strobject) 'strobject & "|" & strobject & "[\s]+" <=== added better search regexpression to handle text 1.0.2 objarray(1)="html tag||input" objarray(2)="type||radio" objarray(3)="micclass||WebRadioGroup" Case "radiobyid" objarray(0)="html id||" & SetSearchRegExp(strobject) 'strobject & "|" & strobject & "[\s]+" <=== added better search regexpression to handle text 1.0.2 objarray(1)="html tag||input" objarray(2)="type||radio" objarray(3)="micclass||WebRadioGroup" Case "image" objarray(0)="name||" & SetSearchRegExp(strobject) objarray(1)="html tag||img" objarray(2)="micclass||Image" Case "imagesrc" objarray(0)="src||" & SetSearchRegExp(strobject) objarray(1)="html tag||img" objarray(2)="micclass||Image" Case "button" objarray(0)="html id||" & SetSearchRegExp(strobject) objarray(1)="html tag||input" objarray(2)="type||button|submit" Case "buttontext" objarray(0)="value||" & SetSearchRegExp(strobject) objarray(1)="html tag||input" objarray(2)="type||button|submit" Case "imagebutton" objarray(0)="name||" & SetSearchRegExp(strobject) objarray(1)="html tag||input" objarray(2)="type||image" Case "imagebuttonalt" objarray(0)="alt||" & SetSearchRegExp(strobject) objarray(1)="html tag||input" objarray(2)="type||image" Case "select" objarray(0)="name||" & SetSearchRegExp(strobject) objarray(1)="html tag||select" objarray(2)="micclass||WebList" Case "checkbox" objarray(0)="html id||" & SetSearchRegExp(strobject) objarray(1)="html tag||input" objarray(2)="type||checkbox" Case "tdtext" objarray(0)="html id||" & SetSearchRegExp(strobject) objarray(1)="html tag||td" Case "validation" objarray(0)="micclass||WebElement" objarray(1)="html tag||span|SPAN" objarray(2)="html id||" & strobject 'objarray(2)="visible||true" Case "reportservicesframe" objarray(0)="micclass||Frame" objarray(1)="url||^.*(?:" & strobject & ").*$" Case "calendar" objarray(0)="html id||" & SetSearchRegExp(strobject) objarray(1)="micclass||WbfCalendar" End Select SetObjectArray = objarray End Function '~~~~~~~~~~~~~~~~~~~~~~ findobject ~~~~~~~~~~~~~~~~~ 'Purpose: ' - Search for and object on the page 'Input: ' obj - A value used to identify the object on a page. can be ID, Text, or other unique value ' objtype - the type of object to find (ie. Link, Text, Image) '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Public function findobject(obj, objtype) Dim found found = false Set myobj = CheckForObject(obj, objtype) If not isEmpty(myobj) then found = true end if findobject = found End Function '~~~~~~~~~~~~~~~~~~~~~~ PageExists ~~~~~~~~~~~~~~~~~ 'Purpose: ' - Verify page object '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Public Function PageExists() Dim return, myobj return = false Set myobj = Browser(Browserobj).Page(Pageobj) myobj.Sync If myobj.Exist(1) Then return = true End If PageExists = return End Function '*************************** 'Find input object and set value '*************************** Public Function SetText(lobj, txtText) 'verify text object exists Dim myobj txtText = cstr(txtText) On error resume next Set myobj = CheckForObject(lobj, "text") If isEmpty(myobj) Then Set myobj = CheckForObject(lobj, "textbyname") End If If isEmpty(myobj) Then Set myobj = CheckForObject(lobj, "textarea") End If If not isEmpty(myobj) then ' With Browser(Browserobj).Page(Pageobj) ' .WebEdit(myobj).Set txtText ' end with Dim blnDisabled blnDisabled = 0 blnDisabled = myobj(0).GetROProperty("disabled") If blnDisabled = "1" Then Reporter.ReportEvent micFail, "Set Text", lobj & " is disabled. Unable to set text." else myobj(0).set txtText If myobj(0).GetROProperty("value") <> txtText Then Reporter.ReportEvent micFail, "Set Text", "Set text did not occur" End If wait(1) end if else Reporter.ReportEvent micFail, "Set Text", lobj & " unable to set text value." end if End Function '********************************** 'Don't set text to blank 'use this to skip inputs with default values '********************************** Public Function SetTextSkipBlanks(lobj, txtText) If txtText <> "" Then SetText lobj, txtText End If End Function '*********************************** 'Set text value of a file upload input '*********************************** Public Function SetFileUpload(obj, txtText) 'verify text object exists Dim myobj On error resume next Set myobj = CheckForObject(obj, "fileupload") If not isEmpty(myobj) then ' With Browser(Browserobj).Page(Pageobj) ' .WebEdit(myobj).Set txtText ' end with If myobj(0).GetROProperty("disabled") = "1" Then Reporter.ReportEvent micWarning, "Set File Upload", obj & " is disabled. Unable to set text." else myobj(0).set txtText wait(1) end if else Reporter.ReportEvent micFail, "Set Text", obj & " unable to set text value." end if End Function '******************************** 'Search and set a dropdown list '******************************** Public Function SetDropDown(obj, txtText) Dim myobj On error resume next Set myobj = CheckForObject(obj, "select") If not isEmpty(myobj) then ' With Browser(Browserobj).Page(Pageobj) ' .WebEdit(myobj).Set txtText ' end with If myobj(0).GetROProperty("disabled") = "1" or myobj(0).GetROProperty("disabled") = "disabled" Then Reporter.ReportEvent micWarning, "Set Dropdown", obj & " is disabled. Unable to set value." else If txtText <> "" Then myobj(0).Select cstr(txtText) End If end if else Reporter.ReportEvent micFail, "Set Dropdown", obj & " unable to set value." end if End Function '******************************** 'don't set to blank '******************************** Public Function SetDropDownSkipBlank(obj, txtText) If txtText <> "" Then SetDropDown obj, txtText End If End Function '********************************** 'Search and set checkboxs '********************************** Public Function SetCheckBox(obj, txtText, id) Dim myobj Set myobj = CheckForObject(obj, "checkbox") If not isEmpty(myobj) then ' With Browser(Browserobj).Page(Pageobj) ' .WebEdit(myobj).Set txtText ' end with Select Case lcase(txtText) Case "true", "yes", "on", "1" myobj(id).Set "ON" Case "false","no","off","0" myobj(id).Set "OFF" Case else Reporter.ReportEvent micFail, "Set Checkbox", txtText & " unable to set text value." End Select else Reporter.ReportEvent micFail, "Set Checkbox", obj & " unable to set value." end if End Function Public Function SetCheckBoxSkipBlank(obj, txtText, id) If txtText <> "" Then SetCheckBox obj, txtText, id End If End Function '****************************** 'Click different object based on boolean test '****************************** Public Sub DoClick(objt, objf, blnvalue) If blnvalue Then Click objt else Click objf End If End Sub '****************************** 'Search page for a clickable object and click it '****************************** Public Sub Click(obj) Dim myobj, objname, objlink, splitobj If instr(1,obj,"||") > 0 Then splitobj = split(obj,"||") objname = splitobj(0) objlink = splitobj(1) else objname = obj objlink = 0 End If Dim i, blnexit, blnselect i = 0 blnexit = false blnselect = false On error resume next While not blnexit Select Case i Case 0 'link set myobj = CheckForObject(objname, "Link") Case 1 'image set myobj = CheckForObject(objname, "Image") Case 2 'button set myobj = CheckForObject(objname, "Button") Case 3 'imagebutton set myobj = CheckForObject(objname, "Imagebutton") Case 4 ''buttontext set myobj = CheckForObject(objname, "buttontext") Case 5 ''buttontext set myobj = CheckForObject(objname, "linkbyid") Case 6 'image src set myobj = CheckForObject(objname, "imagesrc") Case 7 'image src set myobj = CheckForObject(objname, "imagebuttonalt") Case else Reporter.ReportEvent micFail, "Click", "Object " & objname & " is not found." blnexit = true End Select If not isEmpty(myobj) Then wait(2) myobj(objlink).Click blnexit = true Browser(Browserobj).Page(Pageobj).Sync End If i=i+1 Wend On error goto 0 End Sub '********************************* 'Set radio button group values '********************************* Public sub RadioButtonGroup(txtobj, txtvalue) Dim myobj On error resume next set myobj = CheckForObject(txtobj, "radio") If not isEmpty(myobj) then myobj(0).Select txtvalue else set myobj = CheckForObject(txtobj, "radiobyid") If not isEmpty(myobj) then myobj(0).Select txtvalue end if end if End Sub '********************************* 'Count rows in results table and report '********************************* Public sub VerifyResults(obj) 'Verify results for pass fail 'HoldsAdmin1_dgHoldList' On error resume next dim rows, objWebTable Set objWebTable = CheckForObject(obj, "table") If not IsEmpty(objWebTable) Then rows = objWebTable(0).RowCount If rows > 1 Then Reporter.ReportEvent micPass, "Search", rows - 1 & " results returned from search" else 'check for one result or other condition Dim msg msg = objWebTable(0).GetCellData(1,1) If instr(1,lcase(msg),"error") > 1 Then Reporter.ReportEvent micFail, "Search", msg else Reporter.ReportEvent micPass, "Search", msg End If End If else Reporter.ReportEvent micWarning, "Verify Results", "Unable to locate results table." End If On error goto 0 End Sub '*************************************** 'Set search expressions '*************************************** Public function SetSearchRegExp(strFieldname) strFieldname = Replace(strFieldname,"\$","$") strFieldname = Replace(strFieldname, "$", "\$") strFieldname = Replace(strFieldname, ":", "\:") strFieldname = Replace(strFieldname, "(", "\(") strFieldname = Replace(strFieldname, ")", "\)") SetSearchRegExp = "([\w\s\:\./$]+(" & strFieldname &")+)|(^" & strFieldname & ")|(" & strFieldname & "[\s]+)" '<=== added better search regexpression to handle text 1.0.2 End Function '************************************ 'Split array and create DP Object '************************************ Public function CreateDescriptionObject(objArray) Dim return, valsplit Set return = Description.Create() For x = 0 to ubound(objArray) - 1 If objArray(x) <> "" then valsplit = Split(objArray(x),"||") return(valsplit(0)).Value = valsplit(1) If valsplit(0) = "html tag" Then return(valsplit(0)).RegularExpression = false End If End If Next set CreateDescriptionObject = return End Function '**************************** 'Change the URL of browser '**************************** Public Sub Navigate(strURL) Browser(Browserobj).Navigate strURL Browser(Browserobj).Sync End Sub '**************************** 'Launch IE and URL '**************************** Public Sub LaunchWebURL(strURL) SystemUtil.Run "iexplore.exe", strURL wait(5) 'Dim myobj: Set myobj = Browser(Browserobj).Page(Pageobj) 'myobj.Sync Browser(Browserobj).Page(Pageobj).Sync Dim i i=1 While not found and i<6 found = PageExists wait(5) i=i+1 Wend If not found Then Reporter.ReportEvent micFail, "Launch URL", "Unable to launch application." End If End Sub '********************************** 'Ask for user input to verify operation '********************************** Public sub ManualResults() Dim myvalue myvalue = msgbox("Did expected result appear?", vbyesno + vbquestion, "Check Results") If myvalue = vbYes Then Reporter.ReportEvent micPass, "Expected Results", "View results came up" else Reporter.ReportEvent micFail, "Expected Results", "View results failed" End If End Sub '************************************ 'Search for any validation of obj '************************************ Public function CheckValidationError(obj) Dim myobj, return, objcnt return = false On error resume next ClearValidationPopup On error goto 0 'if validation object is not specified get all of them If obj = "" Then 'search all validators on page obj = "^[\w\s]+(Validator*)+[\w*]+|^(Validator)+[\w*]|^(Validator)" else 'set field search obj = SetSearchRegExp(obj) End If On error resume next Set myobj = CheckForObject(obj, "validation") On error goto 0 If not isEmpty(myobj) then 'Check each validation message to see if it's visible For objcnt = 0 to myobj.count - 1 If lcase(myobj(objcnt).Object.style.display) <> "none" Then Reporter.ReportEvent micwarning, "Validation Check", myobj(objcnt).GetROProperty("innertext") return = true End If next end if If not return Then 'Check for background color validation methods on all text fields return = ValidateForm() End If CheckValidationError = return End function '*********************************** 'Check a mouse over action '*********************************** Public Sub MouseOver(obj) Dim myobj, objname, objlink, splitobj If instr(1,obj,"||") > 0 Then splitobj = split(obj,"||") objname = splitobj(0) objlink = splitobj(1) else objname = obj objlink = 0 End If Dim i, blnexit, blnselect i = 0 blnexit = false blnselect = false On error resume next While not blnexit Select Case i Case 0 'link set myobj = CheckForObject(objname, "Link") Case else Reporter.ReportEvent micFail, "Mouse over", "Object " & objname & " is not found." blnexit = true End Select If not isEmpty(myobj) Then wait(2) myobj(objlink).FireEvent "onMouseOver" blnexit = true End If i=i+1 Wend On error goto 0 End Sub '***************************** 'Default for all .NET validators '***************************** Public function CheckValidationErrorNET() CheckValidationError "" End function '***************************** 'check for input object validations '***************************** Public function ValidateForm() Dim return, myobj, objcnt return = false ''Get all textbox objects Set myobj = CheckForObject("","alltext") For objcnt = 0 to myobj.count - 1 If myobj(objcnt).Object.CurrentStyle.BackgroundColor = "yellow" Then return = true Reporter.ReportEvent micFail, "Form Validation", "Field " & myobj(objcnt).GetROProperty("Name") & " has failed validation." End If Next ValidateForm = return End Function '****************************** 'Close Browser '****************************** Public Sub CloseBrowser(lBrowserobj) Browser(lBrowserobj).Close End Sub '******************************* 'Find object and return text value '******************************* Public function ReturnValue(obj, objtype) Dim myobj, return, objcnt Set myobj = CheckForObject(obj, objtype) On error goto 0 If not isEmpty(myobj) then return = myobj(0).GetTOProperty("text") end if End Function Public function CheckForString(obj, str) If instr(1,obj, str) > 1 Then CheckForString = true End If End Function '****************************** 'Reports any .NET server error '****************************** Public function CheckForErrorString(obj) Dim x, finished x=0 finished = false While not CheckForErrorString and not finished Select Case x Case 0 CheckForErrorString = CheckForString(obj, "error occurred") Case 1 CheckForErrorString = CheckForString(obj, "error has occurred") Case 2 CheckForErrorString = CheckForString(obj, "Server Error") Case else finished = true End Select x=x+1 Wend End Function '************************** 'test if txtvalue is found on page '************************** Public function KeywordValidate(txtvalue) 'Reverse logic if exist then fail dim browsertext browsertext = Browser(Browserobj).Page(Pageobj).Object.Body.innerhtml If instr(1, browsertext, txtvalue) > 1 Then 'Reporter.ReportEvent micPass, "Keyword Validation", txtvalue & " was located on page." KeywordValidate = False Parameter("blnValidate") = False else 'Reporter.ReportEvent micFail, "Keyword Validation", txtvalue & " was not located on page." KeywordValidate = True Parameter("blnValidate") = True End If End Function '~~~~~~~~~~~~~~~~~~f_UploadQCAttachment~~~~~~~~~~~~~~~~~~~~ 'Purpose: ' - Upload attachments to Quality Center from QTP 'Input: ' - v_Path: the path of the attachment ' - v_Filename: the filename '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Public Function f_UploadQCAttachment (v_Path, v_Filename) Dim o_CurrentRun, o_AttachmentsFact, o_Att, o_ExtStr Set o_CurrentRun = QCUtil.CurrentRun ' Check that we are running this test from QC, otherwise we can exit If (o_CurrentRun Is Nothing) Then Exit Function End If 'If the v_Path has a trailing \, remove it. If (Right(v_Path,1) = "\") Then v_Path = Left(v_Path, Len(v_Path)-1) 'now attach the file to the current test Set o_AttachmentsFact = o_CurrentRun.Attachments Set o_att = o_AttachmentsFact.AddItem(v_Filename) o_att.Post Set o_ExtStr = o_att.AttachmentStorage o_ExtStr.ClientPath = v_Path o_ExtStr.Save v_Filename, true f_UploadQCAttachment = o_att.Post End Function '~~~~~~~~~~~~~~~~~~~~~~ CAPTURE ~~~~~~~~~~~~~~~~~ 'Purpose: ' - Capture screen pring 'Input: ' objname - Used as part of the filename for the screen capture '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Public sub Capture(objname) Dim timestamp timestamp = replace(replace(FormatDateTime(DateValue(Now)), "/", "_") & "_" & Replace(Time,":","")," ", "") If objname = "" Then objname = TestName End If 'check for directory Dim fso: set fso = CreateObject("Scripting.FileSystemObject") Dim capturepath capturepath = "c:\qtpcapture" If not fso.FolderExists(capturepath) Then fso.CreateFolder(capturepath) End If Browser(Browserobj).CaptureBitmap capturepath & "\capture_"& objname & ".bmp", true f_UploadQCAttachment capturepath , "capture_"& objname & ".bmp" 'delete file from local drvie fso.DeleteFile capturepath & "\capture_"& objname & ".bmp" Set fso = nothing End Sub '~~~~~~~~~~~~~~~~~~~~~~ SetCalendarDate ~~~~~~~~~~~~~~~~~ 'Purpose: ' - Set Calendar object value 'Input: ' obj - used to identify object on page ' dtvalue - value to set '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Public Sub SetCalendarDate(obj, dtvalue) Dim myobj On error resume next set myobj = CheckForObject(obj, "calendar") On error goto 0 If not isEmpty(myobj) then 'verify date format as dd-MMM-yyyy If IsDate(dtvalue) = True Then ' DIM dteDay, dteMonth, dteYear ' dteDay = Day(dtvalue) ' dteMonth = Month(dtvalue) ' dteYear = Year(dtvalue) ' JXIsoDate = Right(Cstr(dteDay + 100),2) & _ ' "-" & Right(Cstr(dteMonth + 100),2) & _ ' "-" & dteYear 'if month is not current month then move next On error resume next x = datediff("m", Now, dtvalue) If x < 0 Then x=x*-1 blnprevious = true else blnprevious = false End If y= 1 While y <=x If blnprevious Then myobj(0).ShowPreviousMonth else myobj(0).ShowNextMonth End If wait(1) Browser(Browserobj).Page(Pageobj).Sync set myobj = CheckForObject(obj, "calendar") y=y+1 Wend end if myobj(0).SetDate dtvalue end if End Sub