Saturday 13 July 2024

How to use Java Overiding in Test Automation ?






💛 How to use Java Overiding in Test Automation ?



When I talk to folks in 1:1, I've noticed that many get confused about how to add new steps to the @BeforeTest method when it's set up in a base class.

Using @Override is a good way to handle this and it's a good example to mention in interviews. Just stick to relevant examples when discussing this topic during interviews and avoid giving random examples.



🔺 1️⃣ Overriding in Java:


Overriding is a concept in Java that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. It enables polymorphism, where the same method name can behave differently based on the object it's called on.


🔺 Steps to implement Overriding





👉🏻 Create a Base Test Class:
Define a base test class where you initialize your WebDriver instance and set up common configurations.


public class BaseTest {
protected WebDriver driver;

@BeforeTest
public void setUp() {
// Initialize WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
}

// Other common setup methods can be added here
}



👉🏻 Override Setup Method:
Override the setUp() method in subclasses to provide specific setup actions for each test class

public class MyTest extends BaseTest {

@Override
@BeforeTest
public void setUp() {
// Call the setup method from the base class
super.setUp();

// Additional setup specific to this test class
// For example, navigating to a specific URL
driver.get("https://example.com");
}
}


In this example, the BaseTest class contains the setUp() method annotated with @BeforeTest, which initializes the WebDriver instance.

The MyTest class extends BaseTest, and the setUp() method is overridden to provide additional setup specific to the test class.

The overridden method is also annotated with @BeforeTest to ensure it executes before any test method belonging to the <test> tag in the TestNG XML suite file.



**
🔥 For a more hands-on learning experience in automation with realistic examples, consider exploring a one-on-one guided session on end-to-end automation and SDET(API+UI+Mobile+DevOps+GenerativeAI) : https://lnkd.in/giCxnJJ7

**

Introducing a curated package of Real-Time Interview Qus and Ans by an experienced Amazon SDET, covering topics like Selenium, API, Java, and more, gleaned from successful interview experiences at top companies: https://lnkd.in/gJrBGExe
**



hashtagsidpostjava hashtagsidpost hashtagjava hashtagqualityassurance hashtagtesting hashtagqa hashtagtestautomation hashtagautomation hashtagsoftwaretesting hashtagqaautomation hashtagsoftware hashtagcareer

All Time Popular Posts

Most Featured Post

Good & bad Prompt to write Selenium Tests using Generative AI with GPT

  📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUGmb-P ****** 💛 Good & bad Prompt to writ...