Find window by name, title or URL

This commit is contained in:
Matthew Horan 2012-03-28 11:46:07 -04:00
parent fe46eaf50d
commit aaba4d1245
6 changed files with 46 additions and 7 deletions

View File

@ -92,9 +92,9 @@ class Capybara::Driver::Webkit
end
end
def within_window(handle)
def within_window(selector)
current_window = window_handle
browser.window_focus(handle)
browser.window_focus(selector)
begin
yield
ensure

View File

@ -81,8 +81,8 @@ class Capybara::Driver::Webkit
command("SetSkipImageLoading", skip_image_loading)
end
def window_focus(handle)
command("WindowFocus", handle)
def window_focus(selector)
command("WindowFocus", selector)
end
def get_window_handles

View File

@ -1558,7 +1558,7 @@ describe Capybara::Driver::Webkit do
body = <<-HTML
<html>
<script type="text/javascript">
window.open('http://#{request.host_with_port}/test?#{request.query_string}');
window.open('http://#{request.host_with_port}/test?#{request.query_string}', 'myWindow');
</script>
<p>bananas</p>
</html>
@ -1566,7 +1566,7 @@ describe Capybara::Driver::Webkit do
else
params = request.params
sleep params['sleep'].to_i if params['sleep']
body = "<html><p>finished</p></html>"
body = "<html><head><title>My New Window</title></head><body><p>finished</p></body></html>"
end
[200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
@ -1593,5 +1593,31 @@ describe Capybara::Driver::Webkit do
subject.within_window(subject.window_handles.last) { }
subject.find("//p").first.text.should == "bananas"
end
it "supports finding a window by name" do
subject.visit("/new_window")
subject.within_window('myWindow') do
subject.find("//p").first.text.should == "finished"
end
end
it "supports finding a window by title" do
subject.visit("/new_window")
subject.within_window('My New Window') do
subject.find("//p").first.text.should == "finished"
end
end
it "supports finding a window by url" do
subject.visit("/new_window")
subject.within_window("http://127.0.0.1:#{subject.server_port}/test?") do
subject.find("//p").first.text.should == "finished"
end
end
it "raises an error if the window is not found" do
expect { subject.within_window('myWindowDoesNotExist') }.
to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError)
end
end
end

View File

@ -258,3 +258,12 @@ QWebPage *WebPage::createWindow(WebWindowType type) {
QString WebPage::uuid() {
return m_uuid;
}
QString WebPage::getWindowName() {
QVariant windowName = mainFrame()->evaluateJavaScript("window.name");
if (windowName.isValid())
return windowName.toString();
else
return "";
}

View File

@ -24,6 +24,7 @@ class WebPage : public QWebPage {
void resetWindowSize();
QWebPage *createWindow(WebWindowType type);
QString uuid();
QString getWindowName();
public slots:
bool shouldInterruptJavaScript();

View File

@ -26,7 +26,10 @@ void WindowFocus::focusWindow(QString selector) {
while (pageIterator.hasNext()) {
WebPage *page = pageIterator.next();
if (selector == page->uuid()) {
if (selector == page->getWindowName() ||
selector == page->mainFrame()->title() ||
selector == page->mainFrame()->url().toString() ||
selector == page->uuid()) {
success(page);
return;
}