I have this web code: This code work perfectly in Chrome. But I want to test it with HtmlUnit. I wrote: This code works randomly. Sometime the test passed and sometimes failed. I think it is due because of the asynchronous call to JS, but I couldn’t solve it. So how can I test it? Besides, there is better solution