Selenium Cucumber Java BDD Framework 7 - Page Factory | Step by Step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this session I'm Doug Huff and you can find all my work on my website automation step-by-step com in this session we are going to learn about page Factory and do not worry if you have never learnt this topic earlier or have never understood what exactly page Factory is I will start from scratch and as always I am going to go very basic step-by-step so let's get started and the agenda for today is we are going to see what is page Factory and how to implement page Factory we are going to see a very practical step-by-step demo and I will also share some useful tips with you at the end of this session so let's get started with what is page Factory and page Factory is a simple and easier and an optimized implementation of page object model in selenium so we have already learned about page object model and how to implement page object model in the earlier session if I go to Google and just search for selenium page Factory and let me show you the documentation so if I go to this github of selenium and go to page factory you will see here this is a class in selenium and in order to support page object pattern webdriver support library contains a factory class so this is a class within the selenium library or the API that helps us to implement the page object pattern and if I just search for page Factory API or class you should also see the documentation here so you can see this is from the selenium website and here is the page factory class and you can see all the methods here so do not worry about all this I will show you exactly how to implement so this is the implementation of page object model in selenium and it makes the page object model concepts more optimized and inbuilt in selenium so here as page object model it also works on the same principles that it separates the objects and the test scripts and here we use our annotation fine bye which can be used to target or locate elements and you can use any of the methods like ID name CSS link text partial link test tag name anything to find the elements and then we also use a method called init elements which will initialize all the elements on the webpage if you again see the documentation you will find this method here so here we have init elements I will again show you practically in a moment so this is what page Factory is it is a very simple and easier implementation of page object model and the factory class is inbuilt in the selenium library now let us see our demo how to implement page factory model in our selenium framework so here I will go to my project you can follow this on Windows or Mac so this is the project we created on cucumber Java and here if I go to the SRC test resources we have our feature file so I am just going to take the login demo feature file that we have created earlier where we are doing a login and we are using a username and password to login and also we have a data table that is we are getting data from here now even if you do not have this example do not worry let us see exactly how do we implement page factory and here I am going to the SRC test Java package where we have created our step definition and in the page object model session we also created the pages package so here first step is I will create a class for each page as we do in page object model every web page on the application should have our corresponding class in our framework so I will go here and let me create a new package first so it is very easy to identify I will create a new package and call this as page Factory and say finish ad inside this page factory package now I am going to create a new class so here the website or the application I am going to test is this example application from test project where we are doing a login with the username password and then click on the login button so for this page that is the login page I will create a class so I can name it as login page and just to identify that this is following page Factory I will say PF here and say finish so we have got our class here now I will start creating the elements so here we have user name so we have the username the password and the login button so this is what I am going to use I will say here web element and I can give any name I will say txt username and that's it I will keep on creating the names of the web elements like this and I will also have to import web element from open QA selenium and now this is we have just initialized or declared our web element and we have given a name here now I will use the annotation at find by and if I press ctrl spacebar you can see this is coming from OHE open Q dot selenium dot support if you see here the package is this Oh Rajee open code or selenium dot support dot H factory so this is coming from the factory and I will use this and in the brackets I can now use any of the locators like CSS ID link text you can also use how I will go very simple here you can use name partial text XPath anything and here I will say because I know in my application we have IDs available if I right click and say inspect on of the element like username I can see here we have the ID property available for this enter value is name so I'm going to use this I will say here ID equals name and you can also see it has imported all these libraries so this is all we have to do to locate or to create a locator of any object on the webpage so we have done step number two that is create locators for all the objects and let me complete this by creating locators for all the other objects I will say web element then for password I will create the name now this name you can take anything which can be easily understood and then I will again say at find by and here I know the ID of this object is password then I will say the element and I have to create the button login and here I will say at find by and I know the ID property of this button is login so that's it I will save everything we have done step number two now step number three is we can create methods or the action methods for performing action on these objects now here I can create functions or methods for actions I will say public void now you can create a actions like enter username enter password and login like one method for entering username one method for entering password and then click on login or you can also create a method for a valid user login both will work in real world you may want to use a single method for a valid user login but in our case because we have our feature like anti user name enter password and then a separate step for click on login I'm going to create atomic methods so I will create a method for enter user name and here inside this method now I will use this txt username I will say a txt and if I press ctrl spacebar it is Auto completing and I will say dot and then I will say send keys and I will give the data or the username I want to enter now here you can see I have not used driver dot fine by like we used earlier in page object or in earlier sessions this is the advantage it will take care of all this using this annotation it will take care of finding the element and then we can directly use this name and then do our actions so this is all you will have to do also as this is not a good practice to hard-code the data here and they should be coming from the test scripts I will take this as a input to this function in the arguments I will say this will accept a user name and that user name should go here and this will enter the user name similarly I will create a function for enter password and I will say string password here and here again I will say txt password the name that I have used dot send keys and this variable and then I will say click on login and here I do not need any data I will directly say BTN login dot click and that's it so you can see this is how we will create the functions and this is how simple it is now we have done step number three that is we have created the methods or actions now we have to create a constructor to get the driver instance and also to initialize these elements so here as we have seen in the page object model session I need to pass the driver that is from the test script so that the session is maintained or for example let me show you if I go to the test the step definition of this feature file which is this one or let me show you the form one that we created last time so here we have our webdriver the selenium webdriver which is running this test which is opening the browser and the session is here now the same driver should be passed to this page so that we can maintain the same session and for that we can create a constructor so constructor as I told you in the page object model session as well is a function with the same name as the class name and does not have a return type and I will show you the advantage of that in a moment so I will say public and without any return type I will just give the same name as the class name and a bracket start and close and a curly bracket start and a curly bracket close so this is a constructor and whenever we create any object of this page of this class this constructor will always be called and we can use this property to take advantage to initialize or to pass the webdriver so here I am passing webdriver driver and I will also have to create a the driver instance for this class I will say here let me also do it fullscreen so it is easy for you I will say webdriver driver and I will have to import it from open qsr lynnium so this is the driver of this class and here in the constructor we are getting the driver from the class which will be calling this class or creating the object for this class and I have to make this driver equals to this driver that I am getting in the constructor so I will say driver which is this driver equals to this driver so I will say driver equals driver now of course you can use some different names so that you can identify I can say driver one here and like this this will work that is fine however what we normally do is we say we want to therefore you want to say this driver is the driver of this class so I can say this keyword I can use this keyword and now this driver will refer to the driver of this class and this driver that we are getting here is coming from this is coming from this here so now this is done we have got the driver instance now we also need to initialize the elements and for that we can say we can use the page factory class and use the init elements method so I will say here page Factory dot unit elements and here I will pass the driver instance and then here I have to give the class so I can just say this keyword here which will refer to this class or I can also give the class name like this I will say login page underscore P F dot class so whenever this will be called all these elements will be initialized on this web page and therefore we are putting this inside our constructor function so that whenever an object of this class is created this will run and it will initialize all the web elements so we have done step number four here now step number five is we have to update the test script so that it can refer the methods from this page factory class and for that I will go to my step definitions package where I have all these glue code or the step definitions for the feature file and I'm just going to take this login demo steps I will copy this so if you want you can create a new class but I'm just going to use this we have created this in the earlier session and I will copy and paste it here and I will give it a name login demo steps underscore bf so I can identify this is for the this is calling the page factory concept and I will say okay and here it is and let me also uncomment all this this is what we commented in the last session of page object model I will press ctrl and /on my keyboard and this is now all uncommented also if I go to this login demo steps underscore pom class that we created in the page object model session this I will again and in this I will comment out so that when I run the feature file this is not called and the steps are called from our page factory class that we are going to create now so that's it let me save everything and I will just close this I do not need this I do not need this either yes so now we have our login demo steps underscore PF class and here the first thing is I will create our object of the login page underscore PF class so I will say here login page underscore PF and I can give any name here like login and that's it so here it will create this object for the login page underscore PF class also in case you want to know how do we create objects and want to learn some basic concepts of object-oriented programming you can always go to my website that is automation step-by-step comm and this is just in case you are a beginner in Java and you want to understand few concepts in detail you can find a Java playlist here so under programming section you will see there is a Java playlist this will take you to the YouTube playlist of Java and here you will find these videos the basic beginner videos on getting started with Java and then these videos that is what is object-oriented programming with examples and how to create classes and objects this will be very useful for understanding this concept so this is something you can check coming back to the class so I have created this object or a instance for the login to hpf now wherever I want to use this I will go here for example this is the function which is for entering user name and password now I will use this object login dot and here I will say enter user name and you can see I am Auto getting Auto suggestion and I am not hard coding it because we are already getting this in the function as a string as a argument and this data is coming from the data table of this feature file so I will use the same thing user name here and then login dot enter password and here I will use password and now I no longer need these statements so I can delete or comment these so this was where we were finding the elements locating the elements and providing the data and everything within the test script so this we have changed now we are following the page object model and we are following the page factory concept here and then click on login again I will say login dot click on login and I do not need this here and I will save everything and then we also have our function where we are that logout button is displayed so for this again I will have to create a class so I'm going to create go to the page factory package and here I can create a object and function for the logout button as well here but this is only for the login page and as per the concept of page object model every page should have a separate class in your framework I do remember in the page object model session I created the element and the function within this class so if I go to this login page dot Java that we created in the page object model I did create it here itself which is actually not correct as per the rules of page object model so I'm going to create a new class now under page Factory I will create a new class and I will say this is home page underscore PF and here I will create a object for logout button and all the other objects of the home page I can create here and I will have to import web element from over G open q selenium and I will say find by I know ID is there equals to log out and I will also have to import this find by from open case lynnium support and then I will create our method public void check logout is displayed and here I will say be dn logout dot is displayed and that's it and I will have to add the constructor as well so for that I'm just going to copy this from the login page I'm just going to copy this like from here and I will paste it here and of course the constructor has the same name as the class name so that I will change here this name of the constructor and that's it and now in the test script I will also have to create an object for this page so I will go to the top and the reason I'm creating the objects here is so that I can use this in any of the methods of this class so I will say homepage under scope EF and I can give the name home here and then here I will say in the function to check user is navigated to the home page I will say home dot home dot check logout is displayed and I will remove all comment in this statement and save everything so we have done step number five that we have updated the test scripts and now we will run and validate also just to and show that when I run my feature file this is the class that gets actually executed I will create a print statement I will say sys Oh control spacebar to autocomplete and here I will say I am inside this login steps PF class this is just to check if it is running the right scripts so now I will go to my feature file and do a right click run as cucumber feature and let us see the execution so it should open the Firefox browser and then run our test case so yes it opened the firefox browser and going to the application and looks like it did not enter the data let me very quickly check the console here so here if I check this is fine so you can see this is running from our class here login demo steps underscore PF which is fine and here it is turning this and now here when user enters this username and password here we are getting a nullpointerexception on this particular statement let me go here and see okay so I believe I never created the object or in other words I never called the constructor from the login.php F so I need to do like this login equals new so I unless I use the new keyword it is actually not creating the object or not calling the constructor of the login page PF class so now I will say login equals to new login.php F and Here I am also passing the webdriver instance so here I am passing this webdriver that is here in the test script and this is getting passed here to the constructor of login.php F and if I press ctrl and click on this you can see it takes me to the constructor of this login page and the Scorpius class and here we are passing that driver which is then referring to this driver or is being made equal to this driver of this class and then we are able to maintain the session so now this should run I believe also here if I am using this in a constructor I should be using this keyword then it should work fine otherwise it can be a problem or you can separately create an object but because we are using inside constructor you can use like this and now let me try again I will go to the login demo features and just in case you also face any issues you can also troubleshoot like this or you can also do debugging what you can do is you can go and see where exactly we are getting an issue and then you can create these breakpoints like this so you can see I am going to this margin this blue coloured margin and double clicking which is creating these breakpoints and then after you have created the breakpoints you can go and debug so if I right click and say debug instead of run as say debug ass and then select cucumber feature what will happen is it will go to your breakpoints it will go wherever you have your breakpoints go until there and then it will pause and then you can do step by step execution and see where exactly is the issue so just in case you want you can do that so I hope this should be fine now I will do a right-click on my future file and say run as cucumber feature and let us see the execution this time so it opens the Firefox browser and yes this time it is running fine and this was the first set of data now it is running with second set of data and this is all running fine so we have done all our steps now some useful tips tip number one is we can use an annotation cache a lookup and here if I go to my this class login page here for any pages class here in any of the elements you can use this annotation at cachet and if I press ctrl spacebar I am getting this auto completion so it is again coming from selenium support library so what this will do is if you are working with a very basic website and you know that the locators are not going to change and it will always remain the same so in that case you can use this cache you look up so what this will do is once it has located the elements on this web page it will then store it in the memory and whenever you want to use the element again it will not go to the page and do all the identifications again it will use the memory or the cache you look up to interact with the element so this is one annotation you can use so it can be used to instruct in it elements to cache the element once it is located so that it will not be searched all over again wherever you are calling the method where it is used and this is how it is used you have just have to use the annotation at cache you look up and you have to be very careful when you use it in case you are using any Ajax applications or any applications where the objects can change on user actions this will not work so do not use this if you are using Ajax or these kind of applications also if while using this you are getting still element exceptions then also you can avoid using this annotation so this was tip number one tip number two is whenever we handle Ajax applications with selenium we face a lot of issues with loading time of the elements because in Ajax different elements can load at different times and then elements can also behave differently on user actions and therefore we may get new element exceptions a lot of time if we are handling Ajax with selenium so there is a solution for this in Paige factory class and that is Ajax element locator factory here you can actually set that timeout for the web elements and this is how you can use it so you can say here in the constructor you can say Ajax element locator factory and you can give any name let me say factory here equals new Ajax element locator factory and then here in the search context I will say driver and then you can give that timeout in seconds so let's say I say 30 seconds you can increase this timeout as per the needs of your application and then after this you can just put this factory here so instead of this let me put this here and here I can say factory so now we are using this Ajax element locator factory with a timeout of the thirty Seconds or you can directly say here like this new Ajax element locator Factory and here I will use driver and say thirty seconds and then I can do it in a single sentence instead of these two sentences so this will work if you are using any Ajax or related applications and this is tip number two so here in the above code it will wait maximum for thirty seconds until the element is loaded and in case the element is still not available up to thirty seconds then it will throw no such element exception but it will wait until thirty seconds and will not throw this exception if we are able to get the element within thirty seconds and you can increase the time out as you wish tip number three is with page factory we can also locate lists of element something like this let me show you an example here so here I can say let us say I want to create a list of elements I will use lists and lists of web element and I will say my links and then here I can say so I have to also import this list from some library so I am using Java util and then here I can say head find by and I can say I can say partial link text and here I can give some text so what this means is on this web page all the links which have this value Raghav we'll all get stored into this list called my links and then I can use this in any way I want so this is how you can use this in page Factory and also one more thing that will be useful to you is one more way of finding elements is for example so here if I say web element and let me say any element ABC I can here give the notation fine bye and then there is another way I can say how so how exactly I want to find this element and I will say how dot and you can see all these phase ID CSS link text name XPath etc let us say ID and then I will say using equals I will give the name of Tidy name so this again will get imported from selenium support so this is also how you can use to identify elements with page Factory and this is how you can use page Factory I hope this session was very useful for you you can do some more hands-on and let me know if you face any issues I will meet you in the next episode thank you for watching
Info
Channel: Automation Step by Step
Views: 30,273
Rating: undefined out of 5
Keywords: selenium cucumber bdd tutorials for beginners, selenium cucumber beginner tutorials, what is page factory, how to implement page factory model, how to use page factory in selenium cucumber bdd, how to use selenium for ajax applications, page factory for beginners, how to use page factory
Id: PGLKmKTjMGI
Channel Id: undefined
Length: 33min 0sec (1980 seconds)
Published: Tue Jun 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.