Skip to content

Commit

Permalink
Don't create a command until all arguments are received; don't start …
Browse files Browse the repository at this point in the history
…the next command if a page is still loading
  • Loading branch information
jferris committed Mar 9, 2011
1 parent 5965f8a commit a8e64a5
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 24 deletions.
22 changes: 22 additions & 0 deletions spec/driver_spec.rb
Expand Up @@ -309,4 +309,26 @@
parent.find("./*[@class='find']").map(&:text).should == %w(Expected)
end
end

context "slow app" do
let(:app) do
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)
[200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
[body]]
end
end

it "waits for a request to load" do
subject.find("//input").first.click
subject.find("//p").first.text.should == "/next"
end
end
end
63 changes: 40 additions & 23 deletions src/Connection.cpp
@@ -1,4 +1,5 @@
#include "Connection.h"
#include "WebPage.h"
#include "Visit.h"
#include "Find.h"
#include "Command.h"
Expand Down Expand Up @@ -54,33 +55,15 @@ void Connection::readDataBlock() {
}

void Connection::processNext(const char *data) {
if (m_command) {
continueCommand(data);
if (m_commandName.isNull()) {
m_commandName = data;
m_argumentsExpected = -1;
} else {
m_command = createCommand(data);
if (m_command) {
startCommand();
} else {
QString failure = QString("Unknown command: ") + data + "\n";
writeResponse(false, failure);
}
processArgument(data);
}
}

Command *Connection::createCommand(const char *name) {
#include "find_command.h"
return NULL;
}

void Connection::startCommand() {
m_argumentsExpected = -1;
connect(m_command,
SIGNAL(finished(bool, QString &)),
this,
SLOT(finishCommand(bool, QString &)));
}

void Connection::continueCommand(const char *data) {
void Connection::processArgument(const char *data) {
if (m_argumentsExpected == -1) {
m_argumentsExpected = QString(data).toInt();
} else if (m_expectingDataSize == -1) {
Expand All @@ -90,7 +73,40 @@ void Connection::continueCommand(const char *data) {
}

if (m_arguments.length() == m_argumentsExpected) {
if (m_page->isLoading())
connect(m_page, SIGNAL(loadFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
else
startCommand();
}
}

void Connection::startCommand() {
m_command = createCommand(m_commandName.toAscii().constData());
if (m_command) {
connect(m_command,
SIGNAL(finished(bool, QString &)),
this,
SLOT(finishCommand(bool, QString &)));
m_command->start(m_arguments);
} else {
QString failure = QString("Unknown command: ") + m_commandName + "\n";
writeResponse(false, failure);
}
m_commandName = QString();
}

Command *Connection::createCommand(const char *name) {
#include "find_command.h"
return NULL;
}

void Connection::pendingLoadFinished(bool success) {
m_page->disconnect(this, SLOT(pendingLoadFinished(bool)));
if (success) {
startCommand();
} else {
QString response = m_page->failureString();
finishCommand(false, response);
}
}

Expand All @@ -111,3 +127,4 @@ void Connection::writeResponse(bool success, QString &response) {
m_socket->write(responseLength.toAscii());
m_socket->write(response.toAscii());
}

4 changes: 3 additions & 1 deletion src/Connection.h
Expand Up @@ -14,17 +14,19 @@ class Connection : public QObject {
public slots:
void checkNext();
void finishCommand(bool success, QString &response);
void pendingLoadFinished(bool success);

private:
void readLine();
void readDataBlock();
void processNext(const char *line);
Command *createCommand(const char *name);
void processArgument(const char *line);
void startCommand();
void continueCommand(const char *line);
void writeResponse(bool success, QString &response);

QTcpSocket *m_socket;
QString m_commandName;
Command *m_command;
QStringList m_arguments;
int m_argumentsExpected;
Expand Down
3 changes: 3 additions & 0 deletions src/Visit.cpp
Expand Up @@ -12,6 +12,9 @@ void Visit::start(QStringList &arguments) {

void Visit::loadFinished(bool success) {
QString response;
if (!success)
response = page()->failureString();

emit finished(success, response);
}

19 changes: 19 additions & 0 deletions src/WebPage.cpp
Expand Up @@ -11,6 +11,9 @@ WebPage::WebPage(QObject *parent) : QWebPage(parent) {
strcpy(javascriptString, (const char *)javascript.data());
javascriptString[javascript.size()] = 0;
m_capybaraJavascript = javascriptString;
m_loading = false;
connect(this, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
}

void WebPage::injectJavascriptHelpers() {
Expand Down Expand Up @@ -40,3 +43,19 @@ void WebPage::javaScriptConsoleMessage(const QString &message, int lineNumber, c
std::cout << qPrintable(message) << std::endl;
}

void WebPage::loadStarted() {
m_loading = true;
}

void WebPage::loadFinished(bool success) {
m_loading = false;
}

bool WebPage::isLoading() const {
return m_loading;
}

QString WebPage::failureString() {
return QString("Unable to load URL: ") + mainFrame()->url().toString();
}

5 changes: 5 additions & 0 deletions src/WebPage.h
Expand Up @@ -7,15 +7,20 @@ class WebPage : public QWebPage {
WebPage(QObject *parent = 0);
QVariant invokeCapybaraFunction(const char *name, QStringList &arguments);
QVariant invokeCapybaraFunction(QString &name, QStringList &arguments);
QString failureString();

public slots:
bool shouldInterruptJavaScript();
void injectJavascriptHelpers();
void loadStarted();
void loadFinished(bool);
bool isLoading() const;

protected:
virtual void javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID);

private:
QString m_capybaraJavascript;
bool m_loading;
};

0 comments on commit a8e64a5

Please sign in to comment.