Thursday, April 15, 2021

How to write the Regfunction UFT Framework

How to write the Regfunction UFT Framework :

'*** CommonFunction Starts **************************************************************************************************************

'****************************************************************************************************************************************

' Common Subroutines/Functions contained in this library include:

'

' RegisterUserFunc:

'~~~~~~~~~~~~~~~~~~

' #1 NavigateBrowser   ~~~ Navigates to the given URL 

' #2 Delete_Browser_Cookies   ~~~ Deletes browser cookies for Chrome and IE

' #3 CloseBrowserTabOptional ~~~ Closes the given browser if input is positive (IsTrue)

'

' Functions: 

'~~~~~~~~~~~

' #1 OpenBrowser   ~~~ Opens the given browser to "about:blank"

' #2 SetBrowserZoomTo100          ~~~ Sets the browser zoom level to 100% to alleviate object recognition issues

' #3 CloseAllBrowsers   ~~~ Closes all the open browsers

' #3 CloseBrowserTab              ~~~ Closes a specified browser tab

'

'****************************************************************************************************************************************


'****************************************************************************************************************************************

' Function/Subroutine Name: OpenBrowser

'

' Revision history:

'   Description                                                           Change Code  Date        Author         

'   ---------------------------------------------------------------------------------  --------    --------------------

'   Opens the given browser to "about:blank"                                02/02/2019  Sundar Manikam

' Delete browser cookies if Environment("DeleteBrowserCookies") = True  CC01         02/01/2020  Sundar Manikam

' Assigne the Environment("BrowserName") ot standard values   CC02    09/26/2021  Sundar Manikam

'****************************************************************************************************************************************

'

'@Description Open the given browser


Sub OpenBrowser (browserName)

If browserName = "" Then

'ReportEvent "criticalfail", "No Browser designated. Please designate one of the following browsers:", "IE" & vbcrlf & "Chrome" & vbcrlf & "Firefox"

Exit Sub

End If

'WriteLogLine "OpenBrowser",browserName

Select Case Lcase(browserName)

Case "ie", "internet explorer", "iexplore.exe"

browserName = "iexplore.exe"

Environment("BrowserName") = "ie" 'CC02

Case "chrome", "chrome.exe", "ch"

browserName = "chrome.exe"

Environment("BrowserName") = "chrome" 'CC02

Case "firefox", "ff", "firefox.exe"

browserName = "firefox.exe"

Environment("BrowserName") = "firefox" 'CC02

Case "incognito"

Environment("BrowserName") = "incognito"

Case Else

browserName = "iexplore.exe"

Environment("BrowserName") = "ie" 'CC02

End Select

'Close all open browsers except ALM

CloseAllBrowsers()

If Environment("BrowserName") = "incognito" Then

Systemutil.Run  "chrome.exe", " -incognito about:blank", "", "", 3

Else

SystemUtil.Run browserName,"about:blank", "", "", 3

End If

'CC01 - Delete cookies if Environment("DeleteBrowserCookies") = True

If IsTrue(Environment("DeleteBrowserCookies")) Then

Environment("DeleteBrowserCookies") = False 'Reset Environment("DeleteBrowserCookies") = False

Browser("Creationtime:=0").Sync

Browser("Creationtime:=0").DeleteCookies "yes" 'delete cookies

'WebUtil.DeleteCookies

'OpenBrowser Browsername

End If

'Browser("Creationtime:=0").Sync

browserVersion = Browser("Creationtime:=0").GetROProperty("Version")

Environment("browserVersion") = browserVersion

Reportevent micDone, "Open Browser " & browserVersion, "Open Browser " & browserVersion

'ReportEvent micDone, "Browser Version: " & browserVersion, "Browser Version: " & browserVersion

'WriteLogLine "OpenBrowser","Browser Version: " & browserVersion


End Sub

'

'****************************************************************************************************************************************




'****************************************************************************************************************************************

' Function/sub-routine name: SetBrowserZoomTo100

'

' Revision history:

'   Description                                                                   Date          Author         

'   ----------------------------------------------------------------------------  --------      --------------------

' Called from the OpenBrowser function

'   Original Code                                                                 02/02/2019    Sundar Manikam

'   modified to clarify code and use common sendkeys Function                     01/02/2020    Sundar Manikam 

' Added condition for chrome and ff to set browser to 100% CC01   03/29/2020 Sundar Manikam

' Added condition for IE to set browser to 100% CC02   04/05/2020 Sundar Manikam

'*************************************************************************************************************************************

'

'@Description Sets browser zoom level to 100% for IE, Chrome, or Firefox. 


Sub setBrowserZoomTo100 ()


    Exit Sub

    'WriteLogLine "setBrowserZoomTo100",""

    If Environment("BrowserName") <> "ie" Then

    ctrlKey       = "^"

    zoomLevel_100 = "0"

    If Browser("micclass:=Browser").Exist(3) Then    

        SendKey ctrlKey & zoomLevel_100

    End If

ElseIf Environment("BrowserName") = "ie" Then

'Open the options list and check that "Zoom (100%)" is present, if not then reopen and select it

Browser("CreationTime:=0").Sync

Browser("CreationTime:=0").WinToolbar("Text:=Favorites and Tools Bar","nativeclass:=ToolbarWindow32").Press "Tools"

wait 1

If Browser("CreationTime:=0").WinMenu("menuobjtype:=3").GetItemProperty("<Item 3>", "Label") <> "Zoom (100%)" Then

Browser("CreationTime:=0").WinToolbar("Text:=Favorites and Tools Bar","nativeclass:=ToolbarWindow32").Press "Tools"

Browser("CreationTime:=0").WinMenu("menuobjtype:=3").Select "<Item 3>;100%"

End If

End If


End Sub ' SetBrowserZoomTo100 ()

'

'****************************************************************************************************************************************



'****************************************************************************************************************************************

' Function/Subroutine Name: NavigateBrowser


' Revision history:

'   Description                                                                   Date        Author         

'   ----------------------------------------------------------------------------  --------    --------------------

'   Navigates to the given URL                                 02/02/2019  Sundar Manikam

' Added step Sendkey "{TAB}" to get focus off the address field CC01   05/24/2020  Sundar Manikam

' Added call to Function setBrowserZoomTo100   3/29/2021   Sundar Manikam

'****************************************************************************************************************************************

'

'@Description Navigates to the given URL

Sub NavigateBrowser (objBrowser, URL)

If URL = "" Then

Exit Sub

End If

'WriteLogLine "NavigateBrowser",URL

On Error Resume Next

objBrowser.Navigate URL

Sendkey "{TAB}" 'CC01

If Err.Number <> 0 Then

ReportEvent micFail, "Navigating to " & URL & " Failed", Err.Description

Else

ReportEvent micDone, "Browser Navigation", URL

End If

setBrowserZoomTo100

End Sub


RegisterUserFunc "Browser", "Navigate", "NavigateBrowser"

'

'****************************************************************************************************************************************



'****************************************************************************************************************************************

' Function/Subroutine Name: DeleteBrowserCookies


' Revision history:

'   Description                                        Change #                   Date        Author         

'   ----------------------------------------------------------------------------  --------    --------------------

'   Deletes browser cookies for Chrome and IE               12/21/2020  Sundar Manikam

' added VBS code to delete IE cookies CC01   01/12/2021  Sundar Manikam

' 'Verify the Delete Browsing History window appears  CC02   01/28/2021  Sundar Manikam

' and activate it

'****************************************************************************************************************************************

'

'@Description Deletes browser cookies for Chrome and IE


Sub Delete_Browser_Cookies(objBrowser, YesNo)

If Not IsTrue(YesNo) Then

Exit Sub

End If

WebUtil.DeleteCookies 

'WriteLogLine "Delete_Browser_Cookies",YesNo

' browserType = objBrowser.GetROProperty("application version")

'

' objBrowser.highlight 'Make sure browser is in the foreground

' objBrowser.ClearCache

' 'Delete Chrome Cookies

' If InStr(1, browserType, "Chrome") > 0 Then

'

' Set oDelCookies = CreateObject("WScript.Shell")

' oDelCookies.SendKeys "^+{DELETE}" '^ for Ctrl key, + for Shift key

' wait 2

' oDelCookies.SendKeys "{TAB 8}" 'You need to tab 8 times to reach ClearBrowsingData button

' wait 1

' oDelCookies.SendKeys "{ENTER}"

' Set oDelCookies = Nothing

'

' ReportEvent micDone, "Deleted Chrome browser cookies", "Deleted Chrome browser cookies"

'

' CloseBrowserTab "Settings"

'

' 'CC01 - Delete IE Cookies

' ElseIf InStr(1, browserType, "internet") > 0 Then 

'

' Set oDelCookies = CreateObject("WScript.Shell")

' oDelCookies.SendKeys "^+{DELETE}" '^ for Ctrl key, + for Shift key

' wait 2

'

' 'CC02 - Verify the Delete Browsing History window appears and activate it

' If Not Browser("CreationTime:=0").Dialog("text:=Delete Browsing History","nativeclass:=#32770").exist(1) then

' ReportEvent micWarning, "Internet Options Failed to Appear", "Internet Options Failed to Appear"

' Exit Sub

' End If

'

' Browser("CreationTime:=0").Dialog("text:=Delete Browsing History","nativeclass:=#32770").Activate

'

' oDelCookies.SendKeys "+{TAB}"

' wait 2

' oDelCookies.SendKeys "{ENTER}"

' wait 2

'

'' Set WshShell = CreateObject("WScript.Shell") 'CC01

'' WshShell.run "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8"

''

'' 'To clear browsing cookies

'' WshShell.run "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255"'2"

' ReportEvent micDone, "Deleted Internet Explorer browser cookies", "Deleted Internet Explorer browser cookies"

' End If

'

' 'wait for cookies to be deleted

' Wait 5

End Sub


RegisterUserFunc "Browser", "DeleteCookies", "Delete_Browser_Cookies"

'

'****************************************************************************************************************************************



'****************************************************************************************************************************************

' Function/Subroutine Name: CloseAllBrowsers

'

'

' Revision history:

'   Description                                                                   Date        Author         

'   ----------------------------------------------------------------------------  --------    --------------------

'   Original Code                                                                 01/02/2019  Sundar Manikam

'   modified code to improve readability including variable names, fixed memory   02/02/2020  Sundar Manikam 

'     leaks, and changed reporting status to micInfo

' Added calls to HandleRandomErrors (False) CC01   06/22/2021  Sundar Manikam

'

'*************************************************************************************************************************************

'

'@Description Closes all instances of Internet Explorer


Function CloseAllBrowsers ()

    

    'WriteLogLine "CloseAllBrowsers",""

    

    ReportEvent micDone, "Closing All Browsers", "Closing All Browsers, except ALM (Application Lifecycle Management)"

    

    On Error Resume Next

    

    '-----------------------------------------------------------

    'Close the Browser Windows

    '-----------------------------------------------------------


    set browserDesc = Description.Create()

    

    browserDesc("micclass").Value = "Browser"

    

    set browsers = Desktop.ChildObjects(browserDesc)

    

    browsersCount = browsers.Count

    

    for i = 0 to browsersCount - 1

    

        sBrowserName = browsers(i).GetROProperty("Name")

        sBrowserTitle = browsers(i).GetROProperty("Title")

        'strCompleteBrowserName = browsers(i).GetROProperty("application version")

        

        If sBrowserName <> "" or sBrowserTitle <> "" Then

        

            isNotALMBrowser = (Instr(1, sBrowserName, "HP ALM") = 0)

            

            If isNotALMBrowser Then'Not(IsEmpty(sBrowserName)) Or Not(IsEmpty(sBrowserTitle)) And isNotALMBrowser then

            

                msg = "Close Browser " & sBrowserName

                

                ReportEvent micDone, msg, msg

                

               'browsers(i).highlight ' for dev/debugging

               'WriteLogLine "CloseAllBrowsers",sBrowserName & " " & sBrowserTitle

                browsers(i).Close

                

                Wait 2

                

                'CC01

                HandleRandomErrors (False)

                

                If Instr(1, sBrowserName, "Object reference not set to an instance of an object") > 0 Then

                    On Error GoTo 0

                    

                    set browserDesc = nothing

                    set browsers    = nothing

                    

                    Exit Function

                    

                End If ' Instr(1,sBrowserName,"Object reference not set to an instance of an object") > 0 Then

            End if ' Not(IsEmpty(sBrowserName)) And isNotALMBrowser then

        End If ' sBrowserName <> "" Then

        

    Next ' for i = 0 to browsersCount - 1


    set browserDesc = nothing

    set browsers    = nothing


    '-----------------------------------------------------------

    'Close the Browser Windows not recognized as Web Browsers

    '-----------------------------------------------------------


    Set windowDesc = Description.Create()

    

    windowDesc("micclass").Value    = "Window"

    windowDesc("nativeclass").Value = "IEFrame"

    


    set windows = Desktop.ChildObjects(windowDesc)

    

    browsersCount = windows.Count

    

    for i = 0 to browsersCount - 1

    

        sBrowserName = windows(i).GetROProperty("text")

    

        If sBrowserName <> "" Then

        

            If Not(IsEmpty(sBrowserName)) then

            

                isNotALMBrowser = (Instr(1, sBrowserName, "ALM") = 0)

                

                If isNotALMBrowser Then

                

                    ReportEvent micDone, "Close Browser", sBrowserName

                    windows(i).Close

                    'CC01

                    HandleRandomErrors (False)

                    

                End If ' isNotALMBrowser Then

            End If '  Not(IsEmpty(sBrowserName)) then

        End If ' sBrowserName <> "" Then

        

    Next 'for i = 0 to browsersCount - 1

    

    Set windowDesc = nothing

    

    On Error GoTo 0

    

End Function ' CloseAllBrowsers ()

'

'****************************************************************************************************************************************



'****************************************************************************************************************************************

' Function/Subroutine Name: CloseBrowserTab

'

' Revision history:

'   Description                                                                   Date        Author         

'   ----------------------------------------------------------------------------  --------    --------------------

'   Original Code                                                                 01/02/2019  Sundar Manikam

'   modified code to improve readability including variable names, fixed memory   02/02/2020  Sundar Manikam 

'     leaks, and changed reporting status to micInfo

'

'****************************************************************************************************************************************

'

'@Description Closes a specified instance of Internet Explorer based on the browser tab name.


Function CloseBrowserTab (browserTabName)


    If browserTabName = "" Then

        Exit Function

    End If

    

    'WriteLogLine "CloseBrowserTab",browserTabName

    

    set browserDesc = Description.Create()

    

    browserDesc("micclass").Value = "Browser"

    

    set browsers = Desktop.ChildObjects(browserDesc)

    

    browsersCount = browsers.Count

    

    for i = 0 to browsersCount - 1

    

        sBrowserName = browsers(i).GetROProperty("name")

        

        browserFound = Instr(sBrowserName, browserTabName)

        

        If browserFound Then

        

            msg = "Close Browser " & sBrowserName

                

            ReportEvent micInfo, msg, msg

            

            browsers(i).highlight

            browsers(i).Close

        else

            msg = "Browser """ & browserTabName & """ Not found."

            

            ReportEvent micWarning, msg, msg

        

        End If '  browserFound Then

    Next ' for i = 0 to browsersCount - 1

    

    set browserDesc = nothing

    set browsers    = nothing

    

End Function ' CloseBrowserTab (pBrowserName)

'

'****************************************************************************************************************************************



'****************************************************************************************************************************************

' Function/Subroutine Name: CloseBrowserTabOptional

'

' Revision history:

'   Description                                                                   Date        Author         

'   ----------------------------------------------------------------------------  ----------  --------------------

'   Optionally closes the browser based on input of YesNo                         01/02/2021  Sundar Manikam

'

'****************************************************************************************************************************************

'

'@Description Closes the browser based on input of YesNo.


Function CloseBrowserTabOptional (objBrowser,YesNo)


    If Not IsTrue(YesNo) Then

        Exit Function

    End If

    

    'WriteLogLine "CloseBrowserTabOptional",YesNo

    

    browserName = objBrowser.ToString

    

    If Not ObjectExist(objBrowser) Then

    ReportEvent micFail, browserName & " does not exist", browserName & " does not exist"

    Exit Function

    End If

    

    On Error Resume Next

    ObjBrowser.Close

    

    If Err.Number <> 0 Then

    ReportEvent micFail, "Failed to close the " & browserName,"Failed to close the " & browserName

    Else

    ReportEvent micDone, "Closed the " & browserName, "Closed the " & browserName

    End If

    

    On Error GoTo 0

    

End Function ' CloseBrowserTabOptional  (objBrowser,YesNo)


RegisterUserFunc "Browser", "CloseOptional", "CloseBrowserTabOptional"

'

'

'*** CommonFunction Ends ****************************************************************************************************************

'

'

'****************************************************************************************************************************************

'****************************************************************************************************************************************




1. Main Script Calls:

'~~~~~~~~~~~~~~~~~~~~


'*** Main Script Starts *****************************************************************************************************************

' Script History :

'****************************************************************************************************************************************

'

RunAction "UI Login [UI Login]", oneIteration

RunAction "Verify Account Overview [Verify Account Overview]", oneIteration

RunAction "Edit Account [Edit Account]", allIterations

'

'*** Main Script Ends *******************************************************************************************************************



2. Action Script Name: UI Login:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

'*** Action Script Starts ***************************************************************************************************************

'****************************************************************************************************************************************

' Action Name: UI Login '### NJPower Me Login

' Action Summary: Invokes the browser, enters the url for the NJPower customer website (https://sit.NJPower.me), 

'

' Action Details:

'   Invokes a browser

' navigates to the url

' selects a region (optional)

' Logs in (optional)

'

' Initial Condition: the NJPower.me website is closed

'

' End Condition: The NJPower.me website is open and optionally logged into

'

'  

' Revision history:

'   Description                                                               CC#  Date      Author         

'   ------------------------------------------------------------------------------- --------  --------------------

'   Original Code                                                                  01/02/19  Sundar Manikam

' Handle the Continue to Site link   CC01 02/28/19  Sundar Manikam

' 'Only select the region if it is different than the one already selected  CC02 03/12/19  Sundar Manikam

' Get the Environment from the UI Environment.xml file   CC03 04/04/19 Sundar Manikam

'   If login page already exist skip launch browser   CC05 05/08/19 Sundar Manikam

' Report the browser and environment the test is running in   CC06 05/12/19 Sundar Manikam

' Added Case "PAT" URL = "https://pat.NJPower.us/content/"     06/19/19 Sundar Manikam

'****************************************************************************************************************************************


'************ CC03 Get the Environment from the UI Environment.xml file *****************


'Begin CC04

'UI_Env = ImportRunTimeData("UI Environment","Environment")

'Environment("BrowserName") = ImportRunTimeData("UI Environment","BrowserName")


strLocalHostConfig = ImportRunTimeDataNew(testEnv, browserName)

StrSplit = Split(strLocalHostConfig, " ", -1, 1)

strTestEnv = StrSplit(0)

strBrowserName = StrSplit(1)


'Import the data file if environment is SIT

If LCase(strTestEnv) = "SIT" Then

Datatable.Import "\\nu.com\data\SharedData\Merger_Temp\Common\Projects\_Standards and Process\Automation\CommonUftAssets\SharedTestData\UI Input Data\" & strTestEnv & "\" & Environment("TestName") & ".xls"

End If


If Err.Number <> 0 Then

reportevent "criticalfail", "Error importing data file. Check the path and file name", Err.Description

End If


'CC06 - Report the browser and environment this test is running in

ReportEvent micInfo, "Execution Browser: " & strBrowserName, strBrowserName

ReportEvent micInfo, "Execution Environment: " & strTestEnv, strTestEnv


execute(getActionParameters ()) 


' Only execute the action if there is data in the local data table for the current iteration

If Not runThisAction Then

    ExitAction

End If


'--------------------------------------------------------------------------------------------------


msgHeader = "Begin Action: " & Environment("ActionName")

msgDetail = RowInformation


ReportEvent micInfo, msgHeader, msgDetail


'--------------------------------------------------------------------------------------------------

' Action/test/business process specific statements

'--------------------------------------------------------------------------------------------------



'If UI_Env <> "" Then

'Select Case LCase(UI_Env)

Select Case LCase(strTestEnv)

Case "tst"

URL = "https://tst.NJPower.xyz/"

Case "uat"

URL = "https://uat.NJPower.us/"

Case "SIT"

URL = "https://SIT.NJPower.me/content/"

Case "PAT"

URL = "https://pat.NJPower.us/content/"

Case Else

'ReportEvent "criticalfail", "Environment " & UI_Env & " Not Acceptable", "Possible Environments: TST, UAT, SIT Please check the MoveIn Environment xml file."

ReportEvent "criticalfail", "Environment " & strTestEnv & " Not Acceptable", "Possible Environments: TST, UAT, SIT Please check the UI Environment xml file."

End Select

'End If


'If BrowserName <> "" Then

' OpenBrowser Environment("BrowserName")

'End If

'End CC04



'CC05

If IsTrue(LoginPageAlreadyExist) Then

Browser("MyHome Origin").Page("MyHome Origin").WebEdit("User ID").Set UserID

Wait 1

Browser("MyHome Origin").Page("MyHome Origin").WebEdit("Password").Set Password

Browser("MyHome Origin").Page("MyHome Origin").WebButton("Log In").ClickOptional ClickLoginBtn

Else


OpenBrowser strBrowserName

Browser("MyHome Origin").Navigate URL

'Account for the SiteFinity page appears

If Browser("MyHome Origin").Page("MyHome Origin").Link("Continue").Exist(3) Then

If Browser("MyHome Origin").Page("MyHome Origin").Link("Continue").GetROProperty("visible") Then

Browser("MyHome Origin").Page("MyHome Origin").Link("Continue").Click

End If

End If

If Browser("MyHome Origin").Page("MyHome Origin").Link("Continue to Site").Exist(4) Then

Browser("MyHome Origin").Page("MyHome Origin").Link("Continue to Site").Click

End If

' verify the NJPower Login page loads

Browser("MyHome Origin").Page("MyHome Origin").Image("NJPower").VerifyPageExist PageNameDescription

'Select the region if it is already selected

If Region <> "" Then

regionSelectionVisible = Browser("MyHome Origin").Page("MyHome Origin").WebElement("select your region below").GetROProperty("visible")

If Not regionSelectionVisible Then

'CC02 - Only select the region if it is different than the one already selected

curRegion = Browser("MyHome Origin").Page("MyHome Origin").WebElement("CurrentServiceArea").GetROProperty("innertext")

If LCase(curRegion) <> LCase(Region) Then

Browser("MyHome Origin").Page("MyHome Origin").WebElement("CurrentServiceArea").Click

Browser("MyHome Origin").Page("MyHome Origin").WebElement("select your region below").WaitProperty "Visible",True,"30000"

End If

End If

End If

If Browser("MyHome Origin").Page("MyHome Origin").WebElement("select your region below").GetROProperty("visible") Then

Browser("MyHome Origin").Page("MyHome Origin").ClickLink Region

End If

'Respond "NO" to the Survey question

If Browser("MyHome Origin").Page("MyHome Origin").Image("invitation1").Exist (3) Then

Browser("MyHome Origin").Page("MyHome Origin").WebArea("no").ClickOptional Window("Internet Explorer").WinObject("Internet Explorer_Server"), "click"

End If

'CC01 - Handle the Continue to Site link

If Browser("MyHome Origin").Page("MyHome Origin").Link("Continue to Site").Exist(1) Then

Browser("MyHome Origin").Page("MyHome Origin").Link("Continue to Site").Click

End If 

Browser("MyHome Origin").Page("MyHome Origin").WebEdit("User ID").Set UserID

Wait 1

Browser("MyHome Origin").Page("MyHome Origin").WebEdit("Password").Set Password

Browser("MyHome Origin").Page("MyHome Origin").WebButton("Log In").ClickOptional ClickLoginBtn


End If



Browser("MyHome Origin").Page("MyHome Origin").VerifyMessages ExpMessages


If VerifyGoPaperless="" Then

If Browser("MyHome Origin").Page("MyHome Origin").WebElement("GoPaperless_Close").Exist(1) Then

Browser("MyHome Origin").Page("MyHome Origin").WebElement("GoPaperless_Close").click

End If

End If


Browser("MyHome Origin").Page("MyHome Origin").WebMenu("WebMenu").Select MenuItemSelect '"My Account"

Browser("MyHome Origin").Page("MyHome Origin").ClickLink LinkToClick


Browser("MyHome Origin").Page("MyHome Origin").VerifyMessages ExpMessages2



'*** Action Script Ends *****************************************************************************************************************


'****************************************************************************************************************************************

No comments:

Post a Comment