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.
arun says
it is failing driver = chrome not running
Mukesh Otwani says
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
Kalaiselvi says
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
Mukesh Otwani says
Hi Kalaiselvi, please refer this YT series https://www.youtube.com/watch?v=vFXL4nMWvXI&list=PL6flErFppaj0WwNOMFeXPVlNCDuJyPYFi
Kalaiselvi says
Ya I have seen.but I need information about base class methods creation.could u pls upload videos for that?
Mukesh Otwani says
Hi Kalaselvi, in that series I have also shown how to create base classes and other utilities as well.
Mayank Singla says
How I can run multiple test Classes without closing the browser/website?
Mukesh Otwani says
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
ali says
perfect, thx.
raja says
hi Mukesh,
Combine of Keyword and page factory framework is possible to automate?
Mukesh Otwani says
Hi Raja,
Yes, it is possible. Framework building is just an approach using which you are executing your test scenario
tejas says
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?
Mukesh Otwani says
Hi Tejas,
Since you are getting NullPointerException, please debug your code because somewhere in your script flow, you haven’t initialized any object properly.
Vishnu says
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…
Mukesh Otwani says
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.
Leena says
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
Mukesh Otwani says
Hi Leena,
Debug your script then you will come know about any missing parameter or value.
Leena says
Thanks. Done with constructor super key in setup method created test case class.
Mukesh Otwani says
Great…:)
pit says
awsome. Is part 2 available ?
Mukesh Otwani says
Hi Arpit will upload soon.
Pooja says
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.
Mukesh Otwani says
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.
James M. says
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?
Mukesh Otwani says
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
Gaurav Khurana says
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
Mukesh Otwani says
Yes Gaurav this is we also use in our framework.
K praneeth kumar says
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??
Mukesh Otwani says
Hi Praneeth,
Just inherit that class.
Steve says
Nice example… thankyou
Mukesh Otwani says
Welcome Steve.
Sowmya says
Mukesh…Your explanation is awesome. Your blog is helping me a lot in learning.
Mukesh Otwani says
Thanks Sowmya I am glad you liked it. Keep visiting.
Jithin says
Hi mukesh, thanks for this tutorial
I have a question. Is it possible to add utility functions along with BaseClass? ..
Mukesh Otwani says
Yes you can but based on functionality you can create packages as well.
Cool says
Hi Mukesh,
Shouldn’t this be @BeforeMethod and @AfterMethod annotations that you use in base class?
Mukesh Otwani says
In video I covered the same.
pavani says
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>
Mukesh Otwani says
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/