How to implement better OOPs How to implement better OOPs selenium selenium

How to implement better OOPs


BasePage needs to know the implementing type for chaining:

/** * @param <T> The implementing type. */public abstract class BasePage<T extends BasePage<T>> {}

Then, you could use:

public T clickGoBack() { return (T) this; }

The problem here, is that this isn't necessarily an instance of T so the cast is not safe, for example with the following class:

public class Page1 extends BasePage<Page2> {}

A solution is to ask the subclasses their this:

public abstract class BasePage<T extends BasePage<T>> {  protected final WebDriver driver;  public BasePage(WebDriver driver) {    this.driver = driver;  }  public T clickGoBack() throws Exception{    driver.click(goBackButton);    return getThis();  }  protected abstract T getThis();}public class HomePage extends BasePage<HomePage> {  public HomePage(WebDriver driver) {    super(driver);  }  @Override  protected HomePage getThis() {    return this;  }}