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

Try to detect when a command starts a page load and wait for it to finish

This commit is contained in:
Joe Ferris 2012-01-31 13:57:57 -08:00
parent 0e32d3c677
commit 18607d0966
3 changed files with 30 additions and 10 deletions

View file

@ -783,14 +783,13 @@ describe Capybara::Driver::Webkit do
context "slow app" do
before(:all) do
@result = ""
@app = lambda do |env|
body = <<-HTML
<html><body>
<form action="/next"><input type="submit"/></form>
<p>#{env['PATH_INFO']}</p>
</body></html>
HTML
sleep(0.5)
if env["PATH_INFO"] == "/result"
sleep(0.5)
@result << "finished"
end
body = %{<html><body><a href="/result">Go</a></body></html>}
[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

View file

@ -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) {

View file

@ -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;
};