Fetch attribute values

This commit is contained in:
Joe Ferris 2011-02-25 00:15:08 -05:00
parent 9f41e030f5
commit e0c4f6f57d
9 changed files with 70 additions and 16 deletions

View File

@ -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?

View File

@ -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)

View File

@ -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

View File

@ -7,7 +7,7 @@ describe Capybara::Driver::Webkit do
body = <<-HTML
<html><body>
<script type="text/javascript">
document.write("he" + "llo");
document.write("<p id='greeting'>he" + "llo</p>");
</script>
</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

23
src/Attribute.cpp Normal file
View File

@ -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);
}
}

16
src/Attribute.h Normal file
View File

@ -0,0 +1,16 @@
#include "Command.h"
#include <QStringList>
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;
};

View File

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

View File

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

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
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