How to deal with reusable components in Selenium Java?


We can deal with reusable components in Selenium Java with the help of inheritance concept. It is a parent child relationship where the child class inherits the properties and methods of the parent class.

Example

For Parent class.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Baseclass {
   public void login() throws IOException {
      Properties prop = new Properties();
      //Reading values from property file
      FileInputStream ips = new FileInputStream(
      "C:\Users\ghs6kor\eclipse- workspace\Inheritance\config.properties");
      prop.load(ips);
      System.setProperty("webdriver.gecko.driver",             "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe");
      WebDriver driver = new FirefoxDriver();
      driver.get(prop.getProperty("url"));
   }
}

Example

For Child class.

import java.io.IOException;
public class Child extends Baseclass {
   public static void main(String[] args) throws IOException {
      // TODO Auto-generated method stub
      Child c = new Child();
      c.login();
      c.testinheritance();
   }
   public void testinheritance() {
      // parent class method used in child class
      login();
      System.out.println("Test Inheritance");
   }
}

Updated on: 11-Jun-2020

789 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements