Implemented Driver#execute_script

This commit is contained in:
Joe Ferris 2011-02-26 14:03:30 -05:00
parent cfb5babae5
commit 3f519cd147
10 changed files with 59 additions and 11 deletions

View File

@ -37,7 +37,7 @@ class Capybara::Driver::Webkit
end end
def execute_script(script) def execute_script(script)
raise Capybara::NotSupportedByDriverError browser.execute_script script
end end
def evaluate_script(script) def evaluate_script(script)

View File

@ -45,6 +45,10 @@ class Capybara::Driver::Webkit
JSON.parse("[#{json}]").first JSON.parse("[#{json}]").first
end end
def execute_script(script)
command('Execute', script)
end
private private
def start_server def start_server

View File

@ -115,5 +115,15 @@ describe Capybara::Driver::Webkit do
result = subject.evaluate_script("[1,\n2]") result = subject.evaluate_script("[1,\n2]")
result.should == [1, 2] result.should == [1, 2]
end end
it "executes Javascript" do
subject.execute_script(%<document.getElementById('greeting').innerHTML = 'yo'>)
subject.find("//p[contains(., 'yo')]").should_not be_empty
end
it "raises an error for failing Javascript" do
expect { subject.execute_script(%<invalid salad>) }.
to raise_error(Capybara::Driver::Webkit::WebkitError)
end
end end

View File

@ -7,6 +7,7 @@
#include "Url.h" #include "Url.h"
#include "Source.h" #include "Source.h"
#include "Evaluate.h" #include "Evaluate.h"
#include "Execute.h"
#include <QTcpSocket> #include <QTcpSocket>
#include <iostream> #include <iostream>
@ -60,7 +61,8 @@ void Connection::processNext(const char *data) {
if (m_command) { if (m_command) {
startCommand(); startCommand();
} else { } else {
m_socket->write("bad command\n"); QString failure = QString("Unknown command: ") + data + "\n";
writeResponse(false, failure);
} }
} }
} }
@ -96,13 +98,16 @@ void Connection::finishCommand(bool success, QString &response) {
m_command->deleteLater(); m_command->deleteLater();
m_command = NULL; m_command = NULL;
m_arguments.clear(); m_arguments.clear();
if (success) { writeResponse(success, response);
}
void Connection::writeResponse(bool success, QString &response) {
if (success)
m_socket->write("ok\n"); m_socket->write("ok\n");
} else { else
m_socket->write("failure\n"); m_socket->write("failure\n");
}
QString responseLength = QString::number(response.size()) + "\n"; QString responseLength = QString::number(response.size()) + "\n";
m_socket->write(responseLength.toAscii()); m_socket->write(responseLength.toAscii());
m_socket->write(response.toAscii()); m_socket->write(response.toAscii());
} }

View File

@ -22,6 +22,7 @@ class Connection : public QObject {
Command *createCommand(const char *name); Command *createCommand(const char *name);
void startCommand(); void startCommand();
void continueCommand(const char *line); void continueCommand(const char *line);
void writeResponse(bool success, QString &response);
QTcpSocket *m_socket; QTcpSocket *m_socket;
Command *m_command; Command *m_command;

View File

@ -7,8 +7,6 @@ Evaluate::Evaluate(WebPage *page, QObject *parent) : Command(page, parent) {
} }
void Evaluate::start(QStringList &arguments) { void Evaluate::start(QStringList &arguments) {
Q_UNUSED(arguments);
QVariant result = page()->mainFrame()->evaluateJavaScript(arguments[0]); QVariant result = page()->mainFrame()->evaluateJavaScript(arguments[0]);
addVariant(result); addVariant(result);
emit finished(true, m_buffer); emit finished(true, m_buffer);

17
src/Execute.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "Execute.h"
#include "WebPage.h"
Execute::Execute(WebPage *page, QObject *parent) : Command(page, parent) {
}
void Execute::start(QStringList &arguments) {
QVariant result = page()->mainFrame()->evaluateJavaScript(arguments[0]);
QString response;
if (result.isValid()) {
emit finished(true, response);
} else {
response = "Javascript failed to execute";
emit finished(false, response);
}
}

12
src/Execute.h Normal file
View File

@ -0,0 +1,12 @@
#include "Command.h"
class WebPage;
class Execute : public Command {
Q_OBJECT
public:
Execute(WebPage *page, QObject *parent = 0);
virtual void start(QStringList &arguments);
};

View File

@ -10,3 +10,4 @@ CHECK_COMMAND(Node)
CHECK_COMMAND(Url) CHECK_COMMAND(Url)
CHECK_COMMAND(Source) CHECK_COMMAND(Source)
CHECK_COMMAND(Evaluate) CHECK_COMMAND(Evaluate)
CHECK_COMMAND(Execute)

View File

@ -1,8 +1,8 @@
TEMPLATE = app TEMPLATE = app
TARGET = webkit_server TARGET = webkit_server
DESTDIR = . DESTDIR = .
HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Node.h JavascriptInvocation.h Url.h Source.h Evaluate.h Execute.h
SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Node.cpp JavascriptInvocation.cpp Url.cpp Source.cpp Evaluate.cpp Execute.cpp
RESOURCES = webkit_server.qrc RESOURCES = webkit_server.qrc
QT += network webkit QT += network webkit
CONFIG += console CONFIG += console