עקב העומס הרב אנו מעדכנים רק את האתר באנגלית - עמכם הסליחה

נקודות יציאה מרובות

ניהול נקודות יציאה מרובות

 
Content:


קל מאוד לשלוט בנקודה בה תסריט הבדיקה נכנס ל-Action / פונקציה – אחרי הכל, יש רק אופציה אחת – מלמעלה. שליטה בנקודות היציאה, זה כבר סיפור שונה לחלוטין - ל-Action יכולה להיות נקודת יציאה אחת, שתיים, או עשר, הכל בהתאם ללוגיקה הפנימית והמבנה שלה. היום נבחן איך ניתן לשלוט בנקודות יציאה מרובות ב-Actions ופונקציות, ולהפוך את הקוד שלנו לקל יותר לתחזוקה ותיקון.
 
כשהתסריט מתקדם כצפוי, זרימת הקוד פשוטה יחסית. גם כשיש בקוד בלולאות, תנאי IF ובחינת מקרים באמצעות Select Case, הקוד מתבצע בקו ישר יחסית, ומסתיים בשורה האחרונה בתסריט. במקרה כזה יש רק שער יציאה אחד, והכל טוב ויפה. אבל, במקרים בהם התסריט נתקבל בבעיות, כמו במקרה של שגיאת מערכת, או כשל בלוגיקה העסקית של האפליקציה, אין מנוס מיציאה מיידית מהקוד הנוכחי. אחרי הכל, אין שום טעם להמשיך נסות להזין 20 שדות, אם הטופס שמכיל אותם בכלל לא נפתח.
 

ניהול נקודות יציאה מרובות ב-Action

 
למזלנו, האנשים הנחמדים ב-Mercury (והיום HP) הבינו את הצורך ביציאה מיידית מ-Action, והכינו עבורנו את פקודות ExitAction ו-ExitActionIteration. לרוב השימוש בהן נראה כך:
 
‘.Action Code
If CritialCondition = False Then ExitAction
‘.Continue Action
 
 
אבל זאת דרך מאוד לא ברורה לצאת באמצע ביצוע משימה. רצוי שנוסיף כתיבה ללוג:
 
If CritialCondition = False Then
            Reporter.ReportEvent MicFail, "Something terrible has happened", "Aborting"
            ExitAction
End If
 
 
בעצם, יש לנו גם כמה אובייקטים שצריך לנקות מהזכרון:
 
If CritialCondition = False Then
            Reporter.ReportEvent MicFail, "Something terrible has happened", "Aborting"
            oFile.Close
            Set oFile = Nothing
            ExitAction
End If
 
 
ורגע, יש מידע שצריך לשתול ב-DataTable:
 
If CritialCondition = False Then
            Reporter.ReportEvent MicFail, "Something terrible has happened", "Aborting"
            oFile.Close
            Set oFile = Nothing
            DataTable("out_EntityID", dtlocalsheet) = sEntityID
            ‘More here
            ExitAction
End If
 
 
אני מניח שתפסתם את הרעיון. במקום פקודה פשוטה, יש לנו קטע קוד מורכב למדי, שמופיע שוב ושוב בכל נקודות היציאה. המשמעות היא שיש לנו קוד כפול שנצטרך לתחזק באופן שוטף. קוד כפול הוא התגלמות זרע השטן בעולם הדיגיטאלי, ואתם ממש לא רוצים משהו כזה בקוד שלכם. באופן רציני יותר, כפילויות קוד כאלו הן מתכון בטוח לבאגים קשים לאיתור, עבודה מיותרת ותסכול, וצריך להמנע מהן בכל מחיר. אחת הדרכים להמנע מכפילויות הקוד היא לארוז אותו במרוכז בפונקציית יציאה. זוהי הפונקציה היחידה ש"מותר לה" (לפחות כפי שאני כותב קוד) להיות בתוך Action, ולא בקובץ חיצוני. להלן דוגמה לשימוש בפונקציה, כמודגם ב-Action Template שלי:
 
Dim sResult      ‘I store result data and values to be reported
                             ‘up the action-call chain
Dim sActionReport        ‘Instead of flooding the log with inner-action messages,
                                          ‘I store them, and report all of them at the exit gate.
                                          ‘Errors are still reported on-the-fly
 
‘Action code goes here
 
‘All the exit gates execute only one command: ActionEnd.
‘It receives two parameters: Boolean for Pass/Fail, string for the exit reason
If CriticalCondition = False Then Call ActionEnd(False, "Reason for exiting")
 
‘Rest of action
 
‘Even the normal successful action exit is managed through ActionEnd,
’so the last line in every action is:
 
Call ActionEnd(True, "Action successful")
 
Sub ActionEnd(bStatus, sReason)
            ‘Report details
            Reporter.ReportEvent MicGeneral, "Inner Action Logs", sActionReport
 
            If bStatus = True Then
                        Reporter.ReportEvent MicFail, "An error has occurred", sReason
            Else
                        Reporter.ReportEvent MicPass, "Action successful", "See inner logs"
            End if
 
            ‘Plant datatable info for action-call chain
            DataTable("out_Status", dtlocalsheet) = bStatus
            DataTable("out_Result", dtlocalsheet) = sResult
            ‘More if needed
 
            ‘Close objects and set to nothing here
 
            ‘Other needed exit code
 
            ExitActionIteration
End Sub
 
 

ניהול נקודות יציאה מרובות בפונקציה

 
כפי שראינו, שימוש בפונקציית יציאה מרכזית מאפשר לשמור על קוד נקי ופשוט, ולמנוע הצפות לוג. טיפלנו בנקודות יציאה מ-Action, אבל מה לגבי יציאה מפונקציה? כמובן שאי אפשר לעשות פונקציית יציאה לפונקציה, אבל ישנו פתרון אלטרנטיבי לבעיה. הפתרון פחות יפה וקשה יותר לתחזוקה מהפתרון ל-Action, ולכן אני ממליץ להשתמש בו רק בפונקציות מסובכות במיוחד, המכילות הרבה נקודות יציאה, וקוד יציאה "עבה". הפתרון מתבסס על פקודת Execute, ולכן אם היא לא מוכרת לכם, אני ממליץ לקרוא עליה בקבצי העזרה של ה-QTP. בשורה התחתונה, פקודת Execute מקבלת מחרוזת, ומבצעת את התוכן שלה כאילו שמדובר בפקודות VBScript. כך שהפקודה Execute "msgbox(2)" תקפיץ msgbox עם המספר 2. להלן דוגמה לשימוש בפתרון בפונקציה ComplexFunc:
 
Function ComplexFunc
            Dim sExitCode
            Dim Result
            Dim oFile         ‘will be FSO textstream
 
            ’separate code lines by vbcrlf or ":"
            sExitCode =     "oFile.Close" & vbcrlf & _
                                    "Set oFile = Nothing" & vbcrlf & _
                                    "ComplexFunc = Result"
                                    ‘More exit code
 
            ‘Function code goes here
 
            ‘Exit Gate
            If CriticalCondition = False then
                        Result = "False, No Connection"
            Execute sExitCode
            Exit Function
End if
 
            ‘More function code
 
            ‘Successful exit
            Execute sExitCode
End Function
 
 
                             

שמור את המאמר לשימוש עתידי

הדפס את המאמר הדפס את המאמר
A PDF Version Of This Post


12 תגובות to “נקודות יציאה מרובות”

  1. Texmob אומר :

    cheap cialis :-DD ultram online drugs %) phentermine online 15864 phentermine online xjkb cialis generic krsln

  2. koolblindguy אומר :

    health insurance bche online auto insurance quotes llym car insureance yov auto insurance =)

  3. TrIpLeXzIbIt אומר :

    accutane online :-(( Tramadol online 564324 xanax wwsciu buy accutane online 8-D

  4. blondieuc אומר :

    phentermine =DDD buy xanax online :-[ accutane tqlc accutane generic 916 what does generic xanax look like 8[[

  5. valetedecopas אומר :

    buy tramadol online 939 buy prednisone online 9647 propecia %DD accutane buy %-(

  6. bebemilch אומר :

    buy tramadol online 32280 bill consolidation buy tramadol %))) cialis online zatqoa prednisone online qcvbjn

  7. n0000bish אומר :

    valium online starlight 55452 buying acomplia buuj ordering cialis online =DD valium online iamyp

  8. bothnia אומר :

    ambien iuoog prednisone 54353 purchase valium from usa 243 ambien rx :-OO

  9. efskater אומר :

    buying accutane wyzcpt phentermine 92969 phentermine fxql prozac nation 988

  10. PinkCreAma אומר :

    buy propecia on line consultation 384647 Valium oxucp valium 0196 levitra delivered 8-]] overnight phentermine %-[[

  11. giltonsena אומר :

    tramadol 3153 valium =PPP buy propecia =( ambien %-)

הוסף תגובה