Page Object Model in Selenium Webdriver Step by Step Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone welcome back to learn automation calm this is Mukesh so today in this video tutorial we are going to focus on the page object model generally it is known as poem so there are two ways to implement this page object model in selenium-webdriver so we are going to introduce page object model first without using page Factory and once you are comfortable with page object model we will switch to the page object model with page Factory that is the actual way to implement page object model so before moving to the actual legend like implementation part this is the high level agenda for today we will talk about what this page object model then what is the need of this page object model in selenium-webdriver and finally how we can implement this so there are so many rumors in the market like what exactly this page object model so someone says page object model is a framework but it's totally a myth frame page object model is just a design pattern not a framework okay so what do you mean by design pattern like you need to design your test cases in such a way that it will be easy to understand so as the names this page object model so here we will be dealing with pages now how you will relate this pages with selenium so pages will be nothing but a pure Java classes so based on your application behavior we will be creating so many pages and whatever is locators that belongs to that page we will store in that java class and we will create separate method to use them so i will show you the example as of now let us consider a scenario like you where you need to test flipkart.com or amazon.com so first you will be creating one page for the login so if you create for login page you will store all the locators which comes under login page like username password click on button in the forget password greet a new account and so on so all the elements we belongs to that class that functionality will come and come enter a separate class so once you log in you will get home page then so you will create a home page our class and you will store all the locators I will show you in a real time example so the main advantage of using this page object model like you will note this once we start designing the test case your script will be in a more readable format so if any third person is checking your code and if they are leaving your code they will be able to understand what exactly you have done in your script okay so if you script in normal way also that's completely fine but your maintenance task will be very high so we will be using beads object model just to achieve three things that is the main feature of page object model like easy to maintain so what does it mean I have the same thing in the next slide I will show you in just a minute readable format we will be creating so many methods in a readable format so that even if you are also checking your code maybe after a year then you will be able to understand like what exactly you have covered in this script and the most important thing reusability you will be able to call them in a different places so that you can reuse a script that is the one of the biggest challenge in automation like how much you can reuse your code okay so all this our theory part so once I will cover the complete tutorial I will come back again to the slide and you will be able to relate what exactly I am talking about okay so as of now you can consider this as a theory but somehow you have a picture in your mind like what exactly this page object model is so as of now I will just give you a one-line statement pages means we will be creating so many Java classes based on the functionality and we will call that Java classes into your regular test cases so let us take a high-level diagram so let's say my application is very small application a hand I have only login page home page and account page and finally you can add one more like logout so if I create one test case I have to go through each and every pages right so in spite of writing everything inside my test case what I will do I will create one Java class let's your homepage dot Java and all the locators like whatever element that comes under homepage I will stored in the home page door Java same thing I will do for login so for login I need username password click on button and forget password segment so all the for locators or you can say web element I will store under login page in the same way will do for account and I will use them inside my test cases now you must be thinking that why I am doing all this headache just for one test case why I am creating this page you are perfectly fine with the concept but just think about the future purpose now let's see if I have more than 100 test cases and if I am not following this approach let's say even for a small change I need to modify all my test cases this consider one scenario say login page ok in the login page they added let's say they change one locator from username to user login so what you will be doing you will be modifying all your test cases right because you are not following this approach so now let us say less task is to task a task export in each and every test case if I am not using this approach so I have to go to individual test case and I have to update right but if I follow this approach and if I know that in login page only one field has been changed from a username to user login then it's very easy task for me to go to this page and just modify one locator olio so while designing automation script you also think for the future perspective because definitely if somebody is adopting your automation script they will be running on the monthly basis weekly basis and maybe on the daily basis right so your maintenance should be very low so in case any changes happen if you modify at one place it should be reflected it multiple places okay so there's one more concept called object repository so you can say we have an entire alternative of object repository here so there are two way to implement the space object model so one is the using normal approach that I am going to cover in this video and we have very you can say structured manner to implement the space object models using page Factory and using find by annotation so let that is little bit complex not complex but once you are familiar with the basic approach then you will be able to relate the second part very easily so I will highly recommend to was this first then only move to the second option so this is what the theory part let me quickly show you how we can do this so I will be using this dummy application that is the wordpress.com so I will be using page object model to automate this what I will do I will automate one scenario using one page and I will give you the second page as an assignment so just try to achieve the same thing in the next page after login so this is my clips I don't have a navigation pane here so this is our already existing project that I have learn automation so I will separately create a new package called pages so let me give the meaningful name like comm dot word press dot pages just try to give us a meaningful name so that you will be able to relate like for which application you're writing pages so let's say if you are practicing at home so you should make in this way so let us say if you are trying on Flipkart just type comm door flip car dot pages so that it will be easy for you so here what I will do I will create one login Java class and I will just give low login page and good thing always try to make a habit of generating comments it will help you in a long run like what this test case or what this class is doing so you can say it is automatically picking my user name location underscore 50 and I will give some comment like this class will store all the locators so this class will store all the locators and methods of login page so this is my class so what you need to do you need to identify the locate web elements first so as I can see I have five web elements here one is username password login remember me checkbox and lost your password so I will quickly login to this application and I can see I have very meaningful name here sorry ID that is user login so you can take this XPath as well I will prefer ID as of now I will show the multiple options in the next locator I will take XPath so we have on by class write in selenium so just type by dot and you can see we have so many locators here right I will take by ID because we have taken ID here and just specify this ID so what is this exact this will return this will return you by class so I will say username just give me a minute I implemented the wrong class so you need to specify by dot ID so what exactly this buy is buys a separate class if you put mouse over here you can see it's a class and we have on a static method caller ID so whatever ID you have taken this mentioned here so what this will return if you put mouse over here it will return you by a class only so I store in the by class only in a same way I will store all the password as well I will say password equal to be Y by dot let me take X path this time so it you will be having combination of locators so for password I will take this X path now let me quickly take one more I will say login button and try to give us a meaningful name so you will be able to relate actually so what this locator day then why it is useful and so on so as of now I will stick to three web elements and let us take this name so all ID all ready we have taken X path elders also we have taken this time I will take a name so you have all three locators now you need to create one constructor as well so I will tell you the reason why we creating this constructor so how to create constructor so guys who are new to Java okay constructor having the same name as per the class name so our class name is login page so what constructor name will be login page okay starting and closing and we will parameter is this constructor so this constructor will accept one argument called webdriver and just specify the driver so I will take one local variable you can say reference variable webdriver so this webdriver will help us to perform activity on our webpages so whenever you type webdriver it will ask you what exactly this webdriver is so you put mouse over here and it will ask you to import this when you import this this - error will go so we will be calling this constructor from our test cases so you need to type this dot driver equal to driver so what this constructor will do the constructor main task just to initialize this webdriver so whenever we will call this page in just few minutes I will tell you how to call them so whenever you call this constructor automatically the driver which you will pass it will accept here and it will initialize this webdriver references so that is the internal task of this this keyword so now we are almost done with this we have three locators we have AB driver a friends now let me create three meaningful methods public void type username I always recommend you to use some meaningful name public void type password one more public wide click on login button okay so we have three methods so how to use them so I will say driver dot find element now we already have located Eddy so what I will do I will simply type this username here and I will say dot send keys so guys the username is admin in the same way we'll do for password I will say driver dot find element bye bye already behaves I will simply take this password here and I will say dot send keys enables it demo one two three demo one three is the password for this wordpress.com and I will say driver dot find element a login button dot click so now you can see it looks quite promising like you have separate Java class that is nothing but a login page that simply has three locators one constructor and three method to use them now let us create a very small test case just to login for this particular application just create a new package and package I will give very meaningful like com dot word press dot test cases just give me a minute I'm getting and call yes sorry so let's click on finish and let's create a new class I will give very meaningful name like verify login let's say verify WordPress login click on finish and if you want to give generate comments it will generate the comment so this generate comment is very useful let's say if you are working inside a team so you will be able to understand like who is the author of this test case ok so let me quickly create one a small test so I will be using testing G here so guys who are new to Destin G I will show you how it works so public wide I will give very meaningful name like verify valid login so this is just a method here and I will use at the rate tes T test so this test is nothing but an annotation that will demonstrate as an entry for this test case I will use only one test case here so I have only the one entry point so when you write tight type test here it will ask you to import J you know testing G just type test range now your test is ready here so we need to start with webdriver so I will type webdriver driver equal to new Firefox driver so tool ask you to input webdriver so yes quickly import this it will ask you to import webdriver as well so yes import this now maximize the window so guys if you have notice here these three lines of code are repetitive in nature so if you just let me type one more here that is this URL so every time every application or you can say in every test case I have to write the three line of code right webdriver opening like maximizing and passing the URL so we need to creat one reasonable method for this like whatever parameter I will pass and whatever URL I will pass it will open that application in that respective browser so in the next video I will cover that part as well as of now let us stick to this three lines now the time came to call that page so our page was login page right so I will type here login page and I will create object of that class I will say login page login equal to new login page and here you need to pass the driver it will ask you to import this is put mouse over here and import this so now what does it mean now you must be able to relate Here I am passing the driver so this driver is basically pointing to the Firefox driver so whenever you create object of it and you pass this driver internally it will pass here it will call the constructor and this driver will associate with this driver and you can use this login page so that is the internal tasks of constructor so if you are new to constructor I will recommend you to refer any which based which is based on constructor so now we are almost done now using this method sorry object you can call that methods that we created type username login dot type password and login dot click on login button so now if you see apart from these three lines these four lines looks quite meaningful for me like I can easily understand like I am working with login page and I am typing user name password and I am clicking a login button as well right so far you can see it's a like and say static because I am NOT using this like dynamically I am NOT passing it parameter I am NOT passing the parameter from it that's a different thing and we never use this we always have to parameterize this so that we can reuse it and finally I will say driver dot quit so let me run this in the next program I will parameter is so let us run this right click and test in G test why wiII I get intestine jitters because we have used a notation at the rate test of testing G so let us run it and you will notice it will open Firefox Maxima it should pass the URL and it will do this task so let us wait for Firefox so guys this you can take as an assignment and you can also start working from your site like I have given a small demo but you need to explore this and if you need to practice just let me know I will provide you some more assignment here so it will launch that URL yes it's launching my internet is slow today so yeah now it will type username admin demo 1 2 3 and login and you can see it's navigating through the dashboard side and it will quit yes it is quitting s because we have given code as well so if you see once our test case will execute it will say pass right because it's working as per the expectation now you can see it's very easy just you need to make sure like whatever the pages you have and what other elements you have to store so now if you let me show you the next step that you can explore from your end if you log in with this credential admin demo 1 2 3 you will get couple of pages to create now if you want to explore this this is very good application for you let's say I you need to work on posts so this is another page so in this page you can add all the web elements you can add a new post and you can publish it save as a draft and so on so you need to create so many pages and you can create all the pages in this folder that we created now like comrat word press dot pages you can create here home page tour java a post page dot java and so on and respectively you can call them so in this example i have associated only one page but once you work in a real organization like real test cases you will be interacting with multiple pages as well so in the same way if you have multiple pages you can start creating pages create object of them and call the respective methods so this is one scenario let me show you how we can parameterize them so I will quickly copy this and I will paste and I will give verify WordPress login one so now what I will do I will parameter is so right now this is not parameterize it means I am simply passing user name/password that is static so how we can parameterize them make them as a parameter s so I will pass one argument called user or let's say UID so whatever UID user will pass will directly pass here I will not touch this whatever user name will pass I will pass here in the same way whatever password user will pass I will die a clip pass here so now if you see here it's throwing error because we need to pass here so I will say admin and I will say this as demo one two three now you can see not become the you can say the standard test case we are passing as a parameter so let us say in case any changes happen like some date test data is getting change we can change here as of now I am taking this data directly from the user but actually we will take this either from the XML or from the excel sheet that is another step as of now I will suggest you just implement this and once you are comfortable with this switch to the next some create some more pages and call them one by one so let us run this again functionality wise nothing will change but the way of writing the test cases is changed now now looks quite readable and meaningful as well so this will run successfully without any doubt okay so once you are comfortable with this just comment below I will create another video on page Factory and using fine buy annotation and it is another very important interview question as well like what is the difference between page factory and normal approach okay so that's all about this video and now if I go back to the previous now you can easy understand what exactly I was talking about like a script will be more readable yes maintaining easier to maintain means in case any changes happened in the application I have to modify only one Java class let's say if username is getting changed so right now it's user underscore name if in case any changes happen I will modify that particular Java class only right readable format and reusable so this reusable script let me quickly show you what do you mean by this so as you can see here I have three methods here so the question comes can we consolidate these three and can can be cleared one method called login yes we can do so let me show you how you can do that and so in that case what you will do you will create a method called login to WordPress and this method will accept two argument called username and password right now same thing but we are doing in a in a different way I will copy them because they are doing the same thing here what do we username will pass I will pass here and in a same way what is throwing error here let me quickly check I will say drivers dot find element username there's some issue here let us change this to user only user ID yes there was a mismatch actually so let's use the different names here driver dot find element by sorry we already have by here so I will quickly say and change it to pass otherwise it will mismatch again password dots and keys and pass here and finally driver dot find element login button dot click ok so this is another way like if you don't want to call them again and again just delete all these three and simply type login dot login to WordPress now you can see how quickly we can reduce the code now whenever I have to log into any application every treat a method and parameter it is now just think from the user perspective I'll say you have thousand script and almost in every test case you have to login to the application then only you will be able to navigate to the next page is right so you can create this method and whenever you need any logging just call this method and you will be able to login in case any changes will happen you have to modify this particular method only so if you want to test this whether it's working fine or not let's run this it's giving some issue because other class is having some issue so just click on proceed because this test does not have any error other class is having some issues so it will run in a normal way because still we have not changed anything but we have changed the writing pattern so guys I will always recommend you to try this and you try to implement this from your end so just let me quickly go through the last slide let's wait for this I am NOT verifying anything because I am assuming you know how to verify the title error messages and whether it's logged in to the application or not yes now once this page will load completely it will call it driver dot quit and it will close the browser yes and distressed pass okay so now you can see it is very reusable so that was the actual meaning of reusable so I think we have covered everything same case if you have still any doubt just drop in email to my milady and soon I will be creating another video on page object model using peach Factory and find by annotation and once you all are familiar with this then we will jump into hybrid framework and how to implement page object model in hybrid framework that is a very hot topic in the market and you will enjoy the topic so thank you once again for watching this video have a nice day bye
Info
Channel: Mukesh otwani
Views: 406,459
Rating: 4.8868713 out of 5
Keywords: Selenium (Software), Framework, Automation Framework, Page Object Model, Java (Programming Language)
Id: w_iPCT1ETO4
Channel Id: undefined
Length: 31min 21sec (1881 seconds)
Published: Thu Nov 12 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.