0

I have created one override method(click() method) in BaseElement class which implements from WebElement interface.

I would like to call this overridden method on all web elements wherever applicable.

WebElement ele = driver.findElement(By.id("button")); // it returns webelement type by default

How Can I call my override click() method on above web element?

I have tried with down casting ele to BaseElement like below:

BaseElement m_ele = (BaseElement) ele; // No compilation error but throws runtime class casting exception

Any idea could help me here in order to call overridden click() method on WebElement? I dont want to user in-built click() method of WebElement.

Let me know if I have to provide any additional information?

Here is my code snippet:

public class BaseElement implements WebElement {

public BaseElement(WebElement element) {
        m_element = element;
}

@Override
    public void click() {
        try {
            m_element.click();
        } catch (WebDriverException e) {
            try {
                logger.debug("Click Failed, Attempting to click by scrolling into view with bottom align option");
                scrollIntoView();
                m_element.click();
            } catch (WebDriverException ef) {
                try {
                    logger.debug("Click Failed, Attempting to click by scrolling into view with top align option");
                    scrollIntoView(true);
                    m_element.click();
                } catch (WebDriverException e2) {
                    try {
                        logger.debug("Click Failed, Attempting to click by scrolling down by 200 pixels");
                        m_jsDriver.executeScript("window.scrollBy(0,200)");
                        m_element.click();
                    } catch (WebDriverException e3) {
                        logger.debug("Click Failed, Attempting to click by scrolling up by 200 pixels");
                        m_jsDriver.executeScript("window.scrollBy(0,-200)");
                        m_element.click();
                    }
                }
            }
        }
    }
}
Al Imran
  • 882
  • 7
  • 29
Prasad
  • 13
  • 1
  • 5
  • 2
    Update the question with a relevant snippet of BaseElement Class – GPT14 May 29 '18 at 05:11
  • Possible duplicate of [Extend Selenium WebDriver WebElement?](https://stackoverflow.com/questions/11642348/extend-selenium-webdriver-webelement) – GPT14 May 29 '18 at 05:57

1 Answers1

0

You can try the following code to use over ridden click method.

WebElement ele = driver.findElement(By.id("button")); 
BaseElement m_ele = new BaseElement(ele);
m_ele.click();
Murthi
  • 5,299
  • 1
  • 10
  • 15