[Selenium WebDriver] - Python - Javascript, ajax and page loads

1,722 views
Skip to first unread message

Ruben

unread,
May 25, 2012, 3:40:33 AM5/25/12
to selenium-...@googlegroups.com
Hello there,

Since a couple of weeks I have been using Selenium as the testing framework for the webapp we develop.
Now, in most cases Selenium works great. Especially the WebDriver creates a great way of testing because it is as close as it gets to a real user.
I'm only running into the follow issue (a lot):

It seems that the Firefox WebDriver waits for the DOM to be loaded

content/server.js->line 49
[code]
 appcontent.addEventListener('DOMContentLoaded',
            function(e) {
              domMessenger.onPageLoad(e);
            }, true);
[/code]
(please do correct me if i'm wrong on this one) - [ See bottom of my post for errors that often occur ]

But... when running a page with JavaScript, JavaScript is going to be executed after the DOM is loaded. This would mean that JavaScript is executed but the WebDriver is already telling the framework that the page is done.
I think this is probably causing the issue while loading the page. You can solve this by inserting a sleep that takes up 1 or 2 seconds. The only problem is that this is a "guess". Me as a testcase writer assumes that the page is fully loaded after that 1 (or 2) seconds.

I was wondering if you guy's (or other people) have a solution for this. I'm not aware of a method that allows you to check if your browser (Firefox) is done executing JavaScript. Because at the moment, determining if the DOM is loaded doesn't mean that the page is fully ready to continue my testcase.

Is anyone else running into this issue?

Cheers,
Ruben.

Errors that often occur:
 - "Modal dialog present".
 - Failed ajax calls simply by continuing to quickly with the test

Mark Collin

unread,
May 28, 2012, 4:08:13 AM5/28/12
to selenium-...@googlegroups.com

Welcome to the nightmare that is working out when a page has actually loaded.  Should it be:

 

·         When a page has finished downloading from the server -> if so how long do we need to stop downloading data for us to know it has finished?)

·         Should it be when the DOM has been created -> What if the DOM is modified after creation?

·         Should it be when JavaScript processing has completed -> What about AJAX?  What about reloading Adverts? What about Heatmapping analytics? (And a million other JavaScript things that could happen at any time)

 

You could try sending over a “return document.readyState == 'complete';" using the JavaScrip executor but YMMV.

 

Have a look at this thread:

 

https://groups.google.com/d/topic/selenium-developers/WuqiU7cKOTk/discussion

 

It should give a much better rounded view of what I’ve said above.

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-developers/-/-xNKcRSAALcJ.
To post to this group, send email to selenium-...@googlegroups.com.
To unsubscribe from this group, send email to selenium-develo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-developers?hl=en.

Patrick Lightbody

unread,
May 28, 2012, 11:47:37 AM5/28/12
to selenium-...@googlegroups.com
Yup, it's hard. Blame AJAX :)

One thing that I find works well is to send the test through BrowserMob Proxy (http://opensource.webmetrics.com/browsermob-proxy/) and use the waitForNetworkTrafficToStop command.

--
Patrick Lightbody



Mel Llaguno

unread,
May 28, 2012, 8:54:46 PM5/28/12
to selenium-...@googlegroups.com
Another way to deal with this is to instrument the AUT to provide a javascript call which evaluates whether or not the application is ready. When paired with a modal spinner, this solution is ideal. Use blocking javascript executor and wrap all WebDriver calls to evaluate the application call back script prior to executing the command. This ensures that the application is ready to continue. 

AFAIK, there is no way for the browser to determine when the DOM is ready (specially if you include different browser variations) given it is easily modified via AJAX. Only the application can know.

M.

Ruben

unread,
May 28, 2012, 9:24:21 PM5/28/12
to selenium-...@googlegroups.com
@Patrick, thanks but I don't think I want to wait till there is no more network traffic. The main issue (I think) with this is that JS can still be running even though network traffic has stopped...
@Mel.llaguna I also thought about that, although this is quiet a drastic option because we would have to program this at the beginning and end of every function.

@Mark as you and @Mel.llaguno said, defining when the page is loaded is very difficult/almost impossible. But so far I can check for the onload event (the driver listens to this), I can check if jQuery is done executing its AJAX calls, the only thing I cannot do is see if JS is done. I was wondering if it is possible to use the Firefox API to get the JavaScript queue. Maybe this queue will help with determining if JS is done or only running a certain type of function (example: sleeps).

Any toughs if this is possible?
M.

To post to this group, send email to selenium-developers@googlegroups.com.
To unsubscribe from this group, send email to selenium-developers+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-developers?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.
To post to this group, send email to selenium-developers@googlegroups.com.
To unsubscribe from this group, send email to selenium-developers+unsub...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/selenium-developers?hl=en.

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.
To post to this group, send email to selenium-developers@googlegroups.com.
To unsubscribe from this group, send email to selenium-developers+unsub...@googlegroups.com.

Mark Collin

unread,
May 30, 2012, 6:12:50 AM5/30/12
to selenium-...@googlegroups.com

If you are using a JavaScript framework like jQuery they should have something to help you. 

 

jQuery has a:

 

jQuery.isReady -> True when DOM is loaded.

jQuery.active -> 0 when no AJAX is being processed.

 

You could also have a look at :

 

https://developer.mozilla.org/en/DOM/document.readyState

M.

To post to this group, send email to selenium-...@googlegroups.com.
To unsubscribe from this group, send email to selenium-develo...@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/selenium-developers?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.

To post to this group, send email to selenium-...@googlegroups.com.
To unsubscribe from this group, send email to selenium-develo...@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/selenium-developers?hl=en.

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.

To post to this group, send email to selenium-...@googlegroups.com.
To unsubscribe from this group, send email to selenium-develo...@googlegroups.com.


For more options, visit this group at http://groups.google.com/group/selenium-developers?hl=en.

--
You received this message because you are subscribed to the Google Groups "Selenium Developers" group.

To view this discussion on the web visit https://groups.google.com/d/msg/selenium-developers/-/77Pu48wnlZUJ.
To post to this group, send email to selenium-...@googlegroups.com.
To unsubscribe from this group, send email to selenium-develo...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages