Best Practices for Page Object Model - UI Automation (Mobile & Web) - Whiteboard Learning

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys this is avidya welcome back to evening automation labs i hope you guys are enjoying this whiteboard learning and thanks for uh giving a good response i'm getting a really good response so that's why i thought of continuing with some really interesting topic so guys today we are going to talk about the best practices for page object model i'm not going to create a page object model but today i'm going to tell you what are the best practices that you have to follow because page object model design pattern is quite famous for any kind of web application or any kind of mobile application that if you are using it so page object model is a design pattern first of all see this you have to understand there is a difference between framework and the design pattern right so with the help of page object model you can include with any kind of framework and you can create one a data driven approach with page object model or hybrid approach with page object order you can do that right guys so page object model is a pattern there are certain guidelines are defined you have to follow certain rules over there so then uh you know process you have to follow certain design you have to follow then only you can achieve a good page object model i see a lot of people they do silly mistakes they don't follow the rules properly they don't follow the guidelines defined okay for page object whatever but see first of all who is defining these rules it's not about that okay yeah i am the owner i'm going to design but there are certain experts are available in the market i'm going to show you those references also that what exactly page object model says and according to these experts right and it's always a debatable topic but you feel after this particular video am i correct or not right so let's start so page object model is a design pattern it's basically designed for ui application ui can be anything it can be mobile it can be your web also because in mobile apps we have different pages in web also we have different pages so it can be applied for both mobile and ui it is not made for api with the help itself you can say that okay with the name itself you can say page object model it means web pages or mobile pages so let's see typically what we say typically we say that okay in page object model there are two important factors one is page one is test right so you have to define your page let's see if i'm talking about java you have to create your page for every page you have to create a separate java class then you have to create a separate test class and then these two classes are communicating with each other and they are we are designing a framework on top of it right this is the main fundamental of uh page object model so let's see i'm going to create two components over here and then we are going to talk about what are different best practices that we have to follow how to create a page class and how to create a test class which is important now let's see a typical application any web application let's see where we have different pages like this so let's see page number one then we have page number two then we have page number three so this is a page layer that i have created i'm going to give some name over here let's see this is my login page dot java class because we know that for every page class we have to create a java class same thing for home page and quickly write home page dot java and let's see this is my some other page let's see this is my search page whatever the page i'm just randomly picking any page name search page whatever i think this marker is not that great let me change it okay perfect now for every page class what we have to be able to create a separate test class so let's see this is my test class then i'm going to create this is my second class and every third test class over here so this is my what login [Music] page test dot java same thing hp test means home page test dot java and this is my let's see what search page test dot java so right so this is the thing that i have created over here perfect now for loginpage.java i'll be using certain tools for test classes i'll be using certain tools and let's see i'm using either you can use if i'm using java essentially also you can use that or you can use junit right place but this concept will remain same for other programming languages if you're using python if you're using pi test or unit test you can use that if you're using uh let's see javascript super jasmine also for watch also same concept will be applied right guys if you're using c sharp m unit okay same concept will be applied the design pattern will remain same so this is a typical structure that we follow now see carefully loginbase.java is having what in loginpage.java we will be defining a couple of important things we will be defining our by locators right with the help of by class or you can use web elements with the help of the defined page factory one so you can define that along with buy locators what we have to do we have to create our page actions same thing you have to create your buy locators and paid actions by locator and many actions over here right so let's see typical by locator we are creating for login page we have username we have password then we have a login button then we have sign up link these are the buy locators that i have created and if i ask you that okay create a page action page action means methods we have to create page method now what do you mean the page methods page actions page actually means which is defining the behavior of the page so page layer this entire layer is this is called a page library for me remember this thing this is a page library a page library right now what do you mean by library library means which is giving you the behavior the application pages behavior or the properties or the methods that what exactly how exactly my page is behaving it means my login page is behaving my home page is behaving my search page is behaving in the form of method i'm going to give that i'm going to give that okay when you call this particular method what is happening with this particular uh page for this particular module for this particular web element i am going to give you i'm going to return you something and then you do whatever you want to do you want to assert it you want to print it or you do whatever you want to do that but so page library is giving you the behavior of the page remember this thing this is the first fundamental of page object model so let's see i have written this thing and now how to create a page action method so let's say i'm going to create a page action method that that let's see for sign up link that method name is i'm going to create something like this that uh sign up link exist or not so this is the method that i have created now this method is going to check that okay hey driver dot find element or buy dot whatever the locator that you have written sign up this particular locator and dot is displayed i'll be using right so this method is giving you what this method is giving you a boolean right it's restrained or not so that's why i'm going to return this thing over here so if i'm returning this is returning what is returning a boolean so i'm writing boolean is a written type of this particular method right and let's make this method as public assume that right so this is a method i have written one page action that i have written similarly i can create one more method let's see my second method i'm going to create that [Music] login method which say that okay hey give me the username or give me the email id and password all right guys so i'm going to create driver dot same thing find element use this particular user name dot send keys and enter this particular email id same thing driver dot find element use this particular password and dot send keys and enter this particular id over there this particular password over there same thing then you have to click driver dot find element click on the login button and then dot click typically so this is my second method page action that i have written now this method is doing what this method is doing login for you guys right so let's see these are different methods actually i have written same thing for home page i have written method number n2 method m3 here i have written m4 and m5 so one example i have shown you likewise for different features you are going to create by locators and these bind locators will be used in these methods to perform to check the behavior of the page and behavior of the web elements and the same thing over here is that you are going to use that for different pages you are going to design now guys these by locators i'll be defining in the form of private variables so i'll be using one encapsulation concept over here that i'm going to encapsulate my by locators and then i'm going to encapsulate with my public methods so all these methods will be public and all these variables will be private in nature so i want to use encapsulation object-oriented programming concept over here that okay all these private variables will be encapsulated and will be accessed by all these public actions or public methods that i have defined fine so same way i'll be using for other methods as well now once the page action is defined immediately i write my test cases so let's move to the test cases part when i move to my test cases part in my login page test if i'm using lithium g or j unit what we have to do we have to write some setup method so i'll be writing my setup setup method means let's see i'll be writing one at the rate before test annotation and inside the setup method i'll launch my browser and everything and then i'll be writing my address test annotation and doing some testing over here along with some assertion and then finally i will be writing my ta double method so this is my ti down method with the help of and the rate after test i'll be writing this is a typical way of writing test cases so same way let's see i'll be writing some other test stages over here test number t2 t3 t4 like that i'm writing along with the assertions now remember one thing when you are writing this particular test and let's see my test name is that verify sign up link so how will you write it so if you really want to write verify signup link this particular test is calling this particular method that you have written sign up link method that you have written so in this particular method over here i'm going to call this method so when i call this method this function is returning what this method is using driver.find element is displayed which respect signup is displayed or not this is the signup is displayed or not and it will return on the basis of its respect yes return true otherwise false so when we are getting true or false over here then i'll be writing my assert over here then i'll be writing let's see one assert true over here that okay yeah element is displayed test is not displayed test case got failed this is the thing guys that you have to do that now what is the rule the rule is let me rub this okay the rule what is the rule in page object model two important rules at the page site okay when you create a page class for page class what are rules the first rule is do not write assertion in page class this is the this is not the right thing you are doing do not write assertion in page class this is the wrong practice base your page class is is not here to validate something page class just will just check that okay what is the behavior what is the current behavior of that particular element am i i mean element is displayed or not page actions are happening or not whatever so you return the state of the page the behavior of the element or the state of the element you return and now it's test class responsibility to test it so the second rule for the test class remember one thing second rule for the test class is that assertion should be written okay in test class assertion should never be written in page class remember this thing if you are writing assertion you go and check your framework in your company if you're writing a search in page file you are doing it wrong according to the design factor you can do it your framework is working fine your execution is fine that's okay but according to the rules according to the guidelines you are doing it wrong but i'll tell you what are the disadvantages with that in some time now test class should have only assertion second third rule is sorry in test class only in the test class only never use web driver apis it means do not use driver dot find element dot click dot send keys or dot is displayed or dot get text do not use your driver api is over here this thing in your test class because drivers is talking to the page driver is talking to the web page driver should be returned should be driver api should be called in your page class and the page tab should give you the behavior the current behavior of the page and it should be validated in test class this is something guys we have to understand what people they do you write a test class you see you would check a lot of the repositories on the page in fact i used to do the same mistake okay earlier but later on i want to know okay no this is also i am also okay doing it wrong this is not the right thing what you do inside a test class do not run a driver please do not call driver.find element it looks noisy it's ugly frankly speaking it doesn't look good okay if you're writing a test having full of driver.find element over there this is a violation of page object model strictly not allowed okay if you are doing it that is your choice but according to the patterns if you really want to use patterns if you usually want to use the back the the advantage of battles in that case you're not doing it properly if you really want to do it you have to follow it otherwise just forget about it okay now and patterns are the very strong concept either it is single responsibility pattern or design patterns or singleton pattern or page object model pattern there are certain patterns are available right in java there are more than 20 patterns are available so that's why there is reason behind that why people why the developers are using these patterns to make your life easy to make your code is proper maintainable maintenance is very important in the framework right guys and i tell you that particular point in some kind so is this clear so in page class do not write an assertion in test class please never use driver apis over there and [Music] test should be responsible only and only for assertions they should not check any behavior of the page over there perfect now you must be thinking okay naveen you are saying you should not use driver then why are you using driver over here because in setup you have to launch the browser and for launching the browser you are using the driver driver equal to new chrome driver and driver. you are launching the url how to solve that thing also right so what exactly we can do let me rub this i go with here it's difficult to explain guys i hope you guys are reading it but let's see now i'll be creating one more page class one once again so typical page class let's see this is my page class this is my page class okay and single page class any page home page or search page or whatever this is my page class in my page class i'll be writing my different buy locators one two three four this is my first second section by locator and then i'll be writing my second section is first section second section is that i'm going to create a constructor on this particular class where i'll be passing the driver over here so that my driver will be initialized over here then the third section is that is my page actions or page methods and be writing different methods m1 m2 m3 four different methods i have written and every method it could be void or most of the time it should return the behavior right so let's see this guy is returning true element is available or not this guy is returning something else let's see once again boolean value this is something is returning a string text and this is something again returning a true value over here like that right means i want to verify that uh the title of the page or that's the header of the page or a logged in username i want to verify so a simple capture driver.finder and then i'll be returning the same text from here right so this all these guys all these page actions they are checking the behavior of the page and then correspondingly what i'm going to do now what i'm going to do is that i'm going to create one base page class over here you create one base page and here you initialize your driver initialize let's see underscore driver method i'll be using and here you use your logic driver equal to new chrome driver so i'll be writing webdriver driver is equal to new chrome driver something like this is equal to new firefox driver is equal to new safari driver like that right with the help of switch x condition switch as a statement or if there's condition you can write it i'm not writing the exact logic over here right and all these page classes they are inheriting they are extending this base page so likewise you can simply do that now when you create your test over here this is my base test i'm going to write this is my base test right and this base test i will be writing my enter it before test annotation and then here i'll be calling this guy so i'll be creating the object of base page base page and then i'm calling initialize driver method over here so i'll get my driver and then here i'll be writing okay let's see here i'm launching the url also with the driver.get and the url also get launch got launched and then i'll be writing at the rate after test and then quick method i'll be calling so the sequence is also written over here somewhere in my base page only right perfect so here i am avoiding to use driver base now all your test classes let's see i have another test class this is my test class and then this is extending this particular test class and here i am calling what getting initialized drivers everything is done simple as writing my test annotation and now i'll just focus on my test cases over here so i simply say that okay hey this is my first test case test case number t1 and this test case number t1 let's see we are calling this particular guy and this guy is returning 2 and now i'm going to assert over here same thing for test case number t2 i'm going to call this particular guy m3 and this guy is giving you a text so i'm going to assert this particular text over here and here i'm asserting asserting true i'm using asset equals over here likewise for test case number t3 i'm calling m4 again i'm doing one assertion over here now it is here so these methods will be called at the runtime from your test energy classes and then whatever you are getting it okay whatever these guys are returning you have to validate over here in the form of assertions you can do that now some people what they do observation also very difficult to write in that case some people say that okay we can write assertion directly over here no what i'm saying is some people what they do you will see this thing in industry in page action i have created one method in the page class this is my page class i have created a method over here i'm writing driver dot file element send keys send keys dot click and then i'm writing an assertion directly over here and then the this particular method is done okay this is a page class i'm writing an assertion let's say you have written an assertion over here and you're creating one one t1 method over here this is my test one and you are calling this particular guy from here and you are not validating anything over here everything is validated over here this is the wrong practice this is according to the page object model you should not follow this page object model should give you the behavior assertion is the testing responsibility or your test responsibility same thing if you're using tomorrow let's see a javascript right base if you're using javascript you can use a watcher or jasmine apis so you have to create your page classes dot js.js you have to create it and then they should give you the behavior if you see the webdriver io series we are giving the behavior we are returning something and inside the matcha or inside adjustment we are writing assertions over there with the help of child assertions or something like this we are using it over there same thing in python so we will be doing that same thing in java so remember guys this is a wrong practice we should not follow that now some people say that okay if you are not writing assertion over here and every time you are writing a session over here then you have to write so many assertions to solve this particular problem what you do you create an assertion common assertion page less a common assertion.java class you create your own assertion class for your application let's see this is my login assert.java and you write your assertion methods over here a1 a2 a3 which are actually using assert class assert dot assert equals assad dot uh true or something like this and you are when you are getting something over here right when you are writing something and you are calling this and then you pass this true to over here and these guys are validating and this is written with the help of test ng only this is not part of your page class so they are talking to each other this class and this class your test class and the assertion class they are talking to each other that you can do that which is absolutely fine now what is the advantage of this approach the advantage of this approach is i'm not talking about this approach this is the first approach we should never use this advantage of this thing is guys that maintenance of the framework first of all you are maintaining proper buy locators over here right go to login page class and yeah these are the buy locators they are participating if any changes are there in the locator simple go there and check it over there or update over there and then all the respective page objects you have written if someone is checking the library so that's why this is called my page library okay that's why this is called page library so someone will come over here and then check okay yeah this is the m1 method m2 method m3 method and m4 method what is the behavior if someone has to change over here they will update the step if page behavior is getting changed on new page got added or something got delete or something like this they are just deleting over here or adding over here they're just changing the m1 method then you don't need to verify anything over here this method should always return something so all the things they should return all these page actions should return and the same return should be validated over here so this is my validation class all validated should be validated over here fine so remember this thing guys in fact the creator of page object model martin follower what exactly he says that he clearly written i'll show you i'll give you the description uh link in the description that okay he clearly says that okay he has written a blog he's clearly written if you are writing assertions in your page classes assertion in your page classes you are doing it wrong in fact there is a column for simon stevert the creator of main contributor for selenium he clearly says that if you're using driver in your test classes you are doing it wrong right dave so two rules as i told you please do not use driver over here in your in your test class please do not use that and please do not use assertion over here this is the main crux of the today's topic please do not use assertion at the base site please do not use driver at the test site driver is interacting with the web page it should be written in your page class assertion is part of test ng it should be written in test class and your page action should return something so that it can be validated over here with the help of test ng assertion library like that and the same fundamental will remain same in other programming languages also because this is a design pattern it's not only for java you can apply for any programming languages over there right now one small thing asking what is page chaining model in page object model right place so let's see quickly now see paint chaining water is also very fundamental concept in page object model page object model says that okay whenever you are creating a page let's see a typical example is i'm this is my login page in login page i have username password and the login button when i enter my username enter my password and i click on this particular login button what is happening we are landing on some different page that is my home page right please this is my so this is my login page and this is my home page i'm landing on my home page so when you write the page class over here so what will you write you will be writing a login method this login method is what right so simple i am going to write type username type password type or send keys or whatever password what will you write click login button and then what it means you're just clicking on it and doing what it means you're landing on the home page it means this method should return what it should return home page class object it means it should return new homepage class object over here it means if you are landing from one page to another page and just because of login page it is happening that you're landing from login to home page it's login page responsibility login page method responsibility to return the next landing page class object right guys so if two pages are communicating with each other because page number p1 because of that having one method over here and now this method is moving u to p2 landing on p2 now its method responsibility to return the object of p2 remember this same thing its login method responsibility this particular login method responsibility to return the next landing page so this is my landing page what is my landing page my landing page is home page now let's see there is a button or there is a link the link name is contacts okay contacts so when you click on this particular link this is the contacts link you are landing on a different page this is my contacts page right guys now obviously i'll be writing a method over here my method name is let's see click contacts right in this particular method i'm going to click on it click let's see contact like that and now we're exactly after clicking on it where exactly we are blending we are landing on contacts page so it should return what this method should return new contacts page object okay like that right guys so right but let's see i have one uh one header is written i want to validate this particular header so i create one method over here header whatever method header method i simply write driver dot finally that's it this guy is returning what this way returning header is displayed or not so this guy is returning true or false right is return true or false this guy is not returning the next landing page class object because we are just validating we are not clicking on it and after clicking on it also we are not landing on anywhere right so whenever you have something like this you are clicking on the login this particular button you're landing on the home page so it should return the home page class object when you click on the contacts it should return the context page class okay from the contacts page if you are going to achieve the product page it should return the product page as object so that's why what you're creating a chain over here that's why this concept is called page chaining model okay page chaining model remember this thing right so a method in page class can return the behavior for login what is the behavior for login the behavior is logging method responsibility is not to validate that okay login is done successfully or not that is should be validated in testing g class right please login method responsibility to give you home page class object now from the home page class object i will be validating in my sng class that home page is visible or not like that so that's why i guess this is called page changing modeling so these are the different uh some basic links over here i'll be and in fact i'll be reading out for you guys so i shared this particular link in fact couple of lines i'll show you see this is a link provided by i mean this is a martinfollower.com he's the main creator of this guy i'll show you don't worry about that this guy says that okay two important lines i'll be reading out that okay yeah i favor having no assertions in page objects i think you can avoid duplication by providing a social library for common assertion library you can do that that already we have covered page objects are commonly used for testing but should not make assertions themselves it's clearly written over there their responsibility is to provide access the state of the underlying page it means the state of the behavior of the underlying page it's up to the test client to carry out the assertion logic who is the test line for me test ng j unit test or unit test or n unit it's their responsibility to write the assertion logic right let's see it's clearly written over there i'll share this particular paragraph okay like that now you see there is a paragraph simon stevert says that it's common to write tests using okay from the specific language and if you have web driver api in your test method you are doing it wrong simon stevert clearly saying that right guys so if you are writing your driver api if you means you are calling your driver apis in your test classes you're doing it wrong it's clearly written over there and this guy martin follower says that okay he is the main creator for the page object model design pattern and this blog is written in 10 september 2013 i'll share with you okay with this thing like that okay and he has written one more thing over there i'll show you that there are different opinion on whether page object should include assertions themselves or just provide data for test scripts that i told you some people they advocated okay now assertion logic should be written in page objects some people they say that okay no assertion should be written in test classes but what exactly i prefer i favor having no assertions in page objects i think you can avoid duplication by providing assertion library for common assertion you can do that but do not write any assertion in your page class clearly written over there that is what i want to read it out for you guys okay so that's all for today i hope guys i know this is something it's really difficult for me also to explain everything on the whiteboard but i try the best but you can implement the same thing in your page object model if you're using it same thing will be applied for mobile automation also or any other prototyping languages you can do that so thank you watch thanks for watching this particular video guys if you have any questions if you have any suggestions or if you don't agree with that approach please write in the comment section everyone's opinion matters a lot it's not like okay that if i have written something that's a universal truth no it's not like that it's up to you how exactly you want to execute that but a lot of advantages with this approach first of all maintenance the report also very properly visible over there that okay yeah proper assertion logic you can check that okay yeah this particular method is doing this particular assertion in the report also assertion errors also will be visible properly over there and the second third thing is that for a new guy for a newbie who joins the company who says that okay yeah proper patents been followed plus this is the page behavior and this is the thing tomorrow any changes are happening in the page it's easy for me to change the locator and the page actions right then we don't need to change anything in the assertion assertion they are written separately over there in the test classes so whatever you're returning from the page class tessentiae will do that that's the only purpose of test engine right that's what smg says okay you just don't worry about it you just give it to me i just validate it my job is not to talk to element or web element or you know web pages that is your driver responsibility that's why i'm not going to use driver also test ng says that driver should be written inside the page class let page will behave like a library will behave like an interface for me to interact with the page classes over there okay on your web application sorry to the pages on your web application so that is the page responsibility not the test engine responsibility right this is what you have to understand i hope you got it thank you so much thanks for watching this particular video guys please subscribe to the channel if you're liking this whiteboard series a lot of new things i'll be coming uh you know i'll be i'll be coming with the different scenarios and different use cases different practical problems we will be discussing in this particular session i hope you guys are enjoying it thanks for watching and please subscribe to my channel and let me know if you have any issues take care
Info
Channel: Naveen AutomationLabs
Views: 43,637
Rating: 4.9530711 out of 5
Keywords: Page Object Model, Selenium, webdriver, POM selenium, selenium pom testng, POM by naveen, UI Automation using POM, Appium POM, Page Object Model by Naveen automationlabs, naveen automation labs
Id: PmijECRKeII
Channel Id: undefined
Length: 37min 12sec (2232 seconds)
Published: Wed Aug 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.