How to use Base Class in Hybrid Framework

Answered

Hi,

Could you please explain me the below logic,

1. What is the use of this logic and
2. How to use it?.


package pageObjects;
import org.openqa.selenium.WebDriver;
public class BaseClass {
public static WebDriver driver;
public static boolean bResult;
public BaseClass(WebDriver driver){
BaseClass.driver = driver;
BaseClass.bResult = true;
}

}

URL: http://www.toolsqa.com/selenium-webdriver/selenium-automation-hybrid-framework/

Selenium Train Asked on December 14, 2014 in Selenium WebDriver.

I request you to close all of your questions now.

on December 16, 2014.
Add Comment
1 Answer(s)
Best answer

Mr Selenium,

It is had to explain in few words, as it is an advance concept of Java when we extend classes but I will give it a  try.

The reason to keep WebDriver driver in the Base class, because all of my Page Object Model classes are Extending Base Class. As I am not following the Page Factory pattern given from by the Selenium and I use my own designed Page Object pattern which is based on simple Java concepts. For my page object model I got two options,

First option is:

Home Page Class with Static method of Page Object accepting WebDriver as argument

public class Home_Page {
    private static WebElement element = null;
    public static WebElement lnk_MyAccount(WebDriver driver){
        element = driver.findElement(By.id("account"));
        return element;
}

To use above code I need to write this in my script

 Home_Page.lnk_MyAccount(driver)

Second option is this:

Base Class:

public class BaseClass {
        public static WebDriver driver;
    public  BaseClass(WebDriver driver){
            BaseClass.driver = driver;
        }
}

Home Page Class extending Base Class

 public class Home_Page extends BaseClass{
        private static WebElement element = null;
    public Home_Page(WebDriver driver){
        super(driver);
        }
    public static WebElement lnk_MyAccount(){
        element = driver.findElement(By.xpath(".//*[@id='account']/a"));
        return element;
        }
}
 

To use above code I need to write this in my script

 WebDriver driver = new FirefoxDriver();
new BaseClass(driver);
//Now I do not need to provide driver object to any of my Page Object Class and its method
Home_Page.lnk_MyAccount()

Hope you understand it, please close the question.

Regards,
Lakshay Sharma

xLov3rDns Professor Answered on December 15, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.