3

Is it good practice to override WebElement click() to add some wait functionality, because in some pages i need to click button and in some cases button is not loaded yet so i added wait to check if element is visible.

So My question is: Is it better to create abstract class which would implement WebElement and override click() method to add some wait functionality or it is better just to have plain wait only in specific pages ?

Jainish Kapadia
  • 2,603
  • 5
  • 18
  • 29
  • It is always good to add waits with timeouts. You need them to make your tests more reliable across platforms. – Sameer Aug 30 '17 at 07:16
  • In some pages, if button is not loaded completely, then you should provide `explicit wait` before going to hit the button. – Jainish Kapadia Aug 30 '17 at 07:17

3 Answers3

6

As per the documentation Document Object Model (DOM) Level 2 HTML Specification click() method doesn't accepts any parameters, doesn't return any value and doesn't report any exceptions.

enter image description here

So, its clear that we can't override click() method in the first place.

But you can always write a customized user function to click on a WebElement along with certain types of waits. In these cases Selenium's in-built Explicit Wait i.e. the WebDriverWait comes to our help.


ExplicitWait

As per the documentation, an ExplicitWait is a code block you define, configure and implement for the WebDriver instance to wait for a certain condition to be met before proceeding for the next line of code. There are some methods that helps us to implement ExplicitWait that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one of the way ExplicitWait can be achieved.

You can find a detailed discussion on ExplicitWait in this discussion.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

This is not a correct way to do it, the problems which may occurs due to wait

1- For each click event it will wait for some time.

2- Whatever wait time you are giving may end before the page loads completely (it will work for some pages and fail for the others)

Instead you find that element first and continue the executions see below questions for more details

WebDriver: check if an element exists?

Selenium WebDriver - Test if element is present

smali
  • 4,687
  • 7
  • 38
  • 60
0

I don't think you will be able to do that, as WebElement is an interface not an class (or abstract class).

However if you want, you could implement your own click method which does waiting and then clicking for you.

public void waitAndClick(By identifer){
    WebDriverWait wait = new WebDriverWait(driver, 30);
    WebElement elem = wait.until(ExpectedConditions.presenceOfElementLocated(identifer));
    elem.click();
}
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137