When a group of interconnected input
fields have an onchange
event, the values in some fields are updated correctly while others are not due to interference from the onchange
event.
Once the onchange
event is triggered on a field, it initiates a process that involves other related fields. It stores the value somewhere and clears other related fields if they were not previously processed by their own onchange
event.
I have considered pausing the thread for a set period of time, but this approach seems unreliable. It would involve guessing the processing time and deciding between idle waiting or risking script timeouts.
Is there a way to determine when the JavaScript code (called by the onchange
event) has completed its tasks?
Original Code
Wait<WebDriver> wait = new WebDriverWait(driver, 25, 500);
for(int i = 1; i <= fieldCount; i++) {
elementId = "field$" + i;
wait.until(ExpectedConditions.elementToBeClickable(By.id(elementId)));
driver.findElementById(elementId).sendKeys(data);
//The mess happens if I don't sleep
Thread.sleep(3000);
}
Output
With sleep: Field1
:_w_
... Field2
:_x_
... Field3
:_y_
... FieldN
:_z_
Without sleep: Field1
:_w_
... Field2
:___
... Field3
:_y_
... FieldN
:___
Notes:
While encountering issues, I learned valuable lessons which I feel are important to highlight:
WARNING: Do not mix implicit and explicit waits.
Prefer using
WebDriverWait
overFluentWait
, unless you have very specific requirements. For example,WebDriverWait
handlesNotFoundException
automatically. Refer to this recommendation.