Wednesday, January 25, 2017

AUTOMATION 26 - Design patterns for automation

Technical level: *****

Today's article assumes a familiarity with ideas such as methods, privacy, JUnit tests and objects that was covered in my Java series.

The developer gurus I work with are really keen on design patterns, and often consider understanding them more important than knowing about the details of a language.

A design pattern is a set way of approaching a standard problem.  There are a couple of them floating around for Selenium, but one of the most important ones I've heard about is the page object design pattern.  There will be extensive references at the bottom of the page for further reading.

I'm going to go through an example of how I've analysed how to create automation.  You might take a slightly different approach, but hopefully you'll find the discussion of my approach useful enough to understanding what you need to consider.

First of all, in page object design, you're going to create an object for every page in your application.  This "page object" will know all about the which, what and how of the page,

  • Which elements are on the page.
  • What their state is.
  • How to perform actions on the page


In addition, it's generally a good idea to have the checking that performs pass/fail in your automation separate to the page object itself.  The page object will handle everything about doing things on the page, but will pass out information on the page to this test layer, so it's the test layer which performs the checks.

Our example ... Twitter

Okay, so we're back with Twitter again (I use this a lot).  Every page on Twitter can be turned into a page object.

For simplicity we'll say for now that there's just two.  The main feed page below,


And the login and registration page here,


You need to create objects for both, with a third layer to contain your JUnit tests.  Again we covered JUnit tests extensively in our Java series.

To make life easier, we're going to focus in and do the analysis on login and registration, and focus on this part,



Which elements are on the page "LOCATORS"

Richard Bradshaw's article on page object design here calls the kind of functionality we're going to look at now "locators", and I just love that term for describing this.

We need to create function calls to define all the page elements on the page we'll want to use.  We could do this by either defining WebElements inside the page object, or having methods which return the WebElement.

We need to choose one or the other, and stick to it.  Most importantly we want these to be private.  Only our page object should want to locate the WebElement directly.  The "what" and "how" parts of the object we design will allow external parts of the system to interface with these elements as needed.

Lets do the analysis, we have the following ...

Text fields,

  • Phone, email or username
  • Password
  • Full name
  • Email
  • Password


Buttons

  • Log in
  • Sign up for Twitter


Check boxes

  • Remember me


Links

  • Forgot password?


We need to define them all.  There's also an additional one we might want to define, you can get an error message ...




What their state is "STATE"

This is a bit trickier, but you might want to return the state of the page.  These methods should be public, and will be used by your JUnit tests.  They are similar to the idea of "getters".


So examples might be,

  • Get page content.  Return the text on the entire page.  So the JUnit test can check for certain content.
  • Get error message box state.  Return true if the error message box is displayed.
  • Get error message box content.  Return the text inside the error message box.


How to perform actions "ACTIONS"

My team call these flows.  Rather than just a single action, they're a sequence of actions.  Obviously these methods would need to be public.

If we look at our chosen page, there are three flows ...

Flow 1 - login

  • Enter username
  • Enter password
  • Select/deselect remember me
  • Select log in button


Flow 2 - register

  • Enter full name
  • Enter email
  • Enter password
  • Select sign up for Twitter


Flow 3 - forgot password

  • Select forgot password link


Notice how if this was turned into a test script, all these flows relate to the "action" not "expected result" part of a test script table.  This is intentional, as the part that checks expected result should be an assertion within the JUnit method.



And finally...

The JUnit method can string together sequences of these page object, to create a series of tests.

Let's look at login, we can call that perform login method in multiple tests.  We can leave some fields blank, we can give it correct data, we can give it false data.  Then we should use an assertion within our JUnit to find out if the system is right or now,

  • If we give the correct data, we should find the words "Home", "Moments", "Notifications" on the page.
  • If we give a field as blank, we should get an error message
  • If we give wrong details, we should get an error message


This diagram should explain the relationship (and I'm so sorry, the colour scheme seemed a good idea at the time) ...


The key thing is this approach follows two key rules of software development.  Using methods, which we discussed yesterday.  But also encapsulation, only giving access to items that another level really needs.


Further reading

There's a lot to read about, so this section contains some useful next steps ...

8 comments:

  1. Crisp and Precise post.I want understand state more....State is mostly reflecting the text of the page based on the error message we recieved(in this context).Any other example of state that will be helpful for me to understand more.Thanks

    ReplyDelete
    Replies
    1. It's a great question Keshav. A STATE method should be any information you need to return about your page to your JUnit checks.

      A great example could be an "am I on the page". This could check several things - maybe the URL path or for specific text on the page. But importantly it would return TRUE if those checks pass, or FALSE if it believes that you are not on the page.

      Another example might be you could have a message box area, which you want to return the textual context of. You might want to return data from a specific field. Here's an example, you might have a field which takes data and capitalises everything entered, so you want to enter in "mike", but you need a STATE method to return the content to check in your JUnit assertion that it was turned into "MIKE".

      Hope that helps!

      Delete
  2. Saya ingin berbagi cerita kepada anda bahwa saya IBU HERAWATI seorang TKW dari malaysia dan secara tidak sengaja saya buka internet dan saya melihat komentar IBU DARNA yg dari singapur tentan AKI SYHE MAULANA yg telah membantu dia menjadi sukses dan akhirnya saya juga mencoba menghubungi beliau dan alhamdulillah beliau mau membantu saya untuk memberikan nomor Togel/lottrey toto 4D dr hasil ritual/ghaib melalui jarak jauh dan alhamdulillah itu betul-betul terbukti tembus dan menang RM.230.000 Ringgit ,kini saya kembali indon membeli rumah dan kereta walaupun sy Cuma pembantu rumah tanggah di selangor malaysia, sy sangat berterimakasih kepada AKI SYHE MAULANA dan tidak lupa mengucap syukur kepada ALLAH karna melalui AKI MAULANA saya juga sudah bisa sesukses ini. Jadi kawan2 yg dalam kesusahan jg pernah putus asah, kalau sudah waktunya tuhan pasti kasi jalan asal anda mau berusaha, ini adalah kisah nyata dari seorang TKW, AKI MAULANA adalah guru spiritual terkenal di indonesia. jika anda ingin seperti saya kunjungi situs/website KLIK DISINI RITUAL PESUGIHAN DUNIA GHAIB



    SUCCESS STORY OF HERAWATI MOTHER

    I want to share the story to you that I am HERAWATI MOTHER a TKW from malaysia and accidentally I open internet and I see DARNA MOTHER comment from singapur about AKI SYHE MAULANA who has helped him become successful and finally I also tried to contact him and alhamdulillah he Want to help me to give the number Togel/lottrey toto 4D dr ritual results / unseen and alhamdulillah it really proved translucent and won RM.230.000 Ringgit, now I am back indon buying house and train although sy Just a housemaid at selangor malaysia, sy very thankful To AKI SYHE MAULANA and do not forget to give thanks to ALLAH because through AKI MAULANA I also can be as successful as this.So kawan2 who in distress jg ever break down, when it's time god must be the road of origin you want to try, this is the true story of a migrant worker, AKI MAULANA is a famous spiritual teacher in Indonesia. If you want to like me visit the website CLICK HERE RITUAL GHAIB TEMBUS LOTTREY


    ReplyDelete
  3. Hi Dear!!!
    This Is My First Post.
    We Are a Web Service Provider,
    Online Casino Play Baccarat Online Casino Via Mobile Phone, IPhone, IPad and Roulette Slots...etc "
    PLZ, come & join with us, Thanks for your many tutorials!

    จีคลับ
    goldenslot
    gclub online casino

    ReplyDelete
  4. Terimakasih banyak AKI karna melalui jalan togel ini saya sekarang sudah bisa melunasi semua hutang2 orang tua saya bahkan saya juga sudah punya warung makan sendiri hi itu semua berkat bantuan AKI JAYA yang telah membarikan angka 4D nya menang 275 jt kepada saya dan ALHAMDULILLAH berhasil,kini saya sangat bangga pada diri saya sendiri karna melalui jalan togel ini saya sudah bisa membahagiakan orang tua saya..jika anda ingin sukses seperti saya hubungi no hp O85-244-015-689 AKI JAYA,angka ritual AKI JAYA meman selalu tepat dan terbukti..silahkan anda buktikan sendiri. 2D 3D 4D 5D 6D







    Terimakasih banyak AKI karna melalui jalan togel ini saya sekarang sudah bisa melunasi semua hutang2 orang tua saya bahkan saya juga sudah punya warung makan sendiri hi itu semua berkat bantuan AKI JAYA yang telah membarikan angka 4D nya menang 275 jt kepada saya dan ALHAMDULILLAH berhasil,kini saya sangat bangga pada diri saya sendiri karna melalui jalan togel ini saya sudah bisa membahagiakan orang tua saya..jika anda ingin sukses seperti saya hubungi no hp O85-244-015-689 AKI JAYA,angka ritual AKI JAYA meman selalu tepat dan terbukti..silahkan anda buktikan sendiri. 2D 3D 4D 5D 6D






    Terimakasih banyak AKI karna melalui jalan togel ini saya sekarang sudah bisa melunasi semua hutang2 orang tua saya bahkan saya juga sudah punya warung makan sendiri hi itu semua berkat bantuan AKI JAYA yang telah membarikan angka 4D nya menang 275 jt kepada saya dan ALHAMDULILLAH berhasil,kini saya sangat bangga pada diri saya sendiri karna melalui jalan togel ini saya sudah bisa membahagiakan orang tua saya..jika anda ingin sukses seperti saya hubungi no hp O85-244-015-689 AKI JAYA,angka ritual AKI JAYA meman selalu tepat dan terbukti..silahkan anda buktikan sendiri. 2D 3D 4D 5D 6D



    ReplyDelete
  5. Online gambling is packed with all the entertainment.
    จีคลับIt is an online gambling option that is ready to give the user the fun of gambling games in the amazing. New online gambling brings you to the joy of being good. With quality online casino sites and open 24 hours to allow gamblers to enjoy their favorite online gambling every time. Nowadays online lotto is developed to support your fluency. You do not have to download the program because you can gamble on the web directly. The best way to win money online is online. Not only that, you can earn money from every single game, but the odds are very high, depending on your skill and your luck. Can I find a good strategy? Before you risk it so that anyone can bet on it. Play with the money with a new online game with a service without interruption. Easy to play and access to our online games are many channels. Online Gambling Betting is fun and exciting with all the risky games. บาคาร่า

    ReplyDelete
  6. Casinos that support all your play.
    Goldclub Slot Our online casino site is an online gambling game. And most importantly, it is the site that knows the desire of the gambler as well. Online gambling games in modern times is considered to be a very prosperous casino industry because it is because gamblers from all over the world favor and like to play gambling. Online It is easy to access and play and there are also many online gambling games to choose from. Can be played easily. And there are interesting playing styles. And playing online casino games with online casino is also considered a new way. Gamblers can access and play all kinds of online gambling games. Just by the Internet. Every time you have internet, you can play all online casino games with online casino websites. The convenience and excitement of the online casino site is a fun never before seen. คาสิโนออนไลน์

    ReplyDelete
  7. I'am glad to read the whole content of this blog and am very excited.Thank you.

    ดูหนัง

    ReplyDelete