title: Designing page object structure for selenium tests. Inheriting from WebElement causing problems
body: <p>I'm trying to design some page objects and have some issues with the overall design.</p>
  
  <p>I know a common problem in tests is figuring out whether an element exists, and the solution is to use a try/catch or to use findElements(...) and assert if the size of the resulting list is 0.</p>
  
  <p>I'd like to avoid these try/catches in all my tests. Is it possible to use inheritance to have the try/catch in a super class, making the tests cleaner? I'd like the method to return null if the element doesn't exist, then I can just assertNotNull(...) in the test.</p>
  
  <p>Here's a method from a page object in my current solution:</p>
  
  <pre><code>public WebElement getRegisterLink(){
      List&lt;WebElement&gt; list = driver.findElements(By.linktext("Register"));
      if(list.size() == 0){
          return null;
      }
          return list.get(0);
  }
  </code></pre>
  
  <p>The problem with this is that there is a lot of similar repeated code. My getLoginButton(), getSomePageLink(), etc. look the same.</p>
  
  <p>Here is one idea I've had...</p>
  
  <pre><code>public abstract class MyCompanyElement implements WebElement{
  
  WebElement element;
  
  public MyCompanyElement(WebElement element){
      this.element = element;
  }
  
  @Override
  public void clear() {
      element.clear();
  }
  
  @Override
  public void click() {
      element.click();
  }
  
  @Override
  public MyCompanyElement findElement(By arg0) {      
      try{
          MyCompanyElement e = (MyCompanyElement) element.findElement(arg0);
          return e;
      } catch (NoSuchElementException ex){
          return null;
      }
  
  }
  
  @Override
  public List&lt;MyCompanyElement&gt; findElements(By arg0) {       
      List&lt;MyCompanyElement&gt; list = new ArrayList&lt;MyCompanyElement&gt;();
      for(WebElement e : element.findElements(arg0)){
          list.add((MyCompanyElement) e);
      }
      return list;
  }
  
  //Rest of the methods here which just call the methods of element
  }
  </code></pre>
  
  <p>I then have a BasePage class which all page objects will extend. </p>
  
  <pre><code>public abstract class BasePage {
  
  MyCompanyElement element;
  
  public BasePage(WebDriver driver){
      element = (MyCompanyElement) driver.findElement(By.tagName("html"));        
  }
  
  //More methods here
  
  }
  </code></pre>
  
  <p>The page objects then have methods like getLoginButton(), getRegisterLink(), etc. which return MyCompanyElement instead of WebElement. (Basepage contains elements that are present on all web pages such as the header and footer).</p>
  
  <p>My test might then look like:</p>
  
  <pre><code>Homepage homepage = new Homepage(driver);
  homepage.getLoginButton().click();
  </code></pre>
  
  <p>and if I want to check if an element exists, then...</p>
  
  <pre><code>assertNotNull(homepage.getRegisterLink());
  </code></pre>
  
  <p>The problems with the above solution are:</p>
  
  <ul>
  <li>In MyCompanyElement, it's okay for findElement() to return a MyCompanyElement. But the compiler doesn't like me returning a List of MyCompanyElement in findElements(). Is there a workaround for this?</li>
  <li>Any other ideas which would solve the problem?</li>
  </ul>
  
  <p>Thanks</p>