1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00

Don't wait for finished replies

Synchronous requests will have emitted the finished signal by the time
we connect to it.
This commit is contained in:
Matthew Horan 2012-12-03 22:47:10 -05:00
parent 615698037e
commit 5bef33624c
6 changed files with 58 additions and 21 deletions

View file

@ -2055,6 +2055,43 @@ describe Capybara::Webkit::Driver do
end
end
context "synchronous ajax app" do
let(:driver) do
driver_for_app do
get '/' do
<<-HTML
<html>
<body>
<form id="theForm">
<input type="submit" value="Submit" />
</form>
<script>
document.getElementById('theForm').onsubmit = function() {
xhr = new XMLHttpRequest();
xhr.open('POST', '/', false);
xhr.setRequestHeader('Content-Type', 'text/plain');
xhr.send('hello');
console.log(xhr.response);
}
</script>
</body>
</html>
HTML
end
post '/' do
request.body.read
end
end
end
it 'should not hang the server' do
visit('/')
driver.find('//input').first.click
driver.console_messages.first[:message].should == "hello"
end
end
def driver_url(driver, path)
URI.parse(driver.current_url).merge(path).to_s
end

View file

@ -10,8 +10,7 @@ UnsupportedContentHandler::UnsupportedContentHandler(WebPage *page, QNetworkRepl
void UnsupportedContentHandler::renderNonHtmlContent() {
QByteArray text = m_reply->readAll();
m_page->mainFrame()->setContent(text, QString("text/plain"), m_reply->url());
m_page->setUnsupportedContentLoaded();
m_page->networkAccessManagerFinishedReply(m_reply);
m_page->unsupportedContentFinishedReply(m_reply);
m_page->loadFinished(true);
this->deleteLater();
}

View file

@ -45,18 +45,15 @@ void WebPage::setCustomNetworkAccessManager() {
NetworkAccessManager *manager = new NetworkAccessManager(this);
manager->setCookieJar(m_manager->cookieJar());
this->setNetworkAccessManager(manager);
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(networkAccessManagerFinishedReply(QNetworkReply *)));
connect(manager, SIGNAL(sslErrors(QNetworkReply *, QList<QSslError>)),
this, SLOT(handleSslErrorsForReply(QNetworkReply *, QList<QSslError>)));
connect(manager, SIGNAL(requestCreated(QByteArray &, QNetworkReply *)), this, SLOT(networkAccessManagerCreatedRequest(QByteArray &, QNetworkReply *)));
connect(manager, SIGNAL(requestCreated(QByteArray &, QNetworkReply *)),
SIGNAL(requestCreated(QByteArray &, QNetworkReply *)));
}
void WebPage::networkAccessManagerCreatedRequest(QByteArray &url, QNetworkReply *reply) {
emit requestCreated(url, reply);
}
void WebPage::networkAccessManagerFinishedReply(QNetworkReply *reply) {
emit replyFinished(reply);
void WebPage::unsupportedContentFinishedReply(QNetworkReply *reply) {
m_unsupportedContentLoaded = true;
m_manager->replyFinished(reply);
}
void WebPage::loadJavascript() {
@ -276,10 +273,6 @@ void WebPage::handleUnsupportedContent(QNetworkReply *reply) {
}
}
void WebPage::setUnsupportedContentLoaded() {
m_unsupportedContentLoaded = true;
}
bool WebPage::unsupportedContentLoaded() {
return m_unsupportedContentLoaded;
}

View file

@ -34,8 +34,8 @@ class WebPage : public QWebPage {
bool matchesWindowSelector(QString);
void setFocus();
NetworkAccessManager *networkAccessManager();
void setUnsupportedContentLoaded();
bool unsupportedContentLoaded();
void unsupportedContentFinishedReply(QNetworkReply *reply);
public slots:
bool shouldInterruptJavaScript();
@ -47,8 +47,6 @@ class WebPage : public QWebPage {
void frameCreated(QWebFrame *);
void handleSslErrorsForReply(QNetworkReply *reply, const QList<QSslError> &);
void handleUnsupportedContent(QNetworkReply *reply);
void networkAccessManagerCreatedRequest(QByteArray &url, QNetworkReply *reply);
void networkAccessManagerFinishedReply(QNetworkReply *reply);
signals:
void pageFinished(bool);

View file

@ -36,8 +36,6 @@ WebPage *WebPageManager::createPage(QObject *parent) {
this, SLOT(setPageStatus(bool)));
connect(page, SIGNAL(requestCreated(QByteArray &, QNetworkReply *)),
this, SLOT(requestCreated(QByteArray &, QNetworkReply *)));
connect(page, SIGNAL(replyFinished(QNetworkReply *)),
this, SLOT(replyFinished(QNetworkReply *)));
append(page);
return page;
}
@ -51,8 +49,19 @@ void WebPageManager::emitLoadStarted() {
void WebPageManager::requestCreated(QByteArray &url, QNetworkReply *reply) {
logger() << "Started request to" << url;
if (reply->isFinished())
replyFinished(reply);
else {
connect(reply, SIGNAL(finished()), SLOT(handleReplyFinished()));
m_started += reply;
}
}
void WebPageManager::handleReplyFinished() {
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
disconnect(reply, SIGNAL(finished()), this, SLOT(handleReplyFinished()));
replyFinished(reply);
}
void WebPageManager::replyFinished(QNetworkReply *reply) {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

View file

@ -29,12 +29,13 @@ class WebPageManager : public QObject {
bool isLoading() const;
QDebug logger() const;
void enableLogging();
void replyFinished(QNetworkReply *reply);
public slots:
void emitLoadStarted();
void setPageStatus(bool);
void requestCreated(QByteArray &url, QNetworkReply *reply);
void replyFinished(QNetworkReply *reply);
void handleReplyFinished();
signals:
void pageFinished(bool);