Session 47 – Selenium with Java | TestNG | Page Object … — Transcript

Learn the Page Object Model design pattern in Selenium with Java and TestNG to improve automation framework design and maintainability.

Key Takeaways

  • Page Object Model is a design pattern, not a framework.
  • POM improves test automation by separating locators and test logic into different classes.
  • Using POM reduces code duplication and enhances maintainability.
  • POM facilitates handling multiple web pages in complex test scenarios.
  • TestNG integrates well with POM for structured test execution.

Summary

  • Introduction to the Page Object Model (POM) as a design pattern, not a framework.
  • Explanation of how test cases are typically written without POM, combining locators, test methods, validations, and actions in one class.
  • Description of the problems faced when not using POM, such as poor maintainability and code duplication.
  • How POM separates locators/web elements and test methods into different classes to improve organization.
  • Benefits of using POM include easier maintenance, reduced effort in updating tests, and better framework design.
  • Examples of test cases like login, search, and advanced search to illustrate interaction with multiple web pages.
  • Clarification that POM helps manage multiple page interactions by maintaining separate classes for each page's elements.
  • Discussion on how validations and actions are handled separately in test classes when using POM.
  • Practical insights on how POM reduces complexity and enhances scalability of automation tests.
  • Overview of TestNG usage with POM for structuring and executing tests efficiently.

Full Transcript — Download SRT & Markdown

00:03
Speaker A
All right, so let's start today's session. Today, we're going to see a very important topic which is not specific to TestNG features, but is very important for designing frameworks: the Page Object Model. Page Object Model.
00:28
Speaker A
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
00:52
Speaker A
Okay, so first of all, let us try to understand what this Page Object Model is and why it is so important to design automation frameworks. So first of all, let us try to understand. We have so far created multiple test cases using TestNG style, like we have created multiple test methods and we have executed them through XML files and so on.
01:11
Speaker A
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
01:28
Speaker A
So that is a normal approach, like without using Page Object Model. By using Page Object Model, we can write test cases in two different ways. So if I don't use the Page Object Model pattern, what are the 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 at the same time, if I use Page Object Model in our automation, what are the additional benefits we'll get? So that we'll understand.
01:49
Speaker A
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
02:09
Speaker A
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 locators or the web elements in separate classes. So normally, when you create a test case, what does the test case contain? Test case contains multiple things, like we will have all the locators, we will place them 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 validation methods and action methods. Everything we'll include in one test, right? So we'll identify the elements and we will also interact with the elements, which are called actions. And after interacting with 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?
02:27
Speaker A
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
02:41
Speaker A
So this is without Page Object Model pattern. So Page Object Model is not a framework. So most people will think it is a framework. No, it is not a framework. It is a design pattern. This is a design pattern 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 a separate class and test methods having a separate class. Inside the test methods, we will have all the validations, only validations, okay? And the locators will be put in a separate class. So 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 separate classes. We divide them into separate classes. That is the Page Object Model pattern.
03:00
Speaker A
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
03:18
Speaker A
So why do we need to separate? What is the additional benefit you will get if you separate? How will it reduce our effort? We'll see that practically within examples. So let me show you a small picture here.
03:36
Speaker A
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
04:01
Speaker A
Yeah, all right, so let us 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? If you write 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.
04:25
Speaker A
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
04:38
Speaker A
Okay, so if you look at this, these are all my different tests which I have written in TestNG. Let's say test one, test two, and test three, and so on. So you can have multiple tests.
04:54
Speaker A
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
05:14
Speaker A
Okay, so let us assume my test one is, in this, I want to perform the login test. In test one, I want to perform the login test. In test two, I want to perform the searching test. Search. Test three, I want to perform the advanced searching. So different tests I want to do in different test cases.
05:31
Speaker A
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
05:48
Speaker A
Now, when I do these tests, 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 elements from the login page. So while creating the login test, we will refer to 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.
06:02
Speaker A
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
06:19
Speaker A
Now, when you're writing test case two, and here I want to perform the searching test, and in this again, I want to start from the login page. So first, I need to log into the application, and once I successfully log in, I will get a search box, and here I can do the searching validation.
06:40
Speaker A
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
07:00
Speaker A
So if you want to automate the second test case, how many pages are required? How many web pages do 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, the login page. But if we want to perform the search test, we need two pages: login page and search page.
07:20
Speaker A
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
07:39
Speaker A
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.
08:01
Speaker A
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
08:23
Speaker A
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.
08:44
Speaker A
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
09:00
Speaker A
So how many pages are required to automate test three? Three pages are required: login page, search page, advanced search page.
09:22
Speaker A
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
09:42
Speaker A
Now, if I look at this particular scenario, to automate login test, we are identifying the web elements from the login page. When you automate test 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.
09:59
Speaker A
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
10:32
Speaker A
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 test cases. So in the login test one, I have to locate login page web elements. Again, in 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 in test three also, again one more time we have to write locators for login page also, search page.
10:57
Speaker A
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
11:16
Speaker A
So this is a problem. So this is the first problem: duplication of elements or locators. If you have just one or two test cases, you don't see much difference, 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 write 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.
11:35
Speaker A
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
11:54
Speaker A
Now the second problem, okay, suppose I have located these login page elements in three different tests. So I have located these elements or you have written the locators for login page in three different tests. Tomorrow, something has got changed in the login page itself. Suppose some attribute has got changed or some XPath has got changed for that particular element. Then what should I do? I should go and modify that in every test case.
12:16
Speaker A
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
12:36
Speaker A
Okay, suppose there is an input box for login and the attribute or some XPath has got changed for this input box. Because locators are written in every test, we need to go and modify that particular element specifically.
12:56
Speaker A
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
13:18
Speaker A
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
13:36
Speaker A
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
13:52
Speaker A
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
14:08
Speaker A
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
14:25
Speaker A
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
14:46
Speaker A
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
15:06
Speaker A
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
15:26
Speaker A
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
15:47
Speaker A
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
16:07
Speaker A
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
16:23
Speaker A
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
16:39
Speaker A
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
17:00
Speaker A
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
17:16
Speaker A
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
17:35
Speaker A
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
17:54
Speaker A
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
18:16
Speaker A
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
18:36
Speaker A
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
18:54
Speaker A
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
19:14
Speaker A
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
19:34
Speaker A
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
19:53
Speaker A
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
20:10
Speaker A
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
20:31
Speaker A
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
20:48
Speaker A
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
21:05
Speaker A
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
21:27
Speaker A
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
21:44
Speaker A
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
22:02
Speaker A
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
22:19
Speaker A
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
22:38
Speaker A
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
23:04
Speaker A
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
23:28
Speaker A
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
23:46
Speaker A
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
24:06
Speaker A
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
24:17
Speaker A
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
24:37
Speaker A
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
24:53
Speaker A
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
25:10
Speaker A
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
25:32
Speaker A
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
25:56
Speaker A
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
26:09
Speaker A
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
26:28
Speaker A
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
26:44
Speaker A
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
27:02
Speaker A
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
27:18
Speaker A
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
27:36
Speaker A
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
27:54
Speaker A
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
28:13
Speaker A
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
28:29
Speaker A
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
28:50
Speaker A
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
29:07
Speaker A
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
29:24
Speaker A
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
29:44
Speaker A
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
30:00
Speaker A
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
30:21
Speaker A
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
30:44
Speaker A
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
31:07
Speaker A
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
31:24
Speaker A
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
31:42
Speaker A
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
32:08
Speaker A
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
32:30
Speaker A
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
32:51
Speaker A
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
33:14
Speaker A
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
33:36
Speaker A
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
33:57
Speaker A
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
34:23
Speaker A
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
34:41
Speaker A
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
35:04
Speaker A
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.
35:20
Speaker A
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
35:42
Speaker A
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
36:05
Speaker A
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
36:26
Speaker A
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
36:50
Speaker A
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
37:16
Speaker A
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
37:39
Speaker A
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
37:52
Speaker A
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
38:23
Speaker A
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
38:34
Speaker A
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
39:01
Speaker A
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
39:11
Speaker A
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
39:30
Speaker A
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
39:44
Speaker A
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
40:00
Speaker A
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
40:23
Speaker A
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
40:38
Speaker A
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
40:57
Speaker A
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
41:11
Speaker A
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
41:30
Speaker A
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
41:51
Speaker A
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
42:11
Speaker A
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
42:31
Speaker A
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
42:58
Speaker A
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
43:16
Speaker A
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
43:37
Speaker A
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
43:56
Speaker A
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
44:08
Speaker A
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
44:31
Speaker A
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
44:52
Speaker A
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
45:07
Speaker A
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
45:25
Speaker A
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
45:40
Speaker A
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
45:55
Speaker A
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
46:08
Speaker A
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
46:23
Speaker A
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
46:39
Speaker A
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
46:58
Speaker A
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
47:11
Speaker A
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
47:31
Speaker A
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
47:44
Speaker A
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
48:06
Speaker A
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
48:22
Speaker A
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
48:44
Speaker A
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
49:05
Speaker A
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
49:27
Speaker A
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
49:53
Speaker A
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
50:19
Speaker A
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
50:45
Speaker A
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
51:12
Speaker A
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
51:35
Speaker A
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
51:53
Speaker A
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
52:18
Speaker A
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
52:33
Speaker A
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
52:55
Speaker A
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
53:18
Speaker A
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
53:34
Speaker A
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
53:53
Speaker A
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
54:09
Speaker A
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
54:41
Speaker A
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
54:56
Speaker A
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
55:08
Speaker A
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
55:29
Speaker A
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
55:45
Speaker A
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
56:12
Speaker A
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
56:32
Speaker A
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
56:54
Speaker A
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
57:15
Speaker A
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
57:34
Speaker A
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
57:54
Speaker A
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
58:20
Speaker A
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
58:43
Speaker A
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
59:02
Speaker A
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
59:27
Speaker A
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
59:50
Speaker A
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
60:10
Speaker A
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
60:28
Speaker A
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
60:44
Speaker A
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
61:01
Speaker A
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
61:21
Speaker A
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
61:42
Speaker A
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
62:07
Speaker A
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
62:23
Speaker A
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
62:40
Speaker A
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
63:00
Speaker A
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
63:26
Speaker A
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
63:46
Speaker A
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
64:12
Speaker A
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
64:29
Speaker A
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
64:49
Speaker A
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
65:14
Speaker A
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
65:31
Speaker A
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
65:56
Speaker A
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
66:23
Speaker A
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
66:47
Speaker A
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
67:14
Speaker A
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
67:30
Speaker A
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
67:41
Speaker A
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
67:59
Speaker A
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
68:14
Speaker A
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
68:34
Speaker A
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
69:05
Speaker A
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
69:25
Speaker A
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
69:46
Speaker A
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
70:01
Speaker A
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
70:25
Speaker A
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
70:45
Speaker A
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
71:06
Speaker A
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
71:25
Speaker A
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
71:45
Speaker A
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
72:04
Speaker A
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
72:26
Speaker A
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
72:41
Speaker A
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
73:04
Speaker A
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
73:26
Speaker A
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
73:48
Speaker A
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
74:09
Speaker A
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
74:31
Speaker A
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
74:50
Speaker A
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
75:10
Speaker A
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
75:26
Speaker A
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
75:48
Speaker A
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
76:22
Speaker A
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
76:41
Speaker A
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
77:00
Speaker A
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
77:18
Speaker A
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
77:40
Speaker A
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
77:55
Speaker A
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
78:25
Speaker A
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
78:44
Speaker A
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
78:59
Speaker A
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
79:24
Speaker A
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
79:41
Speaker A
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
80:01
Speaker A
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
80:24
Speaker A
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
80:37
Speaker A
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
81:06
Speaker A
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
81:27
Speaker A
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
81:43
Speaker A
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
82:03
Speaker A
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
82:29
Speaker A
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
82:53
Speaker A
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
83:11
Speaker A
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
83:31
Speaker A
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
83:50
Speaker A
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
84:10
Speaker A
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
84:25
Speaker A
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
84:44
Speaker A
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
84:55
Speaker A
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
85:13
Speaker A
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
85:34
Speaker A
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
85:52
Speaker A
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
86:10
Speaker A
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
86:28
Speaker A
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
86:47
Speaker A
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
87:07
Speaker A
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
87:29
Speaker A
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
87:58
Speaker A
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
88:13
Speaker A
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
88:32
Speaker 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
88:48
Speaker A
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
89:07
Speaker A
practice this both approaches and get familiar with this and we will continue on the next class okay
Topics:SeleniumJavaTestNGPage Object ModelAutomation FrameworkDesign PatternTest AutomationWeb ElementsSDETQA

Frequently Asked Questions

What is the Page Object Model in Selenium automation?

Page Object Model is a design pattern used in Selenium automation that separates web element locators and test methods into different classes to improve maintainability and reduce code duplication.

Is the Page Object Model a framework?

No, the Page Object Model is not a framework; it is a design pattern that guides how to organize locators and test methods in automation projects.

What are the benefits of using the Page Object Model?

Using the Page Object Model makes test automation code easier to maintain, reduces duplication, improves readability, and helps manage interactions with multiple web pages effectively.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →