3

I'm designing an application using Selenium and their PageFactory.

Each of my page object classes inherits from a base class with the standard methods in, but I also have a few pages which require SlowLoadableComponent. As this is implemented as an abstract class I can't extend both that and my base page class.

Is there any downside to making my base page class extend SlowLoadableComponent? If so would you implement the base page class as an interface with default methods or something else?

Ian
  • 1,507
  • 3
  • 21
  • 36

1 Answers1

1

If only you had multiple inheritance...

The workaround to multiple inheritance in Java is delegation.

So you could refactor your code to look like this:

public interface IBasePage {
  //... unimplemented base page methods
}

public class BasePageImpl implements IBasePage {
  public BasePageImpl(WebDriver webDriver) { //... }
  //... implemented base page methods
}

public class FastPage implements IBasePage {
  //... delegate all base page methods to the BasePageImpl
}

public class SlowPage extends SlowLoadableComponent implements IBasePage {
  //... delegate all base page methods to the BasePageImpl
}
michaelsnowden
  • 6,031
  • 2
  • 38
  • 83
  • 1
    But, in pageobject pattern implementing Interfaces is tedious because you have to do that for almost every page. Isn't it? – Saifur Aug 11 '15 at 14:07
  • 1
    @Saifur Only in FastPage and SlowPage. Then you can inherit from those two classes. Also, it's trivial to delegate those methods. IntelliJ can do it for you: https://www.jetbrains.com/idea/help/generating-delegation-methods.html – michaelsnowden Aug 11 '15 at 16:04
  • Thanks for that clarification Michael: I too had not picked up that you are intending that FastPage and SlowPage be inherited from. I have a slight fear of combinatorial explosion with this solution in general, but for this limited case I'm liking it more the more I think about it. – Ian Aug 11 '15 at 19:32
  • @IanWorthington You basically never want to declare a variable as `FastPage` or `SlowPage`. You should always declare them as `IBasePage`. You should use `FastPage` and `SlowPage` when you construct the page. – michaelsnowden Aug 11 '15 at 21:43