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
end end
def within_window(handle) def within_window(selector)
current_window = window_handle current_window = window_handle
browser.window_focus(handle) browser.window_focus(selector)
begin begin
yield yield
ensure ensure

View File

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

View File

@ -1558,7 +1558,7 @@ describe Capybara::Driver::Webkit do
body = <<-HTML body = <<-HTML
<html> <html>
<script type="text/javascript"> <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> </script>
<p>bananas</p> <p>bananas</p>
</html> </html>
@ -1566,7 +1566,7 @@ describe Capybara::Driver::Webkit do
else else
params = request.params params = request.params
sleep params['sleep'].to_i if params['sleep'] 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 end
[200, [200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s }, { '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.within_window(subject.window_handles.last) { }
subject.find("//p").first.text.should == "bananas" subject.find("//p").first.text.should == "bananas"
end 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
end end

View File

@ -258,3 +258,12 @@ QWebPage *WebPage::createWindow(WebWindowType type) {
QString WebPage::uuid() { QString WebPage::uuid() {
return m_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(); void resetWindowSize();
QWebPage *createWindow(WebWindowType type); QWebPage *createWindow(WebWindowType type);
QString uuid(); QString uuid();
QString getWindowName();
public slots: public slots:
bool shouldInterruptJavaScript(); bool shouldInterruptJavaScript();

View File

@ -26,7 +26,10 @@ void WindowFocus::focusWindow(QString selector) {
while (pageIterator.hasNext()) { while (pageIterator.hasNext()) {
WebPage *page = pageIterator.next(); 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); success(page);
return; return;
} }