4

A set of logically related input fields have an onchange event. After iterating over the fields and modifying their values some get updated correctly and some don't because of the stuff done by onchange event.

The moment the onchange event triggers on a field, it starts some processing (involving other related fields), stores the value somewhere and clears other related fields if they weren't previously processed by their own onchange event.

I could put the thread to sleep for an arbitrary amount of time but that doesn't look good. It would be just guessing how much time is going to take the processing and choosing between wasting time in a generous sleep or having a script that can abort due timeouts.

Is there a way to know when the JavaScript code (which is called by the onchange event) has finished doing its work?

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:

I experienced some issues in the way so I just think it's worth mentioning the lessons learned in brief notes:

WARNING: Do not mix implicit and explicit waits.

Use WebDriverWait (specialization of FluentWait) instead of FluentWait, unless you have a very specific requirement. E.g., WebDriverWait ignores NotFoundException (NoSuchElementException's superclass) by default. See recomendation.

CamelCamelius
  • 333
  • 2
  • 15

4 Answers4

3

After serious refactoring and some research, I finally made it. The onchange event fires when the value of the input field is changed and the element loses focus. Manipulating the fields with WebElement (e.g. sendKeys()) is not an option because you are not in control of the background processing, so using JavascriptExecutor is the choice. First, I updated the value of the field using JavaScript (which does NOT trigger the event), and after that, I triggered the onchange event, also using JavaScript:

//Setting implicit wait to 0 to avoid mess with explicit waits
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
//Use WebDriverWait instead of FluentWait (it's superclass)
Wait<WebDriver> wait = new WebDriverWait(driver, 25, 500);
for(int i = 1; i <= fieldCount; i++) {
    String elementId = "field$" + i;
    String javaScript = String.format("document.getElementById('%s').value='%s';", elementId , myValue);
    Object jsResult = wait.until(ExpectedConditions.javaScriptThrowsNoExceptions(javaScript));
    javaScript = String.format("return document.getElementById('%s').dispatchEvent(new Event('change'));", elementId);
    jsResult = wait.until(ExpectedConditions.jsReturnsValue(javaScript));
}

There are some key aspects here.

  • Do NOT mix implicit and explicit waits (I learnt it the hard way), it will cause unexpected results.
  • Use WebDriverWait (specialization of FluentWait) instead of its superclass, unless you have a very specific requirement. If you use FluentWait make sure to ignore appropriate exceptions; otherwise you will start getting NoSuchElementException.
  • The onchange event fires when the value of the input field is changed and the element loses focus.
  • dispatchEvent() dispatches an Event at the specified EventTarget, (synchronously) invoking the affected EventListeners in the appropriate order. This applies for custom events as well. This is a very nice post for events.

I used the following code to get a better grasp of ExpectedConditions.javaScriptThrowsNoExceptions and ExpectedConditions.jsReturnsValue. It is a JavaScript function call that just keeps the engine busy for few seconds. That way you can see the interaction of the explicit wait with JavaScript and inspect the return values. Notice that the JS code is slightly different for each ExpectedCondition:

//ExpectedConditions.jsReturnsValue
String javaScript = "(function watcher(ms){var start=new Date().getTime();var end = start;while(end<start+ms){end=new Date().getTime();};return 'complete';})(5000);return 'success';";
log.trace("javaScript={}", javaScript);
Object jsResult = wait.until(ExpectedConditions.jsReturnsValue(javaScript));
log.trace("jsResult={}", jsResult);

//ExpectedConditions.javaScriptThrowsNoExceptions
javaScript = "(function watcher(ms){var start=new Date().getTime();var end = start;while(end<start+ms){end=new Date().getTime();};return 'complete';})(5000);";
log.trace("javaScript={}", javaScript);
jsResult = wait.until(ExpectedConditions.javaScriptThrowsNoExceptions(javaScript));
log.trace("jsResult={}", jsResult);
CamelCamelius
  • 333
  • 2
  • 15
1

You might try this method which waits until JQuery is set to inactive:

/**
 * Wait until JQuery is inactive
 * @author Bill Hileman
 */
public void waitForJQueryToBeInactive() {

    Boolean isJqueryUsed = (Boolean) ((JavascriptExecutor) driver)
            .executeScript("return (typeof(jQuery) != 'undefined')");

    if (isJqueryUsed) {
        while (true) {
            // JavaScript test to verify jQuery is active or not
            Boolean ajaxIsComplete = (Boolean) (((JavascriptExecutor) driver)
                    .executeScript("return jQuery.active == 0"));
            if (ajaxIsComplete)
                break;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }

}
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • Thanks. JQuery is not being used. Interesting `jQuery.active`. Didn't know about it. According to [question](https://stackoverflow.com/questions/3148225/jquery-active-function) it would work only if the code in execution is an ajax call, did I get it right ? Is there anything like that but that works with JavaScript ? – CamelCamelius Nov 08 '18 at 16:49
  • I do have a more general waitForPageToLoad method that uses javascript to check the document ready state for "complete" It might help, since that flag might be getting changed while the web elements are "busy" I'll post it as a separate answer. – Bill Hileman Nov 08 '18 at 17:41
0

To my best of knowledge, you can't wait for an asynchronous event using Selenium Web Driver. You may, however, be able to wait for effects of that event using WebDriverWait class. If events You mentioned, make some changes in the DOM, you can detect those changes by using selected ExpectedConditions.

   Wait<WebDriver> wait = new WebDriverWait(driver, 15, 500);

   // here you make some change to the input

   wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input")));

This simple example would wait until a button is active. If the expected conditions are not met in the next 15s exception will be thrown.

In case, that provided set of ExpectedConditions is insufficient you can always create your own by implementing ExpectedCondition interface. For more information visit the documentation.

https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/package-summary.html https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/package-summary.html

CamelCamelius
  • 333
  • 2
  • 15
Mateusz
  • 26
  • 3
  • Thanks. Just a side note, I am working with input fields instead of buttons. I would use `ExpectedConditions.elementToBeClickable` but I get an exception so I just check `ExpectedConditions.visibilityOf`. The issue is that the `onchange` events on fields seem to be clearing other fields if they haven't been validated so I wanted to see a way to determine when the event finishes – CamelCamelius Nov 08 '18 at 16:21
  • That was just an idea, maybe I could give a better solution, but I'm still unclear about what You're trying to achieve. If the idea is to see if the fields are empty you could write some custom ExpectedCondition to wait for that specific situation. – Mateusz Nov 08 '18 at 17:01
  • 1
    Got it. Fields are empty and I setup a value. After that it runs some JavaScript. If I populate the next related field immediately, it loses it's content but if I give some time so the JavaScript finishes whatever it is doing, I can update the next related field without issues. Also they look the same before and after the `onchange` event...it's just the background processing what's giving issues and wanted to check if there was a way to do it without hardcoding a sleep time. Thanks – CamelCamelius Nov 08 '18 at 19:41
  • Ok, I think I understand now. You have a set of connected input fields, that if you change one, the asynchronous task prohibits you from changing the next one, however, it does not change its original value (the next one). – Mateusz Nov 08 '18 at 20:19
  • I also must ask are there any visible indications for the user of such process taking place in the background? – Mateusz Nov 08 '18 at 20:30
  • **First comment's answer:** I would say the asynchronous task modifies the other fields instead of prohibiting you to change them because if you do it manually very fast you see the value disappear. I think it might be storing them. **Second comment's answer:** I don't see any visible indication for the processing – CamelCamelius Nov 08 '18 at 22:00
  • I am assuming that you don't have influence over the javaScript code, but the situation you are describing seems like a case of bad UI, so it would seem like a good idea to report it. I would also like You to try manually to do the following: (1) set dummy data on the second field. (2) set desired data on the first one (3) after few second see if the first value is set, and the second is reset or default. If that would be the case I may be able to help You with some workaround. – Mateusz Nov 09 '18 at 13:02
  • Correct, I don't have influence in the JS. I tried manually setting the Second field first and the same thing happened. If I do it fast the value disappears but if I do it fast the value persists...after moving the code I got something different...I am editing question to add findings...also after some debugging I included your code in the original question because it works like a charm :) – CamelCamelius Nov 10 '18 at 07:35
0

The following routine checks the document ready state for "complete" and returns when the page is finished loading/changing.

/**
 * Wait for the web page to finish loading
 * @author Bill Hileman
 */
public void waitForPageToLoad() {

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(new ExpectedCondition<Boolean>() {

        public Boolean apply(WebDriver wdriver) {
            return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
        }
    });
}
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • Thanks Bill! Unfortunately I am already executing `return document.readyState` so everything is already loaded. Sometimes I even have to get the element everytime I want to execute an action on it or I end up getting an error saying that the element is no longer in cache, so I verify elements are there. – CamelCamelius Nov 08 '18 at 22:07