Selenium Cucumber Java BDD Framework 6 - Page Object Model | Step by Step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this session I'm Yakov and you can find all my work on my website automation step-by-step comm in this session we are going to learn about page object model and we will see a practical implementation how do we implement page object model in our selenium cucumber project and as always I am going to go very basic step-by-step and I will start from scratch so today the agenda is we are going to learn what is page object model the advantages of page object model and then we will see a practical demo how to implement page object model in our framework the prerequisites are you should have a project set up with the required maven dependencies and we have already done this in the earlier sessions and they should be assemble login tests or any sample tests created and all this we have covered in the earlier sessions just in case you do not have this peer across its ready you can watch the earlier session and now let's get started and let us see what is page object model so I will go to google and search for page object model and I will go to images and here you can see some of the images here let me see this one and here you can see this is a page object model image and you can see here this part is no palm structure and then this part here is the palm base structure if you see this part here we have a single class and the test scripts and all the locators of the web elements and the action we take on the web elements all these are present in a single class whereas in page object model based structure we have a separate class where we have identifiers of the objects or the elements of the page and then all the actions we do on those elements are also present in this class and the test scripts are in a separate class and the test scripts refer to these methods available in the page class or class a so this is basically what is page object model and here it is a design pattern to create our object repository and a class is created for each page of the application to identify the web elements of that page and we also create the methods to do actions on the objects let me show you here if I go to the demo website that we have seen in the earlier session here we have a login page in this login page we have a username text box or password text box and a login button so these are the three elements that we are interacting with on this web page so in our project in our framework we will create a separate class for this page and then we will create the identifiers or locators for these objects username password and the login button and also the actions that is enter user name enter password and click on login button in that particular class and similarly for every page of the application we will create a separate class in a moment I will show you that and the main advantages it separates the objects from the test scripts so the code becomes very much reusable and maintainable let us very quickly look at the advantage before we go into the demo so it improves maintainability we can maintain the code very easily because now the test scripts are separated from the object repository and the main advantage here is once we have all these object locators and the methods in a separate function or a separate class whenever we have to make any changes or whenever we have to make any updates we just have to go to that particular class and we do not have to go to each and every location wherever these objects are used and it improves the usability it makes the code reusable improves readability because now we have all the objects at a single place so it kind of creates the documentation and it saves a lot of time and efforts it makes all the changes very easier of all three works the tests are less brittle it makes everything easier and faster and improves the overall quality and efficiency so these are the advantages let us very quickly jump into the demo and see how we can implement page object model in our framework so I will go to my window system you can follow this on Windows Mac operating system and on my Eclipse this is the project that we have created earlier and if I go to the SRC test resources package here here we have a features folder and then this is the login demo feature that we created in the earlier session where we are doing a login on this login page and we are also using a data table where we have two sets of data here so this was this is what we created earlier and if I go to the SRC test in java package we have step definitions here and here we have log-in demo steps so this is the step definition or the glue code for that feature file now here step number one is we have to create a class for each page of the application so let me first create a login page I am going to create a new package first so I will go to the SRC test java package do a right click go to new and here I will create a package first and I will name this as pages so that I can keep all the pages class inside this package and click on finish and now inside this pages folder I will create a new class for login page so I will name this as login page so it is easy to refer and now I will say finish and here I have created a separate class now I will create locators for all the objects that we have to use on that page so here I have to use username password and login button so I will say here I can use the by class of selenium and I will say I will give any name I will say txt username now this naming convention you can give as per your understanding something which can be understood by our entire team we normally give the type of the object like it is a text box I can say txt underscore the name and you can also say underscore the page where it is located so that it is easy to locate as of now I'm going to keep it very simple I will say txt username and I will say by dot now you can see we have dot ID name partial name XPath so many locators or strategies to locate and if I go here do a right click and go to inspect here we have ID available and the ID is name here so I'm going to use this and you can also create experts in case you do not have our proper locators available and for that if you want to learn how to create experts you can go on my website automation step-by-step calm and here if you scroll down you will find a link for XPath and web locators and this will take you to a YouTube playlist and here you can see all these videos how to create X paths and then there are some tools which help us to create experts automatically you can also use this and then other locators as well so this will help you if you are completely new you can watch this in this case I will say dot ID and the ID is name and I will create a locator for username and then for password password is by dot ID and I know from the last session the ID here is password and a semicolon then for login button I will say this is button BTN underscore login and then I will say by dot ID and here again I know the ID for login button is login and that's it so we have created all this and now I will also create functions so that I can do the actions on these objects so the first thing I will do is i have to enter username so I will create public void enter username now I am going very atomic I am creating a separate function for reaction in real-time you may want to create a single function for a valid login or for invalid login I will show you that as well in a moment as of now I am creating a function for enter username and here I can say driver now we have not created the webdriver object so let me create a webdriver object and I will create at the top so that I can use it in all the functions I will say webdriver driver and I have to import it from open queuing selenium and you can see it is imported the by class was already improve imported here when we used this and now I will say driver dot find element and now you can see I can directly use the name here txt username that we have given here and I will say dot send keys and I can give the data here now it is not a good practice to hard-code the data we should be getting the data from the test scripts or from wherever this function is called so for that I will take a argument inside this function so I will say here string username or you can name it anything and whatever is passed here should be given in the username field like this so now I am entering user name in the user name textbox similarly I will say public void enter password I am creating a new function here and here again I will get the password as an input to this function and then I will say driver dot find element txt password and I will say dot send keys and I will use this variable password here and then to click on login button I will say let me expand this so here I will say create a function public void click login and here I do not need any data I will just say driver dot find element and I will say BTN login dot click now in case you do not get this Auto suggestion you can press ctrl spacebar sometimes you might have to restart Eclipse but they should normally work and I am getting this order suggestion I have used click here and that's it I have created the identifiers and the methods for all the objects of login page now let me also show you in real world you may want to create a single function for login so I will say public void login valid user and I will have to pass the username and the password here and then I just have to use these same statements that is enter the username enter the password and then click on the login button so this is what we can use but for now as per the feature file we have created because we have atomic actions so we are going to use this and a username and a password and click on login so here we have done step number two or we have also done step number three that is we have created methods or actions to be performed on the objects and now we will refer this in our test scripts so now I will go to the test script or the step definition and here we already have this login demo steps where you can see here we are creating the identifiers or the locators and all the actions within the test script which is what we will change now so instead of changing this let me create a copy I will right click and say copy and paste you can also say control C control V or command C command V in Mac and I will paste it here and I will name this as login demo steps underscore p om so that I can no this is following the page object model design and this is created here so now here let me do the changes here in this function where we are entering username and password instead of these statements now I will have to defer it from the login page class now one important thing here is let me go back to the login page class and here if you see this web driver this is present and this is created inside this login page class and this is what is being used here however when we start our execution from here we should be using the driver instance which is created in the test script so that we can maintain the same session so this driver should be passed here in this login page and we should be able to maintain a session if the same driver is being used now to do this or to force the user to pass our driver object or a driver instance whenever our object or instance is created for login page class we can create a constructor so what is the constructor constructor is a function with the same name as the class name so this is a login page class I will say here public and it does not have a return type I will directly say the name which is login page same as class name and then this is a constructor now this constructor is called every time our instance or a object is created for this login page class so for example if I go here in the step definition class and if I say something like login page login equals new login page so this is how we can create our instance or the object of the login page class and whenever we do this basically whenever we use the new login page or statement this constructor will be by default called every time we create an instance or the object of login page class now we can make use of this and take advantage of this to force the user to pasta webdriver instance so I will say webdriver driver here and now I want to make sure that this driver that we have declared here this driver is equals to whatever driver we get from here so I will say driver now to differentiate between this driver is what we have declared here in this class I can use the disk keyword then I say this store driver this will now refer to this driver here and then this driver is what we are getting it here in the constructor now you can see we can maintain a session whenever we say this whenever we create an object it will force the user to pass the driver object or the driver instance and the same session will be maintained while we are finding the objects and doing action on them also if you see now here it is showing me an error because now our constructor is getting an argument or asking for a argument which is a webdriver instance so if I now say here new login page and I press ctrl spacebar you can see now it is by default asking for a webdriver instance and I will pass the driver whatever I have declared here I will pass it here and now I can say I can use this variable login and I will say login dot so for this particular function we do not have to use anything because it is just navigating to the other page so let me just cut this from here remove this from here and I will put this in the next function where we are entering username and password and now here I can say login dot and you can see I am getting all the functions of that class enter username and here I can pass the hard-coded value from here or if you see here we are already getting this username and password from the feature file data table this is what we did in the last session so I am just going to use this variable that we are passing here so that it runs with all the values of the data table from the feature file I will say username and then similarly I will say login dot enter password and here I will use the password variable and now I no longer need this I can remove or just comment out these statements the same thing I will have to do on the next function which is click on login now instead of creating an object again for the login page I can declare it at the top at a global level so that it can be referred and referenced everywhere so I will just put this here login page login and then I do not need this I just need this login equals new login page and now I can use this in other functions as well so here I will just say login dot click login that's it and I can delete or comment out this statement and we also have a function where we are checking that logout is displayed let me create a identifier and method for logout button as well I will say BTN underscore logout and I will say by dot ID and the ID is logout and to create our method I will go and create a new method public void check logout is displayed and I will say here driver dot find element BTN logout dot is displayed so now here I can say login dot check logout is displayed and I can remove or comment out this statement so now you can see we are referring all the objects and their actions from this login page class and you can now also see the advantage all the object identifiers and the actions for login page objects are present at a single location in this single class these may be referred in 50 test cases or 100 test cases we now do not have to worry whenever we have to make any changes or let us say if this identifiers changes from the back end we just have to come to this single class and make the changes and it will be referred everywhere and wherever we are referring these objects and actions also just in case you are completely new with Java and you want to knows want to learn some basics like how I have created these objects like this here and some more details you can always go on my website automation step by step calm and here inside under programming you will find Java this will take you to the Java playlist on my youtube channel and here you can see all the beginner tutorials getting started with eclipse on my first Java program and you will find this very helpful object-oriented programming what is object-oriented programming and how to create classes and objects so you can refer this and now we are done with our setup let us go to step number five that is run and validate and here if I go to this login steps login demo steps so this is our feature file login demo dot feature file and this is our earlier step definition so to ensure that this does not run this particular file or this particular script let me comment everything from here I will select and press ctrl spacebar so that everything is commented and now this should run our page object model file or page object model class also just to wear fiii when I execute a trance this I will say as Phi a so and control spacebar to autocomplete a sprint statement and I will say Here I am inside login demo steps underscore P um and now I will go to the login time of each F I'll do a right click run as cucumber feature and in the last session we have setup this on a Firefox browser so it opens a Firefox browser goes to the application and you can see this is running with the data from the data table it will run one more time because we have two sets of data this is running the second time with a second set of data and this is done just to verify that it executed from our page object model class let us see and check and yes you can see it is printing I am inside login demo steps page object model so this is working fine now here we can also make all of these identifiers or these statements where we are identifying the element as private so this means that it will only be accessed or can only be accessed within this class by the methods of this class and cannot be accessed directly from outside so this is what we can do although it is not mandatory or we can also make this protected the web driver so that it can be accessed only from this class and this package or the subclass of this class so this is what you can do just in case you want to learn more about access identifiers in Java you can search for Java access identifiers or access modifiers and you can see all this we can use public protected default private so all this you can see here so if we make private it can only be accessed within that class if you make it protected it can be used by that class or can be accessed by the class the package the subclass but not global and if you do not provide any identifier that is default and it can only be accessed within the class and package and not even by subclass or global so you can read more about this but this is a kind of a best practice also one more thing is in page object model one validation that we can do is to verify if we are on the correct page and that we can do in the constructor so for example here I can check if I am on the login page and for that I can say driver dot get title and I can say dot equals and I can say what is the title of this page so it is test project demo I can just verify this test project demo and I can create a condition if this is equals to this or if this is not equals to this test project demo so I am saying not then I will say we have to throw a illegal State exception so this is exception I will throw illegal State exception and I will say that this is not login page and I can also say the current page is and here I can say driver dot get current URL so I'm saying this is not login page and I'm also telling what is the current page so this is what I can do so if I run this now let me check if this works I can say run s cucumber features so this is running fine that means our validation is successful this is a good practice so that you can verify before going further you are on the right page so we have done step number five as well and this is how we implement page object model I hope this session was very useful for you please do some more hands-on I will meet you in the next episode thank you for watching
Info
Channel: Automation Step by Step
Views: 32,776
Rating: 4.9649124 out of 5
Keywords: selenium cucumber page object model, how to create page object model, how to convert selenium cucumber to pom, what is pom, advantages of pom, how to implement page object model
Id: BKefIqoUD3w
Channel Id: undefined
Length: 26min 34sec (1594 seconds)
Published: Mon Jun 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.