0

I´m using Selenium, and I need to start testing my page not only when the page is render, but when all javascript initialization is done. I´m looking to avoid Race condition on my test because some elements are not initialized properly yet.

Simple question there´s any way to ask the DOM when all javascript render has finish?. We know when render has done because of onDocumentReady. But then, we can start initializing some javascripts, and doing some business logic.

I want to know if there´s any mechanism to know when all javascript execution has finish. I read the use of promise object for some async initialization, but I was looking for a more generic way to know it.

paul
  • 12,873
  • 23
  • 91
  • 153
  • I think this is a duplicate of this question. http://stackoverflow.com/questions/1033398/execute-javascript-when-page-has-fully-loaded – Swagin9 Jun 16 '15 at 15:38

1 Answers1

2

There is no generic way to know that JavaScript code has finished doing its initialization. A single page can load dozens of JavaScript libraries that can all do their own thing. Even if you can detect that an initial Ajax operation has completed, there is generally no way to know that this initial operation won't be followed by other operations that are also part of the initialization.

What you have to do is find a way to determine that, as far as your test is concerned, the page is initialized enough for the test to start. How you do this depends on the JavaScript code that is running on your page. Here's an example. For some of my applications I use DataTables to manage some of my tables. The data that appears in a table is loaded through an Ajax call. For some tests, I know that once the data is loaded, the table will have X number of rows so I wait for this many rows to appear in the table and then proceed with the test. This works for some tests. For other tests that do more complicated testing, I have to register an event handler and listen for a table redraw event before the test can proceed. If the only thing I'm testing is the table, it would be wasteful to wait until other stuff on the page has finished initializing.

Louis
  • 146,715
  • 28
  • 274
  • 320
  • yes, that´s exactly what we´re doing, but still there´s some values that sometimes are present on time and sometimes reach the timeout render of selenium. Increase the timeout it would increase the total time of my integration test. – paul Jun 17 '15 at 07:54