Ways of passing Driver to other class

There are different way with which you can pass the driver in other class. Below are those 3 ways.

1. By passing driver as parameter in class object
2.By returning the driver
3.By inheritance using super for driver


1st method is- 


By passing driver as parameter in class object

In this method, we will pass driver as parameter in other class.


public class PageFactory_OtherClass {
WebDriver driver; @BeforeMethod public void beformethods() //In this call driver is initiated. { System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver_78.exe"); driver= new ChromeDriver(); driver.get("https://google.com"); } @Test public void launchBrowser() { SubClass sb= new SubClass(driver);// here we are passing driver as parameter in other class sb.enterSearchText(); } } public class SubClass  { WebDriver driver=null; @FindBy (xpath="//input[@title='Search']") WebElement search; public SubClass(WebDriver driver) { this.driver=driver; PageFactory.initElements(driver, this); } public void enterSearchText() { search.sendKeys("new search"); } }

2nd method is- 

By returning the driver

In this method, we will return driver and use in other classes.



public class SubClass1 { static WebDriver driver; public WebDriver driver() // This method return the driver { System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver_78.exe"); driver= new ChromeDriver(); this.driver=driver; return driver; } } public class SubClass   { static WebDriver driver; @FindBy (xpath="//input[@title='Search']") WebElement search; public SubClass() { SubClass1 subc=new SubClass1(); driver=subc.driver(); //here you use the above driver this.driver=driver; PageFactory.initElements(driver, this); } public void enterSearchText() { driver.get("https://google.com"); search.sendKeys("new search"); driver.manage().window().maximize(); } } public class PageFactory_OtherClass { WebDriver driver; SubClass sb; @BeforeMethod public void beformethods() { sb= new SubClass(); } @Test public void launchBrowser() { sb.enterSearchText(); } }


3rd method is- 


By inheritance using super for driver

This method, we will inherit the driver from supper class.



public class SubClass1 {      // this is super class where driver is initiated.
 static WebDriver driver;
 public SubClass1(WebDriver driver)
 {
  System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver_78.exe");
  driver= new ChromeDriver();
  this.driver=driver; 
 }
}

public class SubClass extends SubClass1  {     //super class inherited here

 @FindBy (xpath="//input[@title='Search']")
 WebElement search;
 public SubClass()
 {
  super(driver);                             //here we call the driver of super class
  PageFactory.initElements(driver, this);
 }
 public void enterSearchText()
 {
  driver.get("https://google.com");
  search.sendKeys("new search");
  driver.manage().window().maximize();
 }
}

public class PageFactory_OtherClass {
  
 WebDriver driver;
 SubClass sb;
  
 @BeforeMethod
 public void beformethods()
 {
  sb= new SubClass();
 }
 @Test
 public void launchBrowser()
 {
 
  sb.enterSearchText();
 }

No comments:

Post a Comment

Software Testing Automation Guide

  get methods in Selenium Webdriver Below are the list of get methods that can be use with driver. Get()- This com...