Skip to content
Browse files

Implemented Node#text; refactored Node commands so that they can be e…

…asily added in Javascript
  • Loading branch information...
1 parent 3e32892 commit 63366bf61485ab26cdbe3c6bdd6275b0144f8f02 @jferris jferris committed Feb 25, 2011
View
8 lib/capybara/driver/webkit/node.rb
@@ -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 @@ def trigger(event)
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
4 spec/driver_spec.rb
@@ -42,5 +42,9 @@
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
12 src/Attribute.cpp
@@ -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
2 src/Connection.cpp
@@ -3,7 +3,7 @@
#include "Find.h"
#include "Command.h"
#include "Reset.h"
-#include "Attribute.h"
+#include "Node.h"
#include <QTcpSocket>
View
14 src/Node.cpp
@@ -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
4 src/Attribute.h → src/Node.h
@@ -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
4 src/WebPage.cpp
@@ -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
1 src/WebPage.h
@@ -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
4 src/capybara.js
@@ -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
2 src/find_command.h
@@ -6,4 +6,4 @@
CHECK_COMMAND(Visit)
CHECK_COMMAND(Find)
CHECK_COMMAND(Reset)
-CHECK_COMMAND(Attribute)
+CHECK_COMMAND(Node)
View
4 src/webkit_server.pro
@@ -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

0 comments on commit 63366bf

Please sign in to comment.
Something went wrong with that request. Please try again.