title: Java Overwriting an existing class method
body: <p>I got a slightly complex issue.</p>
  
  <p>The situation is that I work on a project for automization of selenium-based JUnittests. For this reason I write a testsuite and an abstractTestclass (as base for several testclasses). Also I try to structure my project so that the programmer who has to implement the testcases only has to record them in the selenium IDE and export to JUnit-test-classes. To reach this aim there are some changes required because some methods and functions of the thrown selenium-java-code and the standard selenium packages have little bugs... Such like IE doesn't click sometimes... 
  To avoid those bugs I override the FFDriver and IEDriver class and especially the 'findElement' method. That works fine!</p>
  
  <p>But now I got a new problem. It's the known problem that IE sometimes doesn't click on non-displayed elements. I also have a workaround (Use Javascript-command instead of click()). But as I thought about how to implement I want to do this like the problems I solved with overriding the driver classes and I started override the RemoteWebElement class.</p>
  
  <p>And here comes the problem. If I override the RemoteWebElement class for overriding the click() method I have to use my new RemoteWebElement2_0 class over the whole project instead of the standard. At first guess thats not as worse as it seems (like I have to do the same with the Driver classes). But then I realize that my findElement method delivers a RemoteWebElement not my RemoteWebElement2_0. I see no chance to change the findElement method to deliver my RemoteWebElement2_0 and downcast is not possible.</p>
  
  <p>I have the following ideas to fix this problem but don't know how to implement them or even if the are implementable:</p>
  
  <ul>
  <li>1.) override directly the original RemoteWebElement class (should be possible but how?)</li>
  <li>2.) avoid returning a normal RemoteWebElement in the findElement method</li>
  <li>3.) modify the selenium IDE to doesn't use click() method but a costum implementation</li>
  <li>4.) solve the problem with the click() method with another workaround</li>
  <li>5.) downcasting is not possible!?</li>
  </ul>
  
  <p>Could anyone help me? I know it's a little bit strange but please try to understand me and be not aware to ask if you doesn't!</p>
  
  <p>Here is my Code:</p>
  
  <p>the IEDriver class --></p>
  
  <pre><code>import static org.junit.Assert.fail;
  
  import org.openqa.selenium.By;
  import org.openqa.selenium.NoSuchElementException;
  import org.openqa.selenium.ie.InternetExplorerDriver;
  import org.openqa.selenium.remote.RemoteWebElement;
  
  public class InternetExplorerDriver2_0 extends InternetExplorerDriver {
  
      private static InternetExplorerDriver2_0 instance = null;
      private long startTime;
      private long stopTime;
      private By olderBy;
  
      private InternetExplorerDriver2_0() {
          super();
      }
  
      public static synchronized InternetExplorerDriver2_0 getInstance() {
          if (instance == null) {
              instance = new InternetExplorerDriver2_0();
          }
          return instance;
      }
  
      @Override
      public RemoteWebElement2_0 findElement(By by) {
          return elementSearch(by, InternetExplorerDriver2_0.getInstance());
      }
  
      private RemoteWebElement2_0 elementSearch(By by,
              InternetExplorerDriver driver) {
          startTime = System.currentTimeMillis();
          RemoteWebElement helpingElement = null;
          RemoteWebElement2_0 h2 = null;
          isElementPresent(by);
          try {
  
              //this not allowed downcast causes problems :(
  
              helpingElement = (RemoteWebElement) super.findElement(by);
              h2 = (RemoteWebElement2_0) helpingElement;
          } catch (Exception e) {
              System.out.println("[error] method 'elementSearch' incomplete" + e.getLocalizedMessage());
              fail("Test not successfull!");
          } finally {
              stopTime = System.currentTimeMillis();
              timeWarning(by.toString());
          }
          olderBy = by;
          return h2;
      }
  
      private boolean isElementPresent(By by) {
          try {
              super.findElement(by);
              return true;
          } catch (NoSuchElementException e1) {
              try {
                  InternetExplorerDriver2_0.getInstance().findElement(olderBy).click();
                  super.findElement(by);
                  return true;
              } catch (Exception e2) {
                  stopTime = System.currentTimeMillis();
                  timeWarning(by.toString());
                  AllTest.updateLogger("[main] ERROR\tThe following expression could not be solved: " + by);
                  fail("Test not successfull! --&gt; Error: Element not found. Please check the failed XPath's");
                  return false;
              }
          }
      }
  
      private void timeWarning(String s) {
          if (stopTime - startTime &gt; 500) {
              AllTest.updateLogger("[main] WARNING\tHigh response-time detected: " + (stopTime - startTime) + " ms [@element: " + s + "]");
          }
      }
  }
  </code></pre>
  
  <p>The RemoteWebDriver2_0 class --></p>
  
  <pre><code>import org.openqa.selenium.By;
  import org.openqa.selenium.ElementNotVisibleException;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.remote.RemoteWebDriver;
  import org.openqa.selenium.remote.RemoteWebElement;
  
  public class RemoteWebElement2_0 extends RemoteWebElement {
  
      public RemoteWebElement2_0() {
          super();
      }
  
      @Override
      public void click() {
          try {
              super.click();
          } catch (ElementNotVisibleException e1) {
  
              //Here starts the workaround for the IEDriver.click problem
  
              try {
                  RemoteWebDriver helpDriver = (RemoteWebDriver) this.getWrappedDriver();
                  helpDriver.executeScript("arguments[0].click();", this.getWrappedDriver().findElement(By.id("eb_format_12h")));
              } catch (Exception e2) {
  
              }
          }
      }
  }
  </code></pre>
  
  <p>If you need some more Code please ask for it.</p>
  
  <p>Thanks upwards!!!</p>