Page Object Model (Simplified) with an abstract class

Karthik KK
A free video tutorial from Karthik KK
Software Architect, Consultant,Youtuber and Best Seller
4.3 instructor rating • 27 courses • 232,345 students

Lecture description

In this lecture we will start working with POM, but instead of initializing the page object in all the page classes, we will initialize in one class and inherit that class in all the page classes.

Learn more from the full course

Automation framework development with Selenium C# (Advanced)

An End-to-End Selenium automation framework development course

15:52:08 of on-demand video • Updated January 2021

  • Understand what an automation framework is
  • Choosing right automation framework for your project
  • Building more robust and hybrid automation framework
  • Leveraging the power of C# to build generic framework model
  • Extending the automation framework with Behavioral Driven Development with Specflow
  • Developing an Industry standard framework with complete modularity on framework as well as Application test code
English [Auto] Page object model of simplicity, a good way to stay flexible is right, less code, so the page initialization with base class. Instead of initializing the page in each classes, let's try to create a base abstract class. So this makes code much simpler and easier to understand. At the same time, we introduce some standards to be followed in our code while starting to code with page object model. So if this is really not going to make sense, let's see this in action. So in the last video of discourse, what we are doing is we created a page object model and also we moved all the objects of the page in this class, the log in page class, and then we try to initialize the page or here. And then we go to the test and the disregard past and know what I'm going to do is I'm going to write one more page. Let's say I write a page for the user list. So once I log into the application as admin, I then see that I will have a employee list. Once I click this employee list, I should see the search text box, something like this. So if I want to verify, if I end up with this employee list page, then I need to create a new page in here because the employee list is again a different page then compared to the login page tasks. So what I'm going to do, I'm going to add one more page in here. And let's call this as employee page, and then I'm going to add some of the objects in here so far, adding the objects, what I'm going to do is I need to identify their controls property. So let's see, what is the control property for this text box? I think the text box has got a name and the name is search term. So what I'm going to do, I'm going to copy this line of code to save some time. I'm going to it right here. And let's add the references for the find by and then let's add the reference for the environment. So everything is fine now and now. Let's add the name as search term. All right. And now you can see that within this employee page, again, I have to call the initialize the page initialize method. So, again, I have to call a constructor and then I have to again call the page factory dot in it elements. And again, I have to parse the driver, which is nothing but the I the driver of the driver. And then here I have to pass the driver and I have to pass this as the page. So this is a kind of war, writing the same code again and again. So it is always better to reduce the number of lines of code and make some standard to be followed in our code. And that's why, if you remember, in our framework design, we created a class file called Base Based Arts. Yes. So why is this empty now? Let's make use of it. What I'm going to do is I'm going to make this class the base page class to be a abstract class this time. And then I'm going to call or page initialize in here, and then I'm going to make this guy, the base page guy, to be a base class for all or pages, which are basically right in future. So what I'm going to do is I need to call the page initialize in this particular class file. So far doing that, we first of all need to add the selenium package. So, again, I need to install the selenium support package for my R2 framework. So I'm going to hit enter. All right, and now if I go to the reference, I can see that I will get both the driver and the driver support accurate in my reference. And now if I create a constructor for the base page and I can call the page factory, which control dot, dot, initialize element and then initialize element, I need to pass the driver. So again, I'm going to pass the driver as the driver and the driver is again missing. So I'm going to add the reference and then within this. In its element, I will pass the driver and this as the page, and then I'm going to say I know how to make use of this base page class. All you have to do is to call this base page to our login page. Daudzai is pretty simple, right? And now you're initialise page method is no longer be usable. I mean, you don't even have to write this code anymore. All you have to do is to just pass the eye, the driver to the base class itself. But we need to somehow call the base class here by calling like this base page. But how to do that? But currently you can see that we'll be getting an exception here. And the reason is we did not add the reference for our aero framework yet in our project. So let's quickly add that. So I'm going to add a reference and the reference is going to be solution this time. So I'm going to select the outer framework. I'm going to hit, OK? And now if I press control DOT, you can see that I'll be getting it using a framework dot based namespace. And now if I add this, I'll be getting a different error altogether. I'll be getting there is no argument which gives the corresponding required format of driver to the base page, meaning I don't have to do these things anymore. All I have to do is just pass in base of driver and the driver should be actually coming from. I have a driver of driver and this is how you can actually write the code. So as you can see, my initialize page does not have any page initialize here. But still I am parsing the driver and that is the reason that I need to write this constructor. And the same thing happens for this employee page as well. I don't have to do this page fact without any elements anymore because it is being taken care of by my base page of my framework. But I need to pass base of driver. The reason is I need to pass the driver in here. Right. And the reason which I created this employee page once again is to show you that every time you're going to create a new page, all you have to do is to just call this base page based class and then your page initialization will take place there automatically. You don't have to explicitly call this page initialize for each and every class is right. This is going to reduce some lines of code for you. But still, you see that there is a problem. Why do I write this constructor every time in all these pages? Right. So in the next video, we'll try to overcome this problem by using this driver start class. So far, we have not given any life to this driver as class, but we will come to the class and will give life to the class and will reduce this lines of code as well.