Add API to start load and wait till DOM is ready

This commit is contained in:
Kovid Goyal 2013-03-31 08:45:11 +05:30
parent a671413989
commit 64431fa9fb

View File

@ -92,6 +92,10 @@ class WebPage(QWebPage): # {{{
def on_unsupported_content(self, reply): def on_unsupported_content(self, reply):
self.log.warn('Unsupported content, ignoring: %s'%reply.url()) self.log.warn('Unsupported content, ignoring: %s'%reply.url())
@property
def ready_state(self):
return unicode(self.mainFrame().evaluateJavaScript('document.readyState').toString())
# }}} # }}}
class ProxyFactory(QNetworkProxyFactory): # {{{ class ProxyFactory(QNetworkProxyFactory): # {{{
@ -368,6 +372,29 @@ class Browser(QObject, FormsMixin):
self.page.mainFrame().load(QUrl(url)) self.page.mainFrame().load(QUrl(url))
return self._wait_for_load(timeout, url) return self._wait_for_load(timeout, url)
@property
def dom_ready(self):
return self.page.ready_state in {'complete', 'interactive'}
def wait_till_dom_ready(self, timeout=30.0, url=None):
start_time = time.time()
while not self.dom_ready:
if time.time() - start_time > timeout:
raise Timeout('Loading of %r took longer than %d seconds'%(
url, timeout))
self.run_for_a_time(0.1)
def start_load(self, url, timeout=30.0):
'''
Start the loading of the page at url and return once the DOM is ready,
sub-resources such as scripts/stylesheets/images/etc. may not have all
loaded.
'''
self.current_form = None
self.page.mainFrame().load(QUrl(url))
self.run_for_a_time(0.01)
self.wait_till_dom_ready(timeout=timeout, url=url)
def click(self, qwe_or_selector, wait_for_load=True, ajax_replies=0, timeout=30.0): def click(self, qwe_or_selector, wait_for_load=True, ajax_replies=0, timeout=30.0):
''' '''
Click the :class:`QWebElement` pointed to by qwe_or_selector. Click the :class:`QWebElement` pointed to by qwe_or_selector.