From 18607d0966580000bb04822809c225930ad556d5 Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Tue, 31 Jan 2012 13:57:57 -0800 Subject: [PATCH] Try to detect when a command starts a page load and wait for it to finish --- spec/driver_spec.rb | 17 ++++++++--------- src/Connection.cpp | 20 +++++++++++++++++++- src/Connection.h | 3 +++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index 69b8d98..be0cd18 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -783,14 +783,13 @@ describe Capybara::Driver::Webkit do context "slow app" do before(:all) do + @result = "" @app = lambda do |env| - body = <<-HTML - -
-

#{env['PATH_INFO']}

- - HTML - sleep(0.5) + if env["PATH_INFO"] == "/result" + sleep(0.5) + @result << "finished" + end + body = %{Go} [200, { 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, [body]] @@ -798,8 +797,8 @@ describe Capybara::Driver::Webkit do end it "waits for a request to load" do - subject.find("//input").first.click - subject.find("//p").first.text.should == "/next" + subject.find("//a").first.click + @result.should == "finished" end end diff --git a/src/Connection.cpp b/src/Connection.cpp index f442ae0..c798f6d 100644 --- a/src/Connection.cpp +++ b/src/Connection.cpp @@ -17,6 +17,8 @@ Connection::Connection(QTcpSocket *socket, WebPage *page, QObject *parent) : m_command = NULL; m_pageSuccess = true; m_commandWaiting = false; + m_pageLoadingFromCommand = false; + m_pendingResponse = NULL; connect(m_socket, SIGNAL(readyRead()), m_commandParser, SLOT(checkNext())); connect(m_commandParser, SIGNAL(commandReady(QString, QStringList)), this, SLOT(commandReady(QString, QStringList))); connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool))); @@ -38,6 +40,7 @@ void Connection::startCommand() { if (m_pageSuccess) { m_command = m_commandFactory->createCommand(m_commandName.toAscii().constData()); if (m_command) { + connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand())); connect(m_command, SIGNAL(finished(Response *)), this, @@ -55,16 +58,31 @@ void Connection::startCommand() { } } +void Connection::pageLoadingFromCommand() { + m_pageLoadingFromCommand = true; +} + void Connection::pendingLoadFinished(bool success) { m_pageSuccess = success; if (m_commandWaiting) startCommand(); + if (m_pageLoadingFromCommand) { + m_pageLoadingFromCommand = false; + if (m_pendingResponse) { + writeResponse(m_pendingResponse); + m_pendingResponse = NULL; + } + } } void Connection::finishCommand(Response *response) { + disconnect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand())); m_command->deleteLater(); m_command = NULL; - writeResponse(response); + if (m_pageLoadingFromCommand) + m_pendingResponse = response; + else + writeResponse(response); } void Connection::writeResponse(Response *response) { diff --git a/src/Connection.h b/src/Connection.h index b39d355..e6682e0 100644 --- a/src/Connection.h +++ b/src/Connection.h @@ -18,6 +18,7 @@ class Connection : public QObject { void commandReady(QString commandName, QStringList arguments); void finishCommand(Response *response); void pendingLoadFinished(bool success); + void pageLoadingFromCommand(); private: void startCommand(); @@ -32,5 +33,7 @@ class Connection : public QObject { CommandFactory *m_commandFactory; bool m_pageSuccess; bool m_commandWaiting; + bool m_pageLoadingFromCommand; + Response *m_pendingResponse; };