Implemented Node#text; refactored Node commands so that they can be easily added in Javascript

This commit is contained in:
Joe Ferris 2011-02-25 23:39:29 -05:00
parent 3e32892e2b
commit 63366bf614
11 changed files with 37 additions and 22 deletions

View File

@ -1,11 +1,11 @@
class Capybara::Driver::Webkit
class Node < Capybara::Driver::Node
def text
raise NotImplementedError
invoke "text"
end
def [](name)
command "Attribute", name
invoke "attribute", name
end
def value
@ -48,8 +48,8 @@ class Capybara::Driver::Webkit
raise NotSupportedByDriverError
end
def command(name, *args)
browser.command name, native, *args
def invoke(name, *args)
browser.command "Node", name, native, *args
end
def browser

View File

@ -42,5 +42,9 @@ describe Capybara::Driver::Webkit do
it "parses xpath with quotes" do
subject.find('//*[contains(., "hello")]').should_not be_empty
end
it "returns a node's text" do
subject.find("//p").first.text.should == "hello"
end
end

View File

@ -1,12 +0,0 @@
#include "Attribute.h"
#include "WebPage.h"
Attribute::Attribute(WebPage *page, QObject *parent) : Command(page, parent) {
}
void Attribute::start(QStringList &arguments) {
QVariant result = page()->invokeCapybaraFunction("attribute", arguments);
QString attributeValue = result.toString();
emit finished(true, attributeValue);
}

View File

@ -3,7 +3,7 @@
#include "Find.h"
#include "Command.h"
#include "Reset.h"
#include "Attribute.h"
#include "Node.h"
#include <QTcpSocket>

14
src/Node.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "Node.h"
#include "WebPage.h"
Node::Node(WebPage *page, QObject *parent) : Command(page, parent) {
}
void Node::start(QStringList &arguments) {
QStringList functionArguments(arguments);
QString functionName = functionArguments.takeFirst();
QVariant result = page()->invokeCapybaraFunction(functionName, functionArguments);
QString attributeValue = result.toString();
emit finished(true, attributeValue);
}

View File

@ -3,11 +3,11 @@
class WebPage;
class Attribute : public Command {
class Node : public Command {
Q_OBJECT
public:
Attribute(WebPage *page, QObject *parent = 0);
Node(WebPage *page, QObject *parent = 0);
virtual void start(QStringList &arguments);
};

View File

@ -26,3 +26,7 @@ QVariant WebPage::invokeCapybaraFunction(const char *name, QStringList &argument
return mainFrame()->evaluateJavaScript(javascript);
}
QVariant WebPage::invokeCapybaraFunction(QString &name, QStringList &arguments) {
return invokeCapybaraFunction(name.toAscii().data(), arguments);
}

View File

@ -6,6 +6,7 @@ class WebPage : public QWebPage {
public:
WebPage(QObject *parent = 0);
QVariant invokeCapybaraFunction(const char *name, QStringList &arguments);
QVariant invokeCapybaraFunction(QString &name, QStringList &arguments);
public slots:
bool shouldInterruptJavaScript();

View File

@ -18,6 +18,10 @@ Capybara = {
return results.join(",");
},
text: function (index) {
return this.nodes[index].innerText;
},
attribute: function (index, name) {
return this.nodes[index].getAttribute(name);
}

View File

@ -6,4 +6,4 @@
CHECK_COMMAND(Visit)
CHECK_COMMAND(Find)
CHECK_COMMAND(Reset)
CHECK_COMMAND(Attribute)
CHECK_COMMAND(Node)

View File

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