Fix Inconsistent JS Selenium Tests with element.isEnabled()

I’ve been writing automated regression tests at work over the past month, using Jest and Selenium WebDriver, and have really been struggling to get my tests to pass consistently. I began to suspect that methods like wait(until.elementIsVisible... and wait(until.elementTextIs... did not actually do what I expected since I was getting so many inconsistent failures. I began searching for a way to determine if an element is clickable with Selenium WebDriverJS. This is possible in other languages (I think Python and Java at least), but it’s not available for JavaScript. But it turns out you can make it work.

I ran across this Stack Overflow answer which says,

There does not seem to be a condition equivalent to Python’s selenium.webdriver.support.expected_conditions.element_to_be_clickable. However, looking at the source for that condition, I see that it does two checks:

  1. That the element is visible.
  2. That it is enabled.

So you could wait for both conditions to become true.

This was very exciting indeed. I refactored my helper functions simply by slapping .isEnabled() on the end of lines like driver.wait(until.elementLocated(By.css('#overview-link')), config.timeout), ran a test suite 100 times using the zsh command repeat, and voila! Every. Single. Test. Passed!

I highly recommend using .isEnabled() instead of just relying on wait(until.elementLocated....  And add .isVisible() as needed. I found it wasn’t consistently helpful for me by itself but in combination with .isEnabled() it should be quite consistent.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s