All right so let's start today's session so today we're going to see some most important topic and which is not specific to test NG feature or it is uh very much important for Designing the Frameworks so page object model page object model okay so first of all uh let us try to understand what is this uh page object model and why it is so important to design the automation Frameworks so first of all let us try to understand we have so far created multiple test cases using test NG style like we have created multiple test
methods and we have executed through XML file and so on so that is a normal approach like uh without using page object model by using page object model so we can write test cases in two different ways so if I don't use page object model pattern so what are the uh problems or what are the challenges normally we face if you do not have any page object model approach if you don't use anything in your Automation and the same time if I use page object model in our automation so what are the additional benefits uh
we'll get so that We'll understand so first of all what is Page object model so page object model is basically a design pattern a design pattern means we can maintain the uh locators or the web elements in a separate classes so normally when you create a test case what is the test case contains test case contains a multiple things like we will have all the locators we will place in the same class and all the test methods also we will place in the same class and again in the test methods we will write the simple
code and also we'll put all the validations validation methods and action methods everything we'll include in one test right so we'll identify the elements and uh uh we will also interact with the elements which are called actions and after interacting the web elements we do some validation so everything we include in one single test we'll combine everything locators test methods then validations actions everything we will put in one single test right so this is without page object model pattern so page object model is not a framework so most of the people will think it is
a framework no it is not a framework it is a design pattern this is a Design pattern like which describes the way we maintain the locators the way we maintain the web elements so normally the test case and web elements we don't put together in one single class in the page object model pattern we don't combine these things we will split them into multiple parts so all the locators will have separate class and test methods having separate class inside the test methods we will have all the validations only validations okay and the locators will put
in the separate class so in the page object model is basically a pattern in which we will maintain the locators or web elements and test methods so these two we separate in the separate classes we divided them into the separate classes that is Page object model pattern so why we need to separate what is the additional benefit you will get if you separate how it will reduce our effort we'll see that practically uh within examples so let me show you a small picture here yeah all right so let us uh look at this picture so
this is without page object model pattern so what are the problems normally we have if I do not use page object model pattern uh if You write a test cases normally what are the problems you will face okay let us discuss this pattern first and then we will try to understand what is the advantage of page object model pattern okay so if you look at this these are all my different tests which I have written in test engine let's say test one test two and test three and so on so you can have multiple test
okay so let us assume my test one is in this I want to perform the login test in the test one I want to perform the login test in the test two I want to perform the searching test search test three I want to perform the advanc searching so different tests I want to do in the different test cases now when I do these testing when I write these test cases suppose I want to write a login test case to write a login test case we need to locate a web element from the login page
right we'll maintain the web element from the login page so while creating the login test we will refer the web elements okay and from the login page itself we'll refer all the web elements from the login page we'll put in the test and we Will write the test now when you're writing the test case 2 and here I want to perform the searching test and in this again I want to start from the loog Lo in page so first I need to log to the application and once I successfully log in I will get a
search box and here there I can do the searching validation so if you want to automate second test case how many pages are required how many web pages we need to interact with two web pages one is login page the other one is a search page so to perform the login test we need only one single page login page but if we want to perform search Bo search test we need two pages login page and and search page so in this test I will locate all the elements from the login page and also I will
locate all the elements from the search page two pages are required suppose if you automate the test case three and here I want to perform testing on advanced search and to go to the advanced search first we need to go to login page then we need to go to the search page there I can find the advanced search options so how many pages are required to automate test three three pages are required login page search Page advanced search page now if I look at this particular scenario to automate login test we are identifying the web
element from the login page when you automating test to two we are identifying the web elements from the login page as well as search page similarly when you're automating test three we are locating web elements from the login page search page advanced search page if you look at this particular approach there are two problems we can identify so one problem is let us say the login page web elements we have to identify or locate multiple times in multiple T cases so in the login test one I have to locate login page webs again in the
test two login page is required so here also I can write one more time all the locators login page locators elements we can specify one more time even the test three also again one more time we have to write a locators for login page also search page so this is a problem so this is a first problem so duplication of elements or locators if you have just one or two test cases you don't see much different but if you have a huge number of test cases 150 or 100 test cases then this is the biggest
problem Because identifying the elements in one page one time is not enough so same elements same locators we have to again return multiple times in multiple test cases why because test cases are independent test two is not dependent on test one test three is not dependent on test two so we cannot achieve any reusability here so same elements we have to locate in first test case second test case third test so login page we have to locate three times search page we have to locate two times right so this is the first problem duplication of
elements and locators now the second problem okay suppose I have located this login page elements uh in three different test so I have located these elements or you have written the locators for login page in three different tests tomorrow something is got changed in the login page itself suppose some attribute is got changed or some xath is got changed for that particular element then what I should do I should go and modify that in every test case Okay Suppose there is a input box for login and the attribute or some xath is got changed for
this input box because locators are written in every test so we need to go and Modify that particular element specific element in every test method this is another problem updation so these are the two major problems normally we have if we do not use page object model pattern so to overcome these two problems page object model pattern will be preferred always in our automation Frameworks or automation projects okay everybody's understood these are the two problems if anybody ask you during interent you should able to confident explain this so why we need page object model pattern
can't we do framework or can't we implement the framework without having page object model so we can implement but these are the problems we will face normally okay okay now if I use page object model pattern how we can overcome these two problems how we can overcome these problems by using page object model pattern now let me share another picture yeah yeah so let's look at this this is a page object model pattern so how we can overcome those two challenges by introducing page object model pattern yeah so in the page object model the main
concept is we will divide the locators and test methods in two different classes so as of Now when I create our test case which contains the two parts locators plus methods test methods right everything we include in one test case but in the page object model pattern we will divide them into two different so locators we will maintain in a separate class and test methods we will maintain in the separate class okay locators and test methods we will split into two different classes in the page object model pattern so this is the main crucial step
so when I divide this into two different classes then what are the advantages we will get out of this page object model let us see this let us say in the page object model first step we will try to identify how many pages are there in your application according to the test cases how many pages you are going to interact so first we need to find out let us say I have a three pages in my application login page search page Advan these are the pages P1 P2 P3 I'm just calling these are the pages
on your web page or web applications so I have three pages now in the page object model initial step what we will do is for each page whatever how many pages you have in your application for every page we will create a Dedicated class suppose you have a 10 different pages in your application we will create a 10 different classes so each page is having their own dedicated class which we have to create and what this class contains this class we will specify only the page elements what are the web elements are there is in
this page all the web elements we will keep in this class along with this web elements or page elements we will also write action methods suppose if there's an input box we will write at that page element plus send Keys method that's an action method Suppose there is a button we will identify that element and also we'll perform the action method called click so the page object class contains page elements along with action methods and which doesn't have any test methods doesn't have any validations so test methods validations we will write in a separate class
that's a main class okay so for every page in your application there is a dedicated class which you have to create so for the login page we will create a separate page object class which is called as a page object class which contains only page elements corresponding Action methods and these action methods we have to create our own methods and similarly another page called search the this particular page we will create another page object class which contains elements only from the search page we should not combine so when I create a page object class for
login page it should contains only login page elements we should not add any other elements from any other web pages so it should contains only login page elements and action methods similarly for search page we will get a separate page object class which is also contains only element from the search page and action methods same thing we will repeat for all the pages which are available in our application yes just we have to put only elements and we will write our own action methods again this is a this we can create in multiple ways multiple
approaches are there I will explain them slowly first understand the concept theoretically then we will Implement them practically so the page object class contains only web elements and action methods remember this point first step we will identify all the pages second step we will create a page object classes For each page now the third step test case class we have to create as usual we will create a test NG class right so we will create a test NG class in this test in this class we will have only test methods which contains the validations and
we don't write any elements we don't identify any elements we don't write any action methods because we already written them in the page object list so in the test methods what you will do now in the test methods we will try to get these elements from the page object class and action methods also we will call or else we will just call only action methods and these action methods will perform actions on the web elements after that we do validation here that means our test case will actually talk to the page object class and our
page object class action methods will interact with this page elements and perform corresponding actions on the web page this is the process and the login test when I create a test case which contains only test methods and the validations so no page elements and no actions nothing only test methods and validation so this is very very easy to create the test case now why because we already Separated page elements action methods so 80% of the job is done in the page object class itself only 20% of the task that is writing a test method adding
validations we do in the actual test case so this is the way we need to follow in the page object class uh page object model pattern now if I look at the second test case let's say search page there is a separate page object class is there same Advan say there is another page object class so how many pages we have those many number of page object classes which we need to create every page object class contains their own page elements and their own action methods now how we can overcome the problems which we have
discussed earlier like a duplication of elements and updation problem so how we can overcome those two problems in page of jet model pattern if I look at this approach login page elements we are identifying only once right we put in the page object class corresponding page object class and search page elements we are identifying only once we keep them inside the another page object class similarly advanc search page we are identifying elements only once and keeping in another page Object class so at the time of writing login test which page object class we have to
interact only one page object class we are calling this action methods this action method will interact with the page elements and then it will perform the action so the login test we are using the we are using page elements or locator action methods from the page object L because here we need only one page now when you come to the second test search test how many pages we we have to interact here is two pages one is login page the other one is search page so the login page elements we have to get from the
login page object class and search page elements we we will get it from search page object so we are getting the elements from the two page object classes okay and here we are not going to get any elements or anything we have only test methods and validations but we are getting the elements from two page object classes okay and login page elements we have written only once in the page object class so that we are referring in the test one the same element we are referring the test two same login page elements we are referring
in the test three also so once you write your all Locators and action methods we can reuse them in multiple test cases multiple test cases similarly if I want to automate Advanced research three pages are required Elements which are related to three pages which are required so I'm getting the elements login page element from the login page object class I will get a search page element from the search page and I will get Advanced page element from the Advan so here I'm referring three page object classes to get the elements so in this approach how
we can overcome those two problems which we have discussed first problem is a duplication problem right so here there is no problem of the duplication why because the login page elements we have put in one single page object class the same page object class we are referring in multiple test cases so reusability is achieved so there is no duplication one problem is got solved the second problem updation suppose if there is any change happened in the login page or any other search page or any other page so that particular change we can include only in
the particular page object class only one place only one place we will update suppose any xath Is got modified any any element attributes are got changed so whatever changes are happened in this particular page those changes we can add corresponding in the corresponding page object class only one place one time that will automatically reflect in all the test cases because we reused the same page elements in all the test cases so if you update in one place that will automatically reflect in all the test cases so updation problem is also got solved the first problem
is duplication second problem is a updation if I do not use page object model pattern so we Face these two problems when I use page object model pattern we can avoid these two problems if you have one or two three test cases you don't see much difference and moreover while using page object model pattern you will feel some difficulty because you have a less number of test cases you need to do lot of things but if you have a huge number of test cases then this particular approach will be very very beneficial okay so everybody's
understood about this page object model what is Page object model if I do not use page object model what Is the problem when I use page object model what are an advantage you will get everybody's clear this is a theoretical part so once you clear about Theory then I will jump into practical part so the benefit of object page object model is we will maintain a separate page object classes for every page in your application and whenever you need element from print test cases we will get them from the particular page object and the main
concept in the page object model is we will separate element and action methods in one class test methods and validation in another class and we don't we no need to execute this page object model we just execute only test case this test case will invoke the page object class and the page object class will interact with the application okay so this is the page object model pattern we will always prefer to use in our automation framework or automation projects so without having this kind of pattern we could not find any kind of Frameworks okay so
this is a very very important so without using this there is no framework available so we have to use this Particular pattern in our automation Frameworks now let us jump into the Practical part we'll see how we can create our page object class how we can create the test case and how the test case will interact with the page object class so all these things will see practically one by one and this is the approach we are going to follow in our realtime projects also okay now so let us see how we can create test
cases using page object class approach so while creating the test cases using page object class and the page object class we can create in two different methods page object class also we can create two different ways One is using page Factory use without using without using page Factory by using page Factory so there are two most popular approaches are there approaches to create page object classes there are two approaches we have to create a page object classes one is by using page Factory another one is without using page Factory so this page Factory is basically
a class redefined class provided by selenium web driver itself to create a page object classes even if you're not using this page Factory class we can Still create a page object classes so first we will see without using page Factory how we can create a page object class okay so now I will take one test case so so what are the steps uh we include here first we need to create page object class then we have to create a test case then we have to create an XML file and XML file is optional if you
can if you want to run the test case directly you can run it or through XML file also you can run it so the workflow will be like this let us say this is my application under test and here this is our page object class and this page object class interacted by our actual test case class so when I run the test case it will interact with the page object class the page object class will interact with the application again if you want to run this test case using XML file you can still run it
so now XML file execute the test case and test case will interact with the page object class page object class will actually perform the actions on the application under test so this is the workflow okay now we'll see first we'll create a page object class then we will create A test case and if possible we can also create an XML file and run the test that is again same process earlier we followed right so we can create a page object classes in two different methods one is without using page Factory means we don't use any
page Factory predefined class and second approach is by creating by using page Factory so now let me show you practically how we can do that fine so let's go to eclipse and create new package day 47 okay so first step we need to identify the web page and we need to keep add elements in the page object class right so what I'll do is I will take this orange HRM application again you can take any web page so I'm taking this particular page okay so I want to perform login test first let me take a
simple one okay let's take a login test so if you want to automate this login the login page is required and in the login page what are all elements we required to perform login test what are elements we required in this how many elements we required only three elements Username password login that's it right if you have these three elements we can interact with the web page so when you're creating the page object classes according to the test case what are all elements we required only those elements we will add to the page object class
so you don't need to add every element suppose in this page there are some social icons there are some links there is image there a lot of things are there but for login test I don't need all the elements only few elements are required initially so in the login page whatever elements are required initially for your test case you can can just add only those elements but later on suppose if you're automating some other test cases which are required these logo social icon and everything then later on you can add other elements to the same
page object class so in the day one itself you don't need to keep add all the elements in the page object class not at all needed but initially whatever elements you require just add those elements to the page object class okay so URL is a different this we will put as part of the test method okay we should not include URLs All those things in the page object class remember page object class contains only page elements and corresponding action methods other than this we should not put anything validations we should not put URLs and other
things a test data all those things we should not put only the web elements and corresponding action methods we have to include in the page object class okay and according to the test case first you need to read the test case and what are elements you require to interact add those elements to the test okay so now from this particular login page I will create one page object class and in that I will add these three elements okay so let's go to the eclipse and I'm going to create a page object class so go to
the new class and I will name it as a login page login page okay so whenever you create a page object class remember very carefully that there are three main sections we need to create three main parts any page object class it can be using page Factory or without using p in whichever approach you used while creating the page object class which includes mainly three parts the first part is called locators we need to write all the locators For the elements and before writing the locators we need to write something called Constructor there is a
one Constructor we have to write for the login page so what is this Constructor will do is this Constructor will initiate the web driver because if you want to locate a web element we have to write a find element method when you write a find element method we need a driver also right so we will first write a one Constructor and this will initiate the driver automatically second part is we have to write a locators and the third part is we have to write an action methods action methods we have to write so every page
object class contains these three sections one is Constructor locators list list of all the locators and action methods so what is the purpose of writing the Constructor is it will initiate the driver and locators all expar IDs whatever you want you can put them and action methods by using the locators we will try to locate element and perform the action method so these are the three sections we will write in every page object class right so how to create create a Constructor and what you will do in This Constructor Constructor name should be same as
a class name right so let us take the class name login page and what this login page will do is this login page Constructor will initiate the driver so I'm creating one web driver variable here because I'm making as a class variable so that the same web driver I can access in multiple action methods okay so I created this VAR so to initiate this driver using Constructor what I will do is I will also get the driver from the test so this driver I will pass from the test case later and this particular driver I
will assign to the local class driver how we can assign this one I will assign to the local driver how we can differentiate can we write like this driver. driver can we write like this driver equal to driver no why because this driver is a local variable this is a class variable so the name of the variable is same so we need to differentiate so this is a keyword we have to use this. driver equal to type so this we will create the object of login page later in actual test method so from there we
are invoking the Constructor this will automatically invoke at The time of invoking this Constructor we will pass this driver parameter from the test case and this will create the driver instance whatever driver we are passing the same driver is assigning to the driver variable so the Constructor is doing this particular job it will initiate the driver okay now the second part is called locators how to write the locators just observe this let us say go back to this and I'm locating inspecting the username element and if you go back to this username this is the
exper let's say this is my expert earlier how we have located this web element earlier in the previous approach this is the process guys driver dot find element and by dot XPath which whichever attribute xath Name ID CSS whatever you can put like this XPath so this is the way we can write so this is the locator that we are storing into the variable let's say user txt box and type of this variable is web element so this is how we have written earlier so we first identified the element stor in that element in a
variable and we will perform some action on this variable right this is how we have done earlier now in the page object model pattern How we have to write a locators in which format you have to write a locators just observe in this particular statement there are multiple things are there so if you look at this particular statement who is identifying the element who is identifying find element right so this is a find element not driver find element is a method is trying to find that element how it is finding the element find element is
a method which is finding the element okay fine on what basis it is finding the element based on the locator so whatever we specify in this particular box right this is entire part is called locator okay so the find element is a method which will identify the element based on the locator we passed so whatever is appearing in this particular bracket this particular part is called locator okay now I will split this entire statement into two different parts observe very carefully this is a crucial step so I will separate this locator like this okay now
I will store this locator in a variable I create one variable called like this so this particular part I will store in a variable now what is the type of this variable this is just a locator it cannot Locate an element okay when I use find element then only it will identify the element till the that time it is just a locator and if I store this locator in a variable what is the type of this variable can you guess what type of variable it should be it can be here you can use ID or
css xath whatever it is ultimately this particular locator method will return by by is a return return type by is a return type not a Stringer so these are all different objects okay by type of variable it will return so by. xath if I put only locator separate if I separate this locator part this locator will always return something and that variable should be buy type so this is called locator this is called a locator now in this next statement I will write like this driver. find element okay and inside this I can pass this
variable or not I can pass this variable right so the locator I have stored in a variable and in The Next Step d. find element and this locator I'm passing in this is it exactly same or not so whatever I'm doing here in single step I'm doing the same thing in two different steps right in the first step I've written the locator in the Second Step I'm finding the element I'm finding the element then if you want to perform action you can perform perform the action I'm I'm using send Keys method here send Keys some
value I will pass so the same statement I have split into two different parts the locator I have stored in a variable and after that I'm using find element of locator do send Cas so in the locators part whatever section I have given in this locators section we have to write locators only in this particular format in this this particular format we have to write all the locators okay and this part we will include in the action methods this part we will include in the action methods okay so let us try to write all the
locators in this locator section so first part username input box in inspect this element and you will get this username get this username and write a locator so by. xath you don't need to write any uh find element or find nothing so just by. X paath you can write okay so it will give you some methods like this but you need to just correct this okay so by dot X and here in the double course you can put the TT path and stored this in a variable so here what is this this is input box
so TST uncore I can say username and what is the type of this variable it should be and this is not username element this is a locator so I can say locator only locator it is not a web element this is not a web element this is just a locator okay type of this variable is by fine similarly we will also write the locator for other two elements so for password username and password uncore locator and this is a button right so I can say BTN button and I can say loginor Lo whenever you give
the names give always the meaningful names okay because by seeing the variable name you have to know what exactly it is this is a txt txt means an input box or text box username text box and locator so these three elements we have to write an X paath properly so again I have to go to the next element password and get the password element go back and then specify here this xath for the password now again go back to the application go to the login get the xath for the login and place it over here
like this we have to write all the locators and Instead of xath you can put any other locators if you have ID CSS whatever you want again same process and the return return type is also same if I use ID or xar css whatever it is the return return type is Same by so like this we have to first create all the locators okay here there is a challenge here we have only three elements so we have successfully located each element and I put them here suppose if have a more number of elements you have
10 elements or 20 elements in a web page and it is very very complex page it is very very difficult to go and find each and every element and capture the xath is a repetitive task right it takes a lot of time so we feel some difficulty in this because Iden first we need to locate element capture the xath and copy the xath here and then again go to the web page and try to locate another element again capture the xath again put it here so this is very very tedious task if you have a
more number of elements so to simplify this task selector Hub is provided few options we can easily create our page object class okay so let me show you how we can create locators for all the elements in one single shot Very very easy to do this let me show you how we can do this go to selector Hub in the selector Hub there is a option there are some options here you can see here the second part set driver command to append on selectors okay so you can just click on it here it will ask
you the format it will ask you the format in which format you want to generate the locators you can set the format in whichever format you want suppose I want to generate the format just like this I remove this part find element I don't need only by. xath and xath value just keep this format and also so I'll remove this pack so this is the format I want all the locators right this is the format I want all the locators now you set this format so once you set this format okay now just observe it
is started generating locators in this format so now onward you can locate any element suppose I'm locating login so this is the xath so you can see I'm locating username now you can see same format it is giving right and uh first we need to set this for format the second part is now you can just go here third option click to generate locators page and multiple selectors third option Click on it you'll get a small page here so you can just try to inspect all the elements one after another so now what I'll do
is I will take this particular inspector and what are all elements you want to inspect just click on it first I need username and after that I need password after that I click on login it's not started just a second so let me refresh the page yeah now we can see it is automatically generated let me clear this first again I'm going to create so click on this Arrow Mark take the first element click on it see automatically generated now again click on the Arrow Mark and click on the next element and arrow Mark click
on the login see all three elements are captured just to copy copy here you can see copy option just copy it go back and just place it here that's it the variables you have to create so let's say by txt underscore username underscore locator variables we need to create so very very simple right so we can create locators for any number of elements in simple manner very very simplest way we can do it so useful or not very very useful right so instead of going to each and every element And writing the locator copying it
unnecessary stuff so you can just capture all the elements in one shot and use this copy option and get all of them into your page object okay but in which format you want that you need to specify so again one more sh suppose currently I specifying this format again I need another format for example same it will work for other things also Name ID also you can put here okay but most of the times we prefer them we always prefer only xar okay so suppose uh I want to get this locator along with the find
element method okay for example I want to get this locator along with the find element so then what we can do is just clear this delete all of them and you need to set the format again go to the second option and previously uh go to the second option this is the format we used now change the format if I click on it it will show you different formats in which format you want and this is for Cypress this is for Python and this is for Java so like again change the format now I need
entire locator along with the driver. find element so once you set the format and again it is already started so now try to inspect this element Usern name and then password okay and now you can see login button and once you enter just copy and go back and paste here see this along with find element everything is got generated so these are the very very useful features we will get from the selector Hub and if you go to the selector Hub pro version you'll get some more advanced features so in this what exactly we are
doing is we are just set this format whichever format you want we set this format so I can again remove this part so this is the format I want and I want to locate I want to write locators for multiple elements so once you set this format you can it is already started this one and now inspect first we are inspecting one element generated after that again I have to take this inspector button again I have to locate another element generated again I have to take this inspector again I have to locate element again in
this there is is some reputation process involved what is the reputation process we need to inspect each and every element before inspecting every element every time I have to take this inspector option so every time I have to take this inspector and element identify again Inspector again one more element again inspector another element you have to take this inspector multiple times for multiple web elements so in advanced version in the pro version we can also avoid this reputation I already have the pro version just for your understanding I will show you just reason I'm going
to selector H pro version so in the pro version uh basically you no need to click on this inspector button so the what is the main difference is in the pro version again you'll get the same kind of a window everything is same but here you don't need to click on Arrow Mark every time and locate element just one time you click AR Mark and just whichever element you want you can click on suppose I'm click on here then click on here then click on here click on here like this you can just keep clicking
on all the elements one after another then then it will automatically generate the locator so that's one Advanced feature which is provided in the selector Hub So currently I don't have a login I cannot use it okay so this is very very helpful feature from selector Hub remember it is very useful when you're creating a page Object classes it will reduce lot of time lot of effort for us if you don't have this feature it's very difficult it's take a lot of time to create a page elements especially locators okay and we need to write
our locators that's the one part and other part is we need to get them and put them inside the code is another part so it is very very tedious task so try to use that particular feature whenever you're creating the page objecs okay so once it is done you can just stop this option and everything is gone and again come back to the previous approach like this and find it okay okay so and if you want to reset everything so currently this different formats have set now again you want to go back to the previous
one default so you will have a reset option here can just go back and if I just go back to the settings now here the last option so click to reset all settings to the default if I click on it again everything will be reset just refresh the page now again it come back to the default one so xath is got generated so these are the most important features you can explore from the selector fine so now we have created One Constructor first part is done second part we created a locators this part is also
done because as of now only three elements we required for login I just added them now come to the action methods how to create an action methods for these three elements so how many elements we have added here so those many action methods we have to create so every Loc we have to create specific action method okay so for example the first action method I'm writing make everything as a public so vo set username these are user defin methods so you can give any name to this method but it should be more meaningful when I
when I read this name you have to know why this method is got created set username so this method will set the username and this will I will pass one parameter web element okay web element I will pass into this and what is that web element I will pass is uh sorry not web element username string uh username I will pass okay and this method I will call from the actual test case okay and set username and it will take username as a password okay and this particular username I will try to pass into this
particular element first element so here will Write driver. find element and which locator you have to use this one so use this locator dot send keys of whatever username I'm getting here that you can pass it that's it so this is the action method for username input box so we have taken the username as a parameter and we have located this element by using find element and we pass as the username as a parameter and similar way we have to create another action methods for password also so let me create another action method public void
set password set password and this method also we'll take password parameter so that we will send through test case so string PWD now inside this I will try to locate element driver. find element and uh locator we already located here so just pass this variable element dot send Gees PWD so whatever password I'm getting here as a parameter I'm just passing it so second action method is also done now the third action method for button for clicking on a button public void click click login and here get the element driver. find element and here what
is the locator we have to use this one bdn login locator dot so which action we have to perform Click action just say click action that's it so this is the way we have to create action methods so first part is a Constructor second part or locators third part or action methods so these action methods make them as a public why because these action methods we can call from anywhere in your project from which from whichever test case you have anywhere from your project we can do it okay so this is how we need to
create a page object class for login page we have created so what is the next step we have to create a test case now writing the test case is very very simple because almost 80% of the job is done through Page object lass so let's create the test case now go to new class and I'll name it as a login test login test and say finish Now by using this page object class we have to create our login test okay so this my login test this is my page object class let's keep them side by
side so now by referring this page object class we have to create our own test case login test Cas so let me do create a test case so first I will create one method void setup and this method is responsible For what I will execute this before class only one time right so I'll execute only one time so I'll mention this is the before class annotation and this will do the setup part setup part in the sense it will create the browser and launch the browser and opening the application will be done by the setup
method and I will rate another method void test login so this is the actual test method to perform the login and this I will specify as a test annotation and import this test annotation and the last method I will write vo tier down and this will close my browser atate test ation this is after class because I want to execute only once so three methods I have created and also we have only one test method so let's us implement this with the web driver code we need a driver which is required for all three methods
right so I will create web driver variable as a class variable web driver driver now import this web driver so forget about this login page now forget about this I will show you how we can use this in our test so now in the setup method we have to create the driver so I can say driver equal to new Chrome driver so we created web driver instance using Chrome driver And then if you want to put you can put implicit we driver dot manage dot timeouts dot implicitly wait duration dot of seconds second can said
10 seconds done now we'll also launch our application I can say driver. getet and here I will specify the URL of the application okay done so this is how we will do and also if you want to maximize the page you can try to maximize driver dot manage dot window. maximize so all the setup related stuff we can put in the setup so done now come to the actual so here we will close my browser so driver. bit so now come to the actual test method so what are the things we need to write in
the test method nothing very very easy so we need to just call these action methods from the test method so the test method will just invoke these action methods and these action methods will internally interact with the elements using the locators and perform the action very simple so now if you want to access this action method how can we access from the login page I want to access these methods how to access from another class we need to create an object for login page right so let us do that so in the test login I
will say Drive I'll create object of the login page okay so how to do this login page LP equal to new login page right login page LP equal to new login page and uh in this login page we have already created one Constructor and at the time of creating an object The Constructor will automatically invokes but the Constructor is expecting the driver parameter right so whatever driver we already created in the test we just pass this driver okay now here the Constructor will be invoked the driver will be initiated in the page object class now
the next step we need to just call this action method so now LP dot set username and here we will pass the username I can pass admin and then what happens from the login page it will invoke the set username action method by passing the parameter so this Butler method will be triggered now what this method will do now it will receive the usern name in the variable then it will get the element by using locator then passing the value so single step we'll do the job same thing we will repeat LP dot set password
and here we will pass password it will also invoke set password method from the Page object class pass the value locate element and passing the value same thing we repeat for login button LP dot click login no parameter is required so we have called three action methods from the page object class so these three action methods will invoke these methods from the page object class and each method internally get the web element and perform the action okay so once you click on the login we do the validation also right so here you will put some
validation okay so I'll also put the validation so validation is always part of the test method so here I will WR one assertion asset dot asset equals asset do asset equals I'm verifying the title of the web page okay so I can get driver. getet title so dynamically I'm getting the title driver do get a title so that should be equal to what orange HR okay done clear everything very very simple right so because almost 80% of the stuff we have done through Page object class so in the test method which includes only the action
methods we are just calling and we are doing assertion that's it very simple okay and first setup method Will execute then test login will execute which will again interact with the page object class and finally TI on method will execute very very simple so let's go and execute and which one we have to execute page object class or test case class which class we have to execute we have to execute test case not the page object class so page object class will be referred inside the test case okay let's execute now I'm closing my login
page and then execute run as test NG test so now you can see it is launching my browser going and opening now it is enter username and password then my test is got passed perfect so this is how we can create our test cases using page object class step number one we created new page object class which includes a Constructor locators action methods suppose tomorrow if you want to add some more elements then you can add some more locators some more action methods is a continuous task it is not onetime job okay so for now
I have just added only login test case so these locators these action methods are enough but tomorrow if you want to uh verify this logo Element that is another test so then what you will do you will add this element to the page object class the locator you will add and also one more action method also you need to add for every locator there is a corresponding action method you need to create so what Constructor is doing is very simple so because at the time of creating this login page object we are passing the driver
whatever driver we created in this test we are passing the driver so then the Constructor will invoke it from the page object class and here it will receive the driver same driver whatever parameter I'm passing same parameter I'm receiving here and initiating the local driver because whatever driver we created here this driver will not work for the page object loss because both are two different classes right so that's the reason whatever driver we passed here we created here we're just passing here and receiving here and assigning here so we're getting the driver from the test
case and assigning the same driver to the page object class driver why because this driver is required to find the web elements here everywhere the driver is required so page object Class is also required a driver so that is the reason we are creating the driver in the setup method we are passing to the Constructor and using this driver instance in everywhere so this is the concept so everybody's clear on this this is the one approach this is the first approach to create page object class and once you create your page object class how we
can use it for writing the test case okay now so this is the first approach without using page Factory and now I'm going to show you by using page Factory how we can create a page object l so the writing the test case will not change the process is same but I will show you another way of creating the page object class there is another way we can create a page object class so some people will follow this approach some people will follow page Factory approach it depends upon the people and project because you have
to aware of both approaches because in your company whichever approach they follow so you can adopt that particular approach all right now let me show you another way of creating the page object class so the test case will not change test case exactly the same just I'm showing you Another way of creating the page object class so I'm taking another copy of the same login page I'll name it as login page one previously login page now I'm just changing login page one okay or else you can just log in page two okay so this is
the page object class which we created so in the new approach in the page Factory approach again have three sections Constructor is required okay and locators and action methods also required but what is a change which we need to do is locators and add action methods so in the second approach when I use page Factory approach we no need to use find element or find element methods so far if you want to locate if you want to identify any element or if you want to find any web element we must use find element right find
element for single element find elements for multiple elements so far we have used but when I use page Factory approach we can avoid find element we can avoid find elements we don't need them how we can avoid let me show you so in this I'm just removing this part okay or else I'll comment this part we are writing The locators in the different way even action methods also will write a different way for now I'm just commenting this okay let's comment yeah now we'll see first we need to create the Constructor how to create a
constru Constructor in this so the way of creating a Constructor is also little bit different so how to create the Constructor in this we will create the same name we have to give web driver we have to receive but internally what you will do is this. driver equal to driver is also same but additionally we will add one statement that is Page Factory class we have to use page Factory this is comes from web driver itself selia web driver page Factory dot there is a method in this called init elements page Factory dot init elements
in this we have to pass two parameters one is the driver the other variable is this this is the additional parameter which we need to pass I will explain what is the statement at the end for now just ignore okay just we need to add additional statement in the Constructor that's one change now come to the locators part earlier we have put we have captured only locator and store into this variable And this variable we have referred inside the action method so now what we need to do is we will take the same kind of
an xal same thing but we will write a locator in different manner so we use something called find by annotation find by in the bracket we directly specify X path so I can say x path equal to whichever element you want to find you can specify that X path here directly you can specify in the double quotes Okay so you can directly specify like this so what is the meaning of this so find by is an annotation which will try to find the element by using this xath locator once you find an element we have
to store that element in a variable so here we will create one variable called Web element txt uncore user name and this web element also we need to import this is one full statement and this find by also we need to import from org. open. selenium so let me explain what is this statement so this statement includes locator and also finding the element both but earlier we have just specify only locator and we find the element inside the action method we have written another find element method to find that element but here but here we
do not have this Particular thing find element we are not using instead of that we are using something called find by annotation in this we can just pass whichever locator you want you can pass X paath or you can pass ID if you pass passing ID you can directly say ID equal to ID value if you're passing name you can directly say name equal to the value of the name whatever locator you want you can if you want to use CSS then you can say CSS equal to and the value of the CSS selector so
we can directly specify the locator here inside the find by and this will find that element once you find an element that we are directly storing into this variable equal to and all not required here so this will find that element immediately it will store in a variable and you can put this in the next line also if you want so this will find that element and store into the variable now tell me this variable is a web element or by by top variable or web element type of variable is it correct or not is
it a web element variable or buy Dot variable it is a web element why because we already specified locator here and find anotation is already finding that element And returning that element so this variable should be web Bel in the same process we have to write located for other web Bel so for username or password also we need to get the locator for password right so get the password element so this is expath for the password and then I can put here right so this is txt password element password web element and similar this one
also this is a button so let's get the element for the button and this is a button element capture it and place it here you can use all the locators okay this is BTN underscore login this is the way we have to create the locators so in this identifying element is also included locator is also included both so finally we got the elements directly we get the elements now how to create an action methods so let's create an action methods so the way of creating action method is exactly the same but only the difference is
we just remove the find element not at all needed okay so remove this part but we can pass the value here we can simply get the web element we already have this web element right username use this web element dot send keys of pass this username that's it so we haven't use To find element because we already captured the web element here so you refer the web element do send keys and pass this user name same thing we'll repeat here also remove this part and directly get this web element txt password do send Keys password
and same button login get this web element remove this and put the web B that's it this is another way of creating the page object class very simple this is more flexible and more important most of the people will prefer this approach okay so first part is Constructor second part is again locators third part is action methods the only the difference is pre L we have written only the locators and elements we have identified inside the action methods using find element method but now find by annotation will take care of it so by using the
locator find by annotation will automatically capture the elements element and store in here so we just refer those elements for example if you want to find group of web elements then suppose I want to capture all the links which are there in this particular web page okay I want to capture all the links from this Butler web Page so how we can capture them so normally how we will use find elements right find elements of common xath or find elements of tag name we will use something but how to capture multiple webs so let me
write the locator for multiple webel I want to capture all the links then how we can say at theate find by and here you can specify suppose I want to locate all the links how we can locate tag name right with the tag name what is the tag name is yeah so that you can directly specify so tag name equal to a that's it and then we can store them in a variable so links and what is the type of this variable Now list of web element got my point so this is the way we
can capture group of we and why it is giving tag name is element because this is a k sensitive tag name okay so like this you can specify so this variable contains a group of web elements or multiple web elements so single web element we can locate group of web elements also we can locate using single annotation which is find by everything is depends on the locator what we specified understood everyone so this is a way now finally we have to understand what is this Page factory. init elements see when I finding my web elements
when I use find element or find by are we using driver in this anywhere driver because driver is must right if you want to inter if you want to locate an element or if you want to interact with this element driver should be must right but have you used any driver in this anywhere here are we using any driver no so without having any driver find by annotation is trying to find the element internally how it is possible because of this statement so what this statement will do is from the page Factory page Factory is
a predefined class which is already available in the selenium web driver in this we have to call one method called init elements so what exactly this method will do is this particular method will make sure this Butler driver is applicable for all kinds of elements internally find by elements so this driver is applicable automatically applied on every locator internally so that we cannot access that we cannot see but this particular method will try to initiate all the elements using the driver which we passed that is the Meaning of page factory. init elements okay so the
init elements is a method which will initiate all the elements using driver okay that much is enough just remember this point and this statement is mandatory you cannot skip this but in the previous approach this statement is not required but now we are using this we are created this page object class using using page factory model using page Factory approach that is the reason we have to say the statement this is mandatory required if you do not specify this this won't work page object lass will not work okay so this is how we need to
create a locators and actions so should we change this login test now is it really required to change the login test according to the new page object class is it real really required to change no right why because in the test we are just creating an object for the login page and we are just calling the action methods only one change we have to do what is that instead of keeping login page one you need to just login page two that's it this A only change because we created another right that's it so rest of
them are exactly the same no change in the test Right let's execute our test case and see whether it's working or not right click run as test NG test perfect now is launching my page entering username and password and test is completed okay so this is another way of creating the page object class using page Factory and most of the times I have seen this is the approach used in all the automation projects but in case in your companies if they follow any another approach you can adopt that approach okay but if you're newly creating
a project first project new project from the scratch then I'll prefer to use this particular approach page Factory approach suppose if the project is already existed they already used previous approach okay then you can prefer that but initial days of selenium they have not provided this page Factory in the older versions of selenium so initially they have not provided this so that is the reason people were started creating the page object class in this approach the first approach so later on they have provided page Factory so from then onwards they have started creating the page
object um page object class using find by annotation okay so most Of the companies most of the Frameworks we follow this particular approach okay so if you still want to run this test case using XML file still you can run it so let's create another XML file this login test in XML file we don't need to specify the page object classes okay in XML file we'll specify only the test case class let me create an XML file to test NG and I will create this XML file inside SRC inside the test inside the Java inside
the day 47 say okay and then finish now I created one XML file which contains the test case login test now we can execute from here run as test Eng it is started my test perfect yes now it is got executed right so what is a work flow so how it is got executed first we have application under test so same process application under test so we created page object class which is actually interacting with application under test and this page we have interacted with our test case class and the test case is executed through
XML file so entry point is here and the test XML is executed test case class our test case is internally calling page object class action Methods and Page object page object action methods internally used all the locators and then it is interacted with the application so this is a approach we follow in our Frameworks in our project okay so these are the two different approaches which we have to create page object classes now I will show you a small variation in this uh in the second approach we go back to the second approach in this
we directly specify the locator like this right just by adding keyword xath equal to xath suppose if I want to use ID then you can say ID equal to ID or if you want to use name you can say name equal to name like that right instead of specifying the locator directly like this we have another way to specify the locator so let me show you that for example let's command this and what I can do in this is remove this part this is just another flavor if you want to follow you can follow otherwise
if you feel difficulty you can leave it here we will use something called a keyword called how here we can use some keyword called how Okay small case how equal to and there is a predefined class Called how another class here this how is just a variable and here how is a predefined class in this if if I click on the dot here you will see all the locators different options class name CSS id id or name link text name partial link text everything is there so you can take one of the locator I'm taking
X paath how equal to how. xath comma this is parameter now the second parameter here we have to use another keyword called using equal to in the double CES we have to specify the X this is another way of specifying the locator so how is a keyword how equal to how dot here how H is a capital letter how dot here you can specify which locator you want to use comma using is also another keyword equal to and the value of the XO you can keep in the double CS this is how we need to
write so you can follow this one directly or you can follow this form so some people will also use this notation so by seeing this you should not be confused that's the reason I'm saying it this notation But whichever is preferable you can prefer that which one easy for you guys first one or second one which one is easy first one or second one first one is Always easy right yes so you can go with that no problem but somewhere in some projects if they have used this particular notation just you need to follow this
okay so how is a keyword equal to how. xath or how. ID how. tag name whatever you want to use and using is a keyword and equal to specify if you're using ID here then in this you have to specify ID and if you are using CSS here then here you have to specify the CSS value like that so this is another way of locating web elements using find by okay so this is how we can create page object class and test cases so understood everybody so please confirm in the chat box everyone so without
using page Factory using page Factory okay but in some Frameworks very rare case some Frameworks they didn't even use page object classes instead of that they will keep all the locators in the properties file in the properties file in the last class I have shown you how to read properties file right key and value PIR so they follow the first approach but instead of creating a Java class they create all these locators in the properties file and from the properties file they will get The locators and implement the action methods but that is not preferable
that is too lengthy process but this is a most preferable approach either go with the first approach or second approach okay and if you don't even want to watch if you don't want to use any page object model pattern then you can use a properties file that is alternative but I don't see anyone using that approach but there is a way okay so this is the approach we follow in our framework in our project in the coming sessions this is the approach we follow using find by and we know need to use find element find
elements those methods are not needed when I use find My Method clear everyone so please confirm in the chat box this is all about page object classes and if you have a more number of test cases more number of pages then you will see lot of advantage of this because if you have a single test case you will feel some difficulty because to create one test case do we need to really create a two different files not needed right but if you have a more number of test cases more number of pages in your application
this would be very very useful will Reduce lot of time and effort duplication it will avoid we can easily update also suppose this login page is used in multiple test cases and if there is any changes happen you can do change only in one page object class that will automatically reflect in all the test cases so when to use find B the tag name suppose if you want to uh find out group of elements right so here when I say tag name equal to a all the links will be captured because every link is tag
name is a only right so this will capture all the links from the web page and storing into the variable so whenever you want to capture group of e elements so for those elements you will have a same tag name in that case you can use a tag name otherwise leave it I just given one example here yes is there any purpose of how we understood using page Factory and without page Factory so as a I told you initial days of selenium they have not provided a page Factory approach page Factory class so at that
time people were started following this one so this is the approach the first approach we have just captured the locators and we have used find element okay In the later on so they have introduced page Factory class so from then onwards we have started using this page Factory class okay what is an advantage of using this approaches we no need to use find element method because find by itself will do two tasks identifying the element see using the locator it is also identifying the element so that is an advantage you will get here but when
you go to the previous approach previous approach it is just capturing the locator that's it it's not identifying the element for identifying the element we have to again write another step inside the action method but in the in the latest in the latest approach the find by will automatically capture the locate and element also we find out here but in action method we just specify the web element and do the action methods both are almost similar but only thing is in the latest approach page Factory approach we no need to use this dri everywhere and
we no need to write a separate find element or find elements method so find by Method will automatically take care of it so second one is a most preferable approach so because Different companies different people will use different approaches but as per my experience my understanding I always prefer the second approach using find by Even in our next coming project the framework what we are going to develop we use this particular second approach page Factory approach both approaches are fine okay so slightly differences but the way of working is almost it will not improve any
speed or performance or anything both will execute similar fashion but when I use find by anotation we can simply avoid few things so there is no specific purpose just to reduce the our some effort that is the reason they have introduced this page object model pattern all right so that's all for this class uh practice this and uh you can also create multiple page object classes and we can refer multiple page object classes in one single class also we can do if possible but in the framework you will get more clarity because you will automate
more number of test cases we will create more number of page object classes so you will learn lot of things so far whatever we learned we will integrate all These Concepts in one single place so that is our framework so when you when you create a framework work the first and most important task is creating the page object classes so there we will spend a lot of time 70% of the time you will spend while creating the page object classes so once you create your page object classes the rest of the things will become very
very easy so creating the test cases are very easy than creating the page object classes because page object classes will take a lot of time time consuming time consuming effort because we will have so many elements in the so many pages we need to deal with all of them okay so in the from the next class we will start creating the framework using page object model pattern so before that practice this both approaches and get familiar with this and we will continue on the next class okay