`within_frame` now works. Takes either an index or a frame name/id.

This commit is contained in:
Aaron Gibralter 2011-04-20 01:42:20 -04:00 committed by Joe Ferris
parent 182cb2e5b7
commit c563ed54cb
4 changed files with 78 additions and 21 deletions

View File

@ -52,10 +52,13 @@ class Capybara::Driver::Webkit
raise Capybara::NotSupportedByDriverError
end
def within_frame(frame_id)
browser.frame_focus_id(frame_id)
yield
browser.frame_focus_parent
def within_frame(frame_id_or_index)
browser.frame_focus(frame_id_or_index)
begin
yield
ensure
browser.frame_focus
end
end
def within_window(handle)

View File

@ -30,12 +30,14 @@ class Capybara::Driver::Webkit
command("Url")
end
def frame_focus_id(frame_id)
command("FrameFocus", frame_id)
end
def frame_focus_parent
command("FrameFocus")
def frame_focus(frame_id_or_index=nil)
if frame_id_or_index.is_a? Fixnum
command("FrameFocus", "", frame_id_or_index.to_s)
elsif frame_id_or_index
command("FrameFocus", frame_id_or_index)
else
command("FrameFocus")
end
end
def command(name, *args)

View File

@ -10,15 +10,16 @@ describe Capybara::Driver::Webkit do
before(:all) do
@app = lambda do |env|
params = ::Rack::Utils.parse_query(env['QUERY_STRING'])
iframe = unless params["iframe"]
"<iframe id=\"f\" src=\"/hello/world?iframe=true\"></iframe>"
end
if iframe
if params["iframe"] == "true"
# We are in an iframe request.
p_id = "farewell"
msg = "goodbye"
iframe = nil
else
# We are not in an iframe request and need to make an iframe!
p_id = "greeting"
msg = "hello"
iframe = "<iframe id=\"f\" src=\"/?iframe=true\"></iframe>"
end
body = <<-HTML
<html>
@ -28,9 +29,7 @@ describe Capybara::Driver::Webkit do
</style>
</head>
<body>
<div id="display_none">
<div id="invisible">Can't see me</div>
</div>
#{iframe}
<script type="text/javascript">
document.write("<p id='#{p_id}'>#{msg}</p>");
</script>
@ -43,12 +42,28 @@ describe Capybara::Driver::Webkit do
end
end
it "finds content after loading a URL" do
it "finds frames by index" do
subject.within_frame(0) do
subject.find("//*[contains(., 'goodbye')]").should_not be_empty
end
end
it "finds frames by id" do
subject.within_frame("f") do
subject.find("//*[contains(., 'goodbye')]").should_not be_empty
end
end
it "raises error for missing frame by index" do
expect { subject.within_frame(1) { } }.
to raise_error(Capybara::Driver::Webkit::WebkitError)
end
it "raise_error for missing frame by id" do
expect { subject.within_frame("foo") { } }.
to raise_error(Capybara::Driver::Webkit::WebkitError)
end
it "returns an attribute's value" do
subject.within_frame("f") do
subject.find("//p").first["id"].should == "farewell"
@ -64,7 +79,7 @@ describe Capybara::Driver::Webkit do
it "returns the current URL" do
subject.within_frame("f") do
port = subject.instance_variable_get("@rack_server").port
subject.current_url.should == "http://127.0.0.1:#{port}/hello/world?iframe=true"
subject.current_url.should == "http://127.0.0.1:#{port}/?iframe=true"
end
end
@ -77,7 +92,7 @@ describe Capybara::Driver::Webkit do
it "evaluates Javascript" do
subject.within_frame("f") do
result = subject.evaluate_script(%<document.getElementById('farewell').innerText>)
result.should == "hello"
result.should == "goodbye"
end
end

View File

@ -6,7 +6,44 @@ FrameFocus::FrameFocus(WebPage *page, QObject *parent) : Command(page, parent) {
}
void FrameFocus::start(QStringList &arguments) {
int index;
bool ok;
bool found = false;
QString response;
emit finished(true, response);
QList<QWebFrame *> child_frames = page()->currentFrame()->childFrames();
if (arguments.length() > 0) {
if (arguments.length() > 1) {
// Find frame by index.
index = arguments[1].toInt(&ok);
if (ok && 0 <= index && index < child_frames.length()) {
child_frames[index]->setFocus();
found = true;
}
} else {
// Find frame by id.
for (int i = 0; i < child_frames.length(); i++) {
if (child_frames[i]->frameName().compare(arguments[0]) == 0) {
child_frames[i]->setFocus();
found = true;
break;
}
}
}
if (found) {
emit finished(true, response);
} else {
response = "Unable to locate frame. ";
emit finished(false, response);
}
} else {
// Set focus on parent.
if (page()->currentFrame()->parentFrame() == 0) {
response = "Already at parent frame.";
emit finished(false, response);
} else {
page()->currentFrame()->parentFrame()->setFocus();
emit finished(true, response);
}
}
}