Thursday, September 15, 2016

לרכוב בלי גלגלי עזר

Riding without training wheels

(English first, as this is also a response to a source whose owner probably does not speak Hebrew)

I'm assuming you have at least heard of Gem Hill's podcast let's talk about tests baby and if you didn't, go and listen to a few episodes - they are short and I usually find them interesting.  I've listened to the episode on using selenium IDE. It just so happens that on the same day I've listened to it, I also attended a local selenium meetup (which, in case you live in Israel, or just happen to be in the neighborhood when one is occurring, can be found here) and after the meetup a few of us grabbed a cup of coffee (tea in my case) and continued to chat for a bit. One of the subjects that came up during this chat was about record and playback tools. If you've read this post you can probably guess that I feel very strongly against this kind of tools.
Listening to Gem's podcast, I noted that I was listening to the rare occasion where the person using selenium IDE1 is doing it properly with the right goals in mind - create a base, use the recording to flesh out some crude scaffolding quickly and then go and fix the generated code. Then, after the chat I had following the meetup, I thought about it a bit more, and realized I don't like selenium IDE even when it's used responsibly (Though, if I understood correctly, Gem's done it right - use once or twice to bootstrap the code, then defenestrate).
Why? Because it's like riding with training wheels.
When I was young, like all of the kids around my age, I had a bicycle with training wheels, and I rode them around. At some point, came the time for me to remove them, and my father spent several hours running behind me, holding the bicycle rack and releasing it when I wasn't noticing. If you've been anywhere near bicycles lately, you probably know that the new fashion on teaching kinds to ride is by having them ride a balance bike - the reason is simple: When using training wheels the kid might get used to using the pedals and the breaks, but will miss the most important thing - learning to self balance on the bicycle. You also acquire some bad habits you will need to get rid of when you'll later learn to properly ride a bike (for instance, the way you turn around is completely different). Finally,  removing the training wheels is quite scary.

The same is true for using selenium IDE - it helps you do something that looks a lot like writing an automated check, and using it you are able to get some results that are (at least at the beginning) better than not using it, but by using it, you are missing the central skill that is required to write decent automated checks - which is the ability to think in algorithms and organize code in a reasonable way.  And just like training wheels, it can be very scary to let go of it. Worse - using it as a learning tool leads to learning some bad habits - such as having each script live in it's own world, initialize everything for itself and so on. In fact, if we were to convert a recorded script to fit into a proper test infrastructure, not a single line of code will remain - initialization and teardown code will be in the superclass, every selenium code will be in the page-objects, and chances are that even page-objects will be hidden beneath another abstraction level. A bad habit that is a bit harder to shrug off is that by using Selenium IDE we are getting used to try and mimic human actions with our automation and completely ignore the fact that humans and computers are different - and things that are easy for a human being are sometimes difficult, or impossible, for a computer - and vice versa. In addition, recorded scripts are usually quite meaningless in terms of what they actually validate.

So, what should we do? To answer that I want to go back to my bicycle example - How do I like to teach riding a bicycle? I got to do that several times in the past - teaching my cousins, and a couple of friends closer to my age that managed to grow up without learning to ride. What I did was to start from the running point, but instead of holding the bicycle from behind, I run side by side with them, holding them by the shoulder and balancing them until they start doing that themselves. It takes about two hours of doing that before they can ride without my help, and then we go on to the more complicated parts of getting on and off the bike. All in all, after four hours of training, we can ride side by side for a short trip. An equivalent behavior will be learning with a mentor - strong style pairing seems to fit well this image, assuming that there is some basic coding knowledge to begin with (otherwise, focusing on at least some basic coding skills should be the first thing to learn). Lacking a mentor around, I guess you could start by copying some template of "how to write a selenium test in language X" from an online tutorial (there are dozens of them). Though, if you don't have a mentor available to help you, feel free to send me a message (there should be a "contact me" form on the left, or you could leave here a comment) and I'll try to help you getting started - though if you are using a language I'm not familiar with, my help will be limited.





1 Just one note - I use here "selenium IDE" quite often, but if you are actually using selenium IDE, you might want to check out "Selenium Builder", since IDE is planned to reach end-of-life for at least 3 years now. 




-----------------------------------
אני מניח שמי שקורא כאן מכיר את הפודקאסט של ג'ם היל let's talk about tests baby, ואם לא - לכו להקשיב לכמה פרקים. הם קצרים, ואני חושב שרובם מעניינים למדי ונעימים להקשבה. האזנתי לפרק האחרון שהתעסק בשימוש בסלניום IDE, ולגמרי במקרה, באותו היום גם השתתפתי במיטאפ סלניום בארץ (אם טרם הגעתם לאחד - למה?). אחרי המפגש נשארנו (לא כולם) לדבר קצת על כוס קפה, ואיכשהו גם כאן השיחה הגיעה לדבר גם על סלניוםIDE. אם יצא לכם לקרוא את הפוסט הזה אתם יודעים מה דעתי על כלי הקלטה. 
תוך כדי הקשבה לפרק, שמתי לב שבאופן שנדיר להיתקל בו, ג'ם משתמשת בסלניום IDE1 בצורה נכונה - היא מקליטה בסיס כלשהו, מייצאת אותו לקוד, ואז עורכת אותו אחרי שיש לה שלד ראשוני, והיא מודעת לחלוטין לכך שזה רק כלי זמני לצורכי לימוד. אבל, אחרי השיחה בערב הבנתי שאני מתנגד אפילו לשימוש הזה בכלי הקלטה. 
למה? כי זה כמו לרכוב על אופניים עם גלגלי עזר. 
כשהייתי ילד היו לי, כמו לכל הילדים בסביבה, אופניים עם גלגלי עזר שאפשרו לי לרכוב מסביב בעצמי. בשלב כזה או אחר הגיע הזמן להוריד את גלגלי העזר ואבא שלי בילה כמה שעות טובות בריצה אחרי כשהוא מחזיק את הסבל ומשחרר אותו בכל הזדמנות בה לא שמתי לב. אם יוצא לכם להסתובב בקרב חובבי אופניים אתם כבר יודעים שהטרנד היום הוא ללמד ילדים בעזרת אופני הליכה. הסיבה לאופנה הזו פשוטה - כשלומדים לרכוב עם גלגלי עזר לומדים להתרגל לרעיון של דיווש, אולי גם מחזקים שרירים מתאימים, אבל מחמיצים לחלוטין את הכישור המרכזי שנדרש ברכיבה על אופניים - היכולת לאזן את הגוף בזמן רכיבה. בנוסף, גם רוכשים כמה הרגלים מהם יהיה צורך להיפטר בהמשך (למשל, הצורה בה פונים בעזרת הכידון על אופניים עם גלגלי עזר שונה לחלוטין מאשר ברכיבה רגילה). וכמובן - זה מפחיד להסיר את גלגלי העזר.
אותו הדבר נכון לגבי סלניום IDE - הקלטה עוזרת לכתוב משהו שנראה כמו בדיקה אוטומטית, והתוצאות שמשיגים בעזרת הכלי הזה עדיפות (לפחות בתחילת הדרך) על התוצאות שניתן להשיג בלעדיו. אבל שימוש בכלי הזה מדלג על הכישור הכי חשוב כדי ליצור בדיקות אוטומטיות  - היכולת לחשוב על אלגוריתם ולארגן קוד בצורה פחות או יותר הגיונית. בדיוק כמו גלגלי עזר, גם על סלניום IDE מפחיד לוותר, וגרוע יותר - רוכשים כמה הרגלים מגונים דרך שימוש בכלי הזה, כמו למשל העובדה שכל סקריפט מוקלט מבצע את פעולות האתחול בעצמו וחי בעולם משלו במקום שקוד האתחול ישב במקום אחד מסודר. למעשה, אם ממירים קוד מוקלט לתוך פרוייקט בו התשתיות מסודרות היטב, לא תישאר שורה אחת מתוך הסקריפט המקורי - קוד האתחול והניקיון אחרי הבדיקה יהיו במחלקה ממנה יורשת מחלקת הבדיקה, קוד סלניום יעבור לתוך page-objects (שבעצמם יהיו חבויים בתוך שכבת אבסטרקציה) ומהבדיקה המקורית לא נשאר כבר כלום. הרגל מגונה שקשה יותר להיפטר ממנו הוא שכלי הקלטה נוטים להרגיל אותנו לחקות פעולות משתמש בעזרת הקוד שלנו ולהתעלם מכך שיש דברים שנכון יותר לעשות אחרת בעזרת מחשב. חוץ מזה, בדיקות שנכתבות בעזרת כלי הקלטה נוטות להיות שטחיות וחסרות שיניים בהשוואה לבדיקות שנכתבו ע"י קוד ויכולות להסתכל על דברים שקורים מחוץ לדפדפן (כמו למשל מסד הנתונים).

אז, מה עושים? לבד זה מפחיד, וגלגלי עזר הם לא פתרון טוב.
כדי לענות על זה אני רוצה לחזור למשל האופניים, ולספר איך אני מלמד לרכוב (אני לא עושה את זה באופן מקצועי במיוחד, אבל יצא לי ללמד כמה בני דודים בגיל הנכון, וכמה חברים בני גילי שהצליחו לגדול בלי ללמוד לרכוב) - אנחנו מתחילים קודם כל מהשלב בו אני רץ לידם (לא מאחור, ליד) ומחזיק אותם בכתפיים כדי לאזן אותם עד שיוכלו לעשות את זה בעצמם. בערך אחרי שעתיים הם מגיעים לנקודה בה הם מסוגלים לרכוב בלעדי, ואז אפשר להתחיל לטפל בנקודות הקשות יותר של לעלות על האופניים ולרדת מהם. בסך הכל, אחרי כארבע שעות (על פני שני מפגשים) אנחנו כבר מסוגלים לרכוב יחד לטיול קצר. המקבילה של זה בעולם התכנות היא עבודה עם מדריך אישי, והרעיון של strong style pairing נראה לי מתאים במיוחד, אם יש ידע כלשהו בתכנות שאפשר לבנות עליו. אם אין ידע כזה, אגב, לרכוש אותו יהיה הצעד הראשון שצריך לעשות בכל מקרה - אין משמעות לשימוש בכלי הקלטה לצורכי לימוד אם אי אפשר להבין את התוצרים של הכלי הזה מספיק כדי לשפר אותם. בהיעדר מדריך זמין, אני מניח שאפשר להסתכל על אחד המדריכים ל"איך לכתוב סלניום בשפה X" באינטרנט (יש לא מעט מהם בחוץ, ואני מחבב את האתר של יוני פלנר שמכיל מדריכים בלא מעט שפות תכנות). אבל, אם אין אף אחד שידריך אתכם, אתם מוזמנים להשתמש בטופס הפנייה שנמצא משמאל, או להשאיר כאן הודעה ואשמח לנסות לעזור לכם להתחיל. 



1 אני משתמש בשם סלניום IDE, אבל אם אתם משתמשים בכלי הזה, עשו לעצמכם טובה והעיפו מבט בSelenium builder שאמור להחליף אותו אחרי שהתמיכה בIDE תסתיים, כמו שאומרים שיקרה כבר לפחות שלוש שנים.  

No comments:

Post a Comment