diff --git a/lib/capybara/driver/webkit.rb b/lib/capybara/driver/webkit.rb index 553d733..be74c5d 100644 --- a/lib/capybara/driver/webkit.rb +++ b/lib/capybara/driver/webkit.rb @@ -6,6 +6,8 @@ class Capybara::Driver::Webkit class WebkitError < StandardError end + attr_reader :browser + def initialize(app, options={}) @app = app @options = options @@ -19,11 +21,11 @@ class Capybara::Driver::Webkit end def visit(path) - @browser.visit(url(path)) + browser.visit(url(path)) end def find(query) - @browser.find(query).map { |native| Node.new(self, native) } + browser.find(query).map { |native| Node.new(self, native) } end def source @@ -66,7 +68,7 @@ class Capybara::Driver::Webkit end def reset! - @browser.reset! + browser.reset! end def has_shortcircuit_timeout? diff --git a/lib/capybara/driver/webkit/browser.rb b/lib/capybara/driver/webkit/browser.rb index 0b9f4a7..1b75814 100644 --- a/lib/capybara/driver/webkit/browser.rb +++ b/lib/capybara/driver/webkit/browser.rb @@ -19,6 +19,14 @@ class Capybara::Driver::Webkit command("Reset") end + def command(name, *args) + puts ">> Sending #{name}" + @socket.puts name + args.each { |arg| @socket.puts arg } + check + read_response + end + private def start_server @@ -48,14 +56,6 @@ class Capybara::Driver::Webkit end end - def command(name, *args) - puts ">> Sending #{name}" - @socket.puts name - args.each { |arg| @socket.puts arg } - check - read_response - end - def read_response response_length = @socket.gets.to_i @socket.read(response_length) diff --git a/lib/capybara/driver/webkit/node.rb b/lib/capybara/driver/webkit/node.rb index 28ea410..cad890f 100644 --- a/lib/capybara/driver/webkit/node.rb +++ b/lib/capybara/driver/webkit/node.rb @@ -5,7 +5,7 @@ class Capybara::Driver::Webkit end def [](name) - raise NotImplementedError + command "Attribute", name end def value @@ -47,6 +47,14 @@ class Capybara::Driver::Webkit def trigger(event) raise NotSupportedByDriverError end + + def command(name, *args) + browser.command name, native, *args + end + + def browser + driver.browser + end end end diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index fa809e0..136ad9c 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -7,7 +7,7 @@ describe Capybara::Driver::Webkit do body = <<-HTML HTML @@ -34,5 +34,9 @@ describe Capybara::Driver::Webkit do expect { subject.find("totally invalid salad") }. to raise_error(Capybara::Driver::Webkit::WebkitError, /xpath/i) end + + it "returns an attribute's value" do + subject.find("//p").first["id"].should == "greeting" + end end diff --git a/src/Attribute.cpp b/src/Attribute.cpp new file mode 100644 index 0000000..7c4d1fe --- /dev/null +++ b/src/Attribute.cpp @@ -0,0 +1,23 @@ +#include "Attribute.h" +#include "WebPage.h" + +Attribute::Attribute(WebPage *page, QObject *parent) : Command(page, parent) { +} + +void Attribute::receivedArgument(const char *argument) { + m_args.append(argument); + if (m_args.length() == 2) { + QString nodeIndex = m_args[0]; + QString attributeName = m_args[1]; + QString javascript = QString("\ + (function () {\ + var node = window.__capybara_nodes[") + nodeIndex + "];\ + return node.getAttribute('" + attributeName + "');\ + })();\ + "; + QVariant result = page()->mainFrame()->evaluateJavaScript(javascript); + QString attributeValue = result.toString(); + emit finished(true, attributeValue); + } +} + diff --git a/src/Attribute.h b/src/Attribute.h new file mode 100644 index 0000000..e2525bc --- /dev/null +++ b/src/Attribute.h @@ -0,0 +1,16 @@ +#include "Command.h" +#include + +class WebPage; + +class Attribute : public Command { + Q_OBJECT + + public: + Attribute(WebPage *page, QObject *parent = 0); + virtual void receivedArgument(const char *argument); + + private: + QStringList m_args; +}; + diff --git a/src/Connection.cpp b/src/Connection.cpp index 69b7a08..05c9d6e 100644 --- a/src/Connection.cpp +++ b/src/Connection.cpp @@ -3,6 +3,7 @@ #include "Find.h" #include "Command.h" #include "Reset.h" +#include "Attribute.h" #include #include diff --git a/src/find_command.h b/src/find_command.h index 859d3f3..ade631c 100644 --- a/src/find_command.h +++ b/src/find_command.h @@ -6,4 +6,4 @@ CHECK_COMMAND(Visit) CHECK_COMMAND(Find) CHECK_COMMAND(Reset) - +CHECK_COMMAND(Attribute) \ No newline at end of file diff --git a/src/webkit_server.pro b/src/webkit_server.pro index eb21536..58c198c 100644 --- a/src/webkit_server.pro +++ b/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 -SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp +HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Attribute.h +SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Attribute.cpp QT += network webkit CONFIG += console staticlib