Return to the first window after yield

This commit is contained in:
Matthew Horan 2012-03-27 13:14:01 -04:00
parent 7149ec7a39
commit 468bec886f
5 changed files with 29 additions and 9 deletions

View File

@ -82,7 +82,11 @@ class Capybara::Driver::Webkit
end end
def window_focus(handle=nil) def window_focus(handle=nil)
command("WindowFocus") if handle
command("WindowFocus", handle)
else
command("WindowFocus")
end
end end
def command(name, *args) def command(name, *args)

View File

@ -1560,6 +1560,7 @@ describe Capybara::Driver::Webkit do
<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}');
</script> </script>
<p>bananas</p>
</html> </html>
HTML HTML
else else
@ -1575,16 +1576,22 @@ describe Capybara::Driver::Webkit do
it "has the expected text in the new window" do it "has the expected text in the new window" do
subject.visit("/new_window") subject.visit("/new_window")
subject.within_window(nil) do subject.within_window(true) do
subject.find("//p").first.text.should == "finished" subject.find("//p").first.text.should == "finished"
end end
end end
it "waits for the new window to load" do it "waits for the new window to load" do
subject.visit("/new_window?sleep=1") subject.visit("/new_window?sleep=1")
subject.within_window(nil) do subject.within_window(true) do
lambda { Timeout::timeout(1) { subject.find("//p") } }.should_not raise_error(Timeout::Error) lambda { Timeout::timeout(1) { subject.find("//p") } }.should_not raise_error(Timeout::Error)
end end
end end
it "switches back to the original window" do
subject.visit("/new_window")
subject.within_window(true) { }
subject.find("//p").first.text.should == "bananas"
end
end end
end end

View File

@ -17,6 +17,10 @@ void WebPageManager::append(WebPage *value) {
webPages->append(value); webPages->append(value);
} }
WebPage *WebPageManager::first() {
return (WebPage*) webPages->first();
}
WebPage *WebPageManager::last() { WebPage *WebPageManager::last() {
return (WebPage*) webPages->last(); return (WebPage*) webPages->last();
} }

View File

@ -5,6 +5,7 @@ class WebPageManager {
public: public:
static WebPageManager *getInstance(); static WebPageManager *getInstance();
void append(WebPage *value); void append(WebPage *value);
WebPage *first();
WebPage *last(); WebPage *last();
private: private:

View File

@ -7,12 +7,16 @@ WindowFocus::WindowFocus(WebPage *page, QStringList &arguments, QObject *parent)
} }
void WindowFocus::start() { void WindowFocus::start() {
WebPage *webPage = WebPageManager::getInstance()->last(); WebPageManager *manager = WebPageManager::getInstance();
if (webPage) {
emit windowChanged(webPage); switch(arguments().length()) {
success(); case 1:
} else { emit windowChanged(manager->last());
windowNotFound(); success();
break;
default:
emit windowChanged(manager->first());
success();
} }
} }