I think that there is a bright future in using PyQt4 with a QWebView
object for writing basic web tests, and for web automation.  My early
experiments suggest that it is a much simpler and faster solution, when
one needs web page browsing with JavaScript enabled, than a heavyweight
technique like running and remotely controlling a separate browser
process.

But though my first few page loads and clicks went well, I quickly found
that sites of any complexity would cause my Python script to crash with
a segmentation fault.  Many of my early examples were non-deterministic
- the script would sometimes crash, but sometimes succeed - but I have
now found a test script which always crashes, and that therefore might
serve as a good example of this bug here on the mailing list.

The script is attached to this email.  Its output when I run it against
the PyQt4 shipped with Ubuntu Maverick is this:

$ python2.6 qtcrash.py
4.7.0
4.7.4
zsh: segmentation fault  python2.6 qtcrash.py

When I download and install the current PyQt4 snapshot instead, this is
the output:

$ ./v/bin/python2.6 qtcrash.py
4.7.0
snapshot-4.8.2-f509333f6b01
zsh: segmentation fault  ./v/bin/python2.6 qtcrash.py

Is anyone else able to reproduce this?  Where would I get started in
tracking down why this code causes PyQt4 to produce a segmentation
fault?  Thanks for any guidance!

"""Routines that actually speak Qt to drive its web views."""

import sys
from PyQt4 import QtCore, QtGui, QtWebKit

print QtCore.QT_VERSION_STR
print QtCore.PYQT_VERSION_STR

class SimpleTest(object):
    def __init__(self):
        self.current_callback = self.callback1

    def step(self):
        next = self.current_callback()
        if next is not None:
            self.current_callback = next

    def findAll(self, selector):
        return list(self.webview.page().mainFrame().findAllElements(selector))

    def click(self, element):
        element.evaluateJavaScript(
            "var event = document.createEvent('MouseEvents');"
            "event.initEvent('click', true, true);"
            "this.dispatchEvent(event);"
            )

    def callback1(self):
        self.webview = QtWebKit.QWebView()
        self.webview.show()
        self.webview.load(QtCore.QUrl(
                'http://ec2-184-73-115-78.compute-1.amazonaws.com/simple/'
                ))
        return self.callback2

    def callback2(self):
        elist = self.findAll('div#content > p > a')
        if elist:
            self.click(elist[0])
            return self.callback3

    def callback3(self):
        elist = self.findAll('div#ferns-info > h2 > a')
        if elist:
            self.click(elist[0])
            return self.callback4

    def callback4(self):
        elist = self.findAll('div#lycophytes-info > h2 > a')
        if elist:
            self.click(elist[0])
            return self.callback5

    def callback5(self):
        pass

# The Qt application itself.

app = QtGui.QApplication(sys.argv)
tester = SimpleTest()
timer = QtCore.QTimer()
timer.timeout.connect(tester.step)
timer.start(50)
sys.exit(app.exec_())
-- 
Brandon Craig Rhodes   [email protected]   http://rhodesmill.org/brandon
_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to