![](https://i0.wp.com/learn-automation.com/wp-content/uploads/2016/09/Base-class-in-Selenium-Webdriver.png?ssl=1)
All of us know the importance of Automation framework which can reduce the huge amount to rework.This article will walk you through How to Create Base Class in Selenium for Better script and trust me you will love this feature.
Long back we have covered Data Driven framework and Page Object model as well, so you can combine all the concepts now with Base class and you can create a healthy framework as well.
What is Base Class in Selenium
- Base class in the main class which will take care of Browser setup, loading configuration file and other reusable methods like screenshot, handling sync issues and many more.
- Using Base class we can avoid code duplication.
- Reuse code as much we can.
Preconditions before jumping into advance concepts.
- Get ready with basic tutorials.
- TestNG should be installed and little knowledge on TestNG Framework.
- Understanding of Inheritance in JAVA.
How Base class works in Selenium
1-When we create base class and if TestCases extends BaseClass then we can use all the methods of Baseclass.
2- Before calling actual @Test Base class methods will get executed and Depends on annotations it will call the respective methods.
3- We can extend this class in all test cases and we can call custom methods as well directly.
YouTube Video for Base Class in Selenium
Base Class in Selenium
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Reporter; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; public class BaseClass { WebDriver driver; @BeforeClass public void setupApplication() { Reporter.log("=====Browser Session Started=====", true); driver=new ChromeDriver(); driver.manage().window().maximize(); driver.get("http://enterprise.demo.orangehrmlive.com/symfony/web/index.php/auth/login"); Reporter.log("=====Application Started=====", true); } @AfterClass public void closeApplication() { driver.quit(); Reporter.log("=====Browser Session End=====", true); } }
Actual Test script for Selenium Framework
package pages; import org.openqa.selenium.By; import org.testng.annotations.Test; public class InvalidLogin extends BaseClass { @Test(description="This TC will perform valid login") public void loginToApplication() throws Throwable { driver.findElement(By.name("txtUsername")).sendKeys("Admin1"); driver.findElement(By.id("txtPassword")).sendKeys("admin1"); driver.findElement(By.id("btnLogin")).click(); driver.navigate().back(); Thread.sleep(5000); } @Test(description="This TC will perform invalid login") public void loginToApplication1() { driver.findElement(By.name("txtUsername")).sendKeys("admin1"); driver.findElement(By.id("txtPassword")).sendKeys("admin2"); driver.findElement(By.id("btnLogin")).click(); } }
I hope you will implement the same concept in your automation framework. If you want to add anything else in this post than let me know in the comment section.
Thank you so much, Kindly share with your friends. See you in the next post.
it is failing driver = chrome not running
Hi Arun,
You may need to debug your code line by line in order to find the actual issue. You can refer this link https://www.youtube.com/watch?v=rXXeHfvbddo
Hi sir
Now I am learning selenium with Java
In junit we r creating base class to reduce duplication and increase code reusability.
login base class methods only I learned from trainer..how I create other methods base classes.? Could u pls tel me.very difficult to find
Hi Kalaiselvi, please refer this YT series https://www.youtube.com/watch?v=vFXL4nMWvXI&list=PL6flErFppaj0WwNOMFeXPVlNCDuJyPYFi
Ya I have seen.but I need information about base class methods creation.could u pls upload videos for that?
Hi Kalaselvi, in that series I have also shown how to create base classes and other utilities as well.
How I can run multiple test Classes without closing the browser/website?
Hi Mayank,
Set your test cases in a proper sequential way so that application screen where your first test ends should be start screen for next test
perfect, thx.
hi Mukesh,
Combine of Keyword and page factory framework is possible to automate?
Hi Raja,
Yes, it is possible. Framework building is just an approach using which you are executing your test scenario
hi i am tejas,i am trying to automate 7 steps of web based application, and executing 7 classes with the help of testng.xml, but my first class is executed only,and the remaining classes is giving null pointer exception.In the first step i fill a form when i click on next button,new page is loaded in the website then my script is not working.How can i handle this?
Hi Tejas,
Since you are getting NullPointerException, please debug your code because somewhere in your script flow, you haven’t initialized any object properly.
Hi Mukesh,
Very useful article. I have learned a lot from your tutorials. Could you please tell me how can I use Explicit wait from a base class just like browser class. I tried to create a base class with explicit waits and call the same from the test class with driver object. but it is showing null pointer exception. It would be great if you could guide me through this…
Hi Vishnu,
Did you debug your code as NullPointerException is generic runtime exception. You need to check which object is getting nullify in your code.
Hi Mukesh,
I m trying to use this base class in hybrid framework. Made browser action as a base class in which I have added setup and browser close methods and calling in test cases classes but it is giving me null pointer exception. Could you please guide me how I can use this concept in hybrid framework? Thanks for your all vedios great job
Hi Leena,
Debug your script then you will come know about any missing parameter or value.
Thanks. Done with constructor super key in setup method created test case class.
Great…:)
awsome. Is part 2 available ?
Hi Arpit will upload soon.
Hi Mukesh,
In this video you have mentioned about part 2 of this video. I could not find that.
Could you please share the video.
Hi Pooja,
Yes, I mentioned about part 2 in the video but it is not yet finalized. Whenever I publish this video(part 2) then you will get an email for that.
Hi Mukesh, your tutorial is awesome.
But how can I implement something in the BaseClass that could make me choose whether Firefox, Chrome, IE or Safari browser I am using. How to implement it in the BaseClass?
Hi James,
There are multiple ways for doing this and one of them is passing parameters(in your case it is ‘browser’ name) to BaseClass using @Parameters annotation from testng.xml file
Thanks it worked well. It would be great if we can know how to pass few parameters to base class.. I mean suppose if we want to pass the URL to the base class.
Also its interesting to see that without creating any object, Base class functions are being called automatically when we run our script
Yes Gaurav this is we also use in our framework.
Hey Mukesh……nice work…
can you tell me how to use the webdriver instance created in baseclass, in multiple tests in different classes, without declaring it as static??
Hi Praneeth,
Just inherit that class.
Nice example… thankyou
Welcome Steve.
Mukesh…Your explanation is awesome. Your blog is helping me a lot in learning.
Thanks Sowmya I am glad you liked it. Keep visiting.
Hi mukesh, thanks for this tutorial
I have a question. Is it possible to add utility functions along with BaseClass? ..
Yes you can but based on functionality you can create packages as well.
Hi Mukesh,
Shouldn’t this be @BeforeMethod and @AfterMethod annotations that you use in base class?
In video I covered the same.
Hi Mukesh,
Your lecture is awesome. I have small doubt. You gave chrome driver and u didnt give any path of chrome.
Is is it possible>
Yes Pavani because I am using MAC system and we can do that. Kindly follow below article for Chrome and Firefox http://learn-automation.com/firefox-browser-on-mac-using-selenium-webdriver/