נקודות יציאה מרובות
Content:
הדפס את המאמר
קל מאוד לשלוט בנקודה בה תסריט הבדיקה נכנס ל-Action / פונקציה – אחרי הכל, יש רק אופציה אחת – מלמעלה. שליטה בנקודות היציאה, זה כבר סיפור שונה לחלוטין - ל-Action יכולה להיות נקודת יציאה אחת, שתיים, או עשר, הכל בהתאם ללוגיקה הפנימית והמבנה שלה. היום נבחן איך ניתן לשלוט בנקודות יציאה מרובות ב-Actions ופונקציות, ולהפוך את הקוד שלנו לקל יותר לתחזוקה ותיקון.
כשהתסריט מתקדם כצפוי, זרימת הקוד פשוטה יחסית. גם כשיש בקוד בלולאות, תנאי IF ובחינת מקרים באמצעות Select Case, הקוד מתבצע בקו ישר יחסית, ומסתיים בשורה האחרונה בתסריט. במקרה כזה יש רק שער יציאה אחד, והכל טוב ויפה. אבל, במקרים בהם התסריט נתקבל בבעיות, כמו במקרה של שגיאת מערכת, או כשל בלוגיקה העסקית של האפליקציה, אין מנוס מיציאה מיידית מהקוד הנוכחי. אחרי הכל, אין שום טעם להמשיך נסות להזין 20 שדות, אם הטופס שמכיל אותם בכלל לא נפתח.
למזלנו, האנשים הנחמדים ב-Mercury (והיום HP) הבינו את הצורך ביציאה מיידית מ-Action, והכינו עבורנו את פקודות ExitAction ו-ExitActionIteration. לרוב השימוש בהן נראה כך:
‘.Action Code
If CritialCondition = False Then ExitAction
‘.Continue Action
If CritialCondition = False Then ExitAction
‘.Continue Action
אבל זאת דרך מאוד לא ברורה לצאת באמצע ביצוע משימה. רצוי שנוסיף כתיבה ללוג:
If CritialCondition = False Then
Reporter.ReportEvent MicFail, "Something terrible has happened", "Aborting"
ExitAction
End If
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
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
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
‘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
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
ניווט במאגר הידע



July 20th, 2010 at 6:13 am
cheap cialis :-DD ultram online drugs %) phentermine online 15864 phentermine online xjkb cialis generic krsln
July 21st, 2010 at 5:46 pm
health insurance bche online auto insurance quotes llym car insureance yov auto insurance =)
July 29th, 2010 at 9:32 pm
accutane online :-(( Tramadol online 564324 xanax wwsciu buy accutane online 8-D
July 30th, 2010 at 9:08 am
phentermine =DDD buy xanax online :-[ accutane tqlc accutane generic 916 what does generic xanax look like 8[[
July 31st, 2010 at 7:10 am
buy tramadol online 939 buy prednisone online 9647 propecia %DD accutane buy %-(
August 4th, 2010 at 2:58 pm
buy tramadol online 32280 bill consolidation buy tramadol %))) cialis online zatqoa prednisone online qcvbjn
August 6th, 2010 at 3:59 pm
valium online starlight 55452 buying acomplia buuj ordering cialis online =DD valium online iamyp
August 10th, 2010 at 10:38 pm
ambien iuoog prednisone 54353 purchase valium from usa 243 ambien rx :-OO
August 22nd, 2010 at 1:07 am
buying accutane wyzcpt phentermine 92969 phentermine fxql prozac nation 988
August 25th, 2010 at 6:58 am
buy propecia on line consultation 384647 Valium oxucp valium 0196 levitra delivered 8-]] overnight phentermine %-[[
August 25th, 2010 at 9:52 am
tramadol 3153 valium =PPP buy propecia =( ambien %-)