Complete Page Object Model (POM) using Page Factory in Selenium

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone welcome back this is mukesh from blunt automation so in this video we are going to discuss mainly on page factory so in the last video I already discussed about page object model but without page factory so this video we'll talk about page Factory in detail and you will get to know the exact usage of page factory in page object model so I will walk you through the current agenda that we have for today so we'll discuss about what is page Factory then what is fine by a notation in page Factory what is how you must be wondering what is this how so we'll discuss this in few minutes then we'll create some sample test cases using page Factory and most important part which you will get in page factories cache look up annotation okay and I will give you some assignment as well so you will get the better understanding once you start practicing so I will show you one official link of page Factory so this the official page factory link I will just give you this link in the description so you can go through this so before moving to the actual link what exactly this page Factory so in order to support the page object pattern webdriver having a separate library that contains a factory class and here we have a separate method to initialize all the web elements using one method called init element and the last point which is the most important to use this page Factory is it have one cache feature so what it will do it will store all the frequently used web element in the cache memory so you don't have to go and check the web element each and every time so performance wise page factories far better than your regular approach so how to implement this we'll discuss in just few minutes so if you go through this official link so in order to support page object webdriver has support separate library so I will show you this code in a normal way so let's get started so if you remember in the last video we already created two packages so comm dot word press dot pages and comm dot word press dot test cases so I will open this package and again I will create a new page so this time I will say this home page or let's say login let me give login page new just click on finish so this is the page which I am creating so this is nothing but a page object where we will store all the locators so this is the dummy application which I will take so this is the URL if you know this otherwise I will give this in the description so this is the username password and login so I will start Firebug again so in the meanwhile I want to introduce another annotation called find by so if you tie find by and let us say if you want to take ID so let us take the ID first for this so ID is user underscore login so this is another way to locate element on a web page so this only will work with the page factory so if you type and at the rate find by and you are using ID here I'd equal to user underscore login then in to specify the web element and I will say this is user name so it will ask you please import the web element so is we will import now it will still give you this error what is this fine bias if you put mouse over here so you can see we are getting this a fine by from a separate library called orz dot open QA dot selenium root support so the support library will contain page as well so as of my few input it will import a separate class qualified by so this is just one line to identify where melamine so in this statement we are saying find an element where ID equal to user underscore login so this is just one line statement you can break into two parts that's not an issue so this is one way now the important end you can say the standard way to use fine by is using how so again I will do the same I will use at the rate fine by now you can specify if you see the line number 10 you can specify ID here name XPath CSIS anything but if you want to tick dynamically using the libraries you have to use one how-how dot it will give you a suggestion to scheme a minute it's not giving sedation there's some Eclipse issue let us drag in yeah so if you drape how so it will ask you like which locator you want to use okay so you can use either class name here CSS ID ID your name and you can see all this is coming from a separate class called how so how is this class here so let us take this ID and say using so using either you can say variable so you need to specify the exact ID so if I go to this password field I can see the IDS user underscore pass I will specify here and again I will use the web element and I will say this is password field change to how yeah so this is another way so you can stick to the first one as well but this is using how so how is a class here ID is a separate static variable use one comma and using is another variable that is string variable and you need to specify the exact value which you want to use so you either you can continue with this I will suggest you stick to the second approach because this is the standard approach let me show you one more example if I go to this login button I got the ID or this time let us take ID oh sorry XPath again I will set the rate fine by I will say how equal to how dot XPath using equal to I will specify the XPath and I will say web element submit button submit underscore button so you can break into two line let's write a that's a good way actually otherwise it will be lengthy string that sometime will confuse you so it will locate username it will locate password field and it will locate the buttom okay so if you want to see some more description if you put mouse over here just to cross verify it will give you a meaningful statement like what exactly this this will do so this fine by used to mark a field on a page object to indicate an alternative mechanism so as your keys as you can see here it is another way to locate the element on a web page and the most important part it can be used in conjunction with page factory this allow users to quickly and easily create page object okay so they have given the syntax as well so whatever syntax sorry I have discussed here the same they have given example like you can take this find by or else you can use this how approach both having the same you can say usage but syntax will change so let us say if you want to work with on ill on any link so let us say i want to click on this lost your password I will take this and I will take this loss to a password question mark again I will type find by how dot link text and I will specify the link text which I want to use so again I will say comma using so whatever value you want to specify specifying the using keyword and again I will say this is forget password link okay so this is how you can look it so I will give you in a somewhat lengthy assignment so you will get a hands-on for this so remaining thing will remain same what I will do I will quickly write one method public void login to WordPress this already I have covered in the last video so I will quickly parameterize this and I will say you name it C username you name would be good or let's say you ID I will take password so I will say pass now I can use this so this is the web element that I already located so I will say username dot send keys so what do we use the name user will enter I will directly pass here and whatever password so password already be located here so I will copy this dot send keys and I will pass the password which user will enter finally I will click so I will say the submit button dot click apart from this you need a constructor as well that we already discussed in the last video so if you have not visited the last video I will highly recommend go through the previous video as well so here I will create a constructor and as we know constructor having the same name as class name of a class name is log in page new and this constructor will accept webdriver as an argument so whatever webdriver we will receive we will directly pass here so this driver will come from page Factory so whatever driver will come from page factory we will initialize to the local driver let me make the changes and Driver so you will not be confused so whatever local driver we will receive from page factory it will initialize to our local driver so this is just a login page let me show you how you can create a test case for this so I will create a separate class verify WordPress or let's say verify valid login and I will show you one additional thing in this video let me create a separate utility so code redundancy will be less here what I will do I will create page factory let us say help helper is the package that I am creating you can rename as utility or helper and class name I will give browser factory in the last video I thought I will call but somehow I mean so I thought just I will cover in this video so what this browser factory will do it will accept the browser name in a parameter and based on your parameter it will trigger the respective browser so what I will do I will simply say public void start browser and I will accept browser name as in parameter so I will say browser name and I will use one if condition so this if condition is very important now so I will check if browser equal to Firefox then start a Firefox maximize and pass the URL right so let me create one global variable here webdriver driver so if it equal to Firefox I will say new Firefox driver else if you can use it three if conditions but this is another way in the else statement I will check if browser name dot equals to crow then open Chrome browser so let me maximize this yeah driver equal to new chrome driver if you are working with chrome driver you have to initially you have to specify the driver path as well that I am NOT specifying as of now because I already covered in the last video say if you are not familiar with how to work with Chrome browser just see my previous tutorial how to start with chrome and I browser so here I will use IE and better if you use equal ignore key so it will not check the this case sensitivity so if is equal to crow skinnet yeah so it will check either Firefox let me make this equal ignore case for other cessful this is optional part okay so if you are working with page factory you can skip this but I thought I will cover this because it will reduce a code and maintenance will be very less so let's say driver equal to new internet explorer driver so based on a parameter it will trigger the respective browser then you can perform your operation like you can maximize travel dot minus dot window dot maximize then you can pass the URL whatever URL user is passing and finally we need to return this driver actually okay so as of now the return type of this method is void so just change it to webdriver and once you are done with this simply return this driver from here I will say return driver so if you're not comfortable this return type and parameter let me know I will create another video but as of now I will take one more argument from here I will say accept the URL s1 string URL so whatever URL user will pass it will directly pass here so it become very customized method now just pass the URL pass the browser and it will start your application okay and we have created this method as non-static so let me make this as static method so we need to create this as well as static now we are good to go so this is our test case that we this is just a class that we created now I will create a method public void check valid user so this is my test and I will use a notation at the rate test it will give error so I will import test ng now you can see what we need to do we need to start application we need to enter user name password and we need to verify the title so for title verification we have not added the code but as of now to start a browser we already created this browser factory right so what I will do I will simply type browser factory browser factory dot and you can see I am getting that method that we created today if you see the arguments it is taking browser name URL and it will return you the web driver so I will pass a browser name as Firefox URL that means specify the URL so what it will return you it will return you web driver reference web driver driver now it looks quite meaningful for me like I have one browser factory from where I'm starting a browser with this URL and it will return me the driver now my task is to start the test case that is nothing but our we need to initialize our page so let me close the unnecessary windows here so we created one page today called login page new we need to initialize all this web element so we have created for web element I will use three so yeah so this is our test now you need to call page factory class so I will type page factory dot so page vector is a class and you can see it contains so many methods so I will stick to the last one and if you see the description it will simply say it will use to instantiate the page object okay and it accept the two arguments the pass the driver and the object with web element so let me use the last one driver we created already so I will directly pass it now it is asking give me the page which you want to initialize and our page that we created today is login page new right so I will go here and I will say I want to initialize this page and unit reuse the binary so guys who are completely new in Java to the dot class will return you the bytecode of this so login page new dot class so what it will return it will return you page object of that particular page login page so if you pass login page new it will return you object of the same page object and you can use it so in case if you are using some other pages so let me take some more if I say page factory dot init element pass the driver and let's see this time I want to initialize login page so I will say login page dot class so what this will return this will return me page object of the same class login page old so only the point which you need to remember if I pass login page here it will return me page object of the same class if I pass login page new it will return me page object of the same class now we are done I need to simply call and specific created page object using page Factory call the method so this is how we can design page object model using page Factory okay and one good feature about page Factory is you can store all the web elements in a cache memory so if I say at the rate cache look up so what it will do it will not check this web element each and every time on a web page okay it will simply check in the cache memory and it will start processing so whatever web element you want to keep in cache memory you can use this separate annotation okay so now we have given one instruction these four element cap and cache so now performance will be high and it's just a small test case so you will not get the difference but if you go for a long run you will notice performance will be high because it will not check these web elements on the web page again it will take from the cache memory and it will start processing so please be careful while using this because this you will use only when you are pretty sure that this web element will not change so if you put if you double click here so it just nearly it will give me these descriptions mark annotations to be applied to the web elements just to indicate that it never changes okay so now the time came to run this test so let us run it you so once we're done with this I will show you one sample task is that you have to automate and I will tell you the scenarios which you want to which you have to cover and if you are very interested to learn all this just once you're done with your assignment let me know if you find any doubts difficulties and once you are done with the assignment if you have want to review your test you can send me a test case I will review so now our application started admin demo 1 2 3 click on login that's it since we have not given any validation here because this is just a one line of code so I will keep this for you my main intention to cover this video was how we can implement this now the assignment which I want to give you is once you login to this dashboard I will give you an end to and scenario so just click on this post so here you will get couple of options like add new category so click on this add new button and you can create a dummy post so once you click on add new it will ask you a couple of things like title I will say gray got new concept today enter this text enter the same text in the you can say body and click on publish so when you click on publish you will notice when text will come so just wait for a minute once this post will be published you will get a text message here you can say the success message post published sorry so you need to verify whether your post has been published or not so this is your validation point as of now you can say and if you want to keep one more validation point if you see the title is coming added post so title should be added post and once you are done with this publishing so you need to capture this and you need to verify whether this message is coming or not third verification point if you want to go little bit deep if you click on View Post it will show you the post which you have created today so you can see right whatever post we have written today hurry caught new concept today you need to verify whether this title is coming on this page or not so this is just optional part if you are willing to learn this page object model in detail so what you need to do now finally just create one more page I will upload this code on my blog so what you have to do you need to create a new page and you can name that page is post page and store all the elements so add new welcome title body publish button so keep all the element on our page that is nothing but a page object create a separate task is call this page object using page Factory and keep relating the point which we discuss now so let's see whether we have covered everything or not so we have got what a space factory page factory's a class that has a static method called init element that will initialize all your web element fine bye is in a separate annotation to identify web element on a web page so this is an alternative approach you can use find a find approaches will not find normal approach using by class how is it again separate class that contains all the locators and catch lookup it will keep your element and cache memory so performance will be better as compared to your normal approach so that is all I have for this video hope you have got the concept which I was thinking to cover and in case if you hand it out you can drop me an email to my email id and for this complete code you can check my block I will update to date itself and whatever links we have discussed in this video I will give in the description so you can go and start practicing so if you like this video please subscribe to my channel and if you find it useful share with your friends so best of luck see you in the next video have a nice day bye bye
Info
Channel: Mukesh otwani
Views: 236,763
Rating: 4.9037633 out of 5
Keywords: POM, Page Object Model, Selenium (Software), Framework
Id: fUSl-WhqymU
Channel Id: undefined
Length: 31min 3sec (1863 seconds)
Published: Thu Dec 03 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.