First pass at within_window support

This commit is contained in:
Matthew Horan 2012-03-26 18:41:33 -04:00
parent 099386d5a7
commit efa577c3f0
15 changed files with 154 additions and 1 deletions

View File

@ -93,7 +93,12 @@ class Capybara::Driver::Webkit
end
def within_window(handle)
raise Capybara::NotSupportedByDriverError
browser.window_focus(handle)
begin
yield
ensure
browser.window_focus
end
end
def wait?

View File

@ -81,6 +81,10 @@ class Capybara::Driver::Webkit
command("SetSkipImageLoading", skip_image_loading)
end
def window_focus(handle=nil)
command("WindowFocus")
end
def command(name, *args)
@connection.puts name
@connection.puts args.size

View File

@ -1549,4 +1549,32 @@ describe Capybara::Driver::Webkit do
subject.source.should == "Hello\0World"
end
end
context "javascript new window app" do
before(:all) do
@app = lambda do |env|
if env['PATH_INFO'] == '/new_window'
body = <<-HTML
<html>
<script type="text/javascript">
window.open('http://#{env['HTTP_HOST']}/test');
</script>
</html>
HTML
else
body = "<html><p>finished</p></html>"
end
[200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
[body]]
end
end
it "has the expected text in the new window" do
subject.visit("/new_window")
subject.within_window(nil) do
subject.find("//p").first.text.should == "finished"
end
end
end
end

View File

@ -25,6 +25,7 @@
#include "ResizeWindow.h"
#include "IgnoreSslErrors.h"
#include "SetSkipImageLoading.h"
#include "WindowFocus.h"
CommandFactory::CommandFactory(WebPage *page, QObject *parent) : QObject(parent) {
m_page = page;
@ -36,3 +37,7 @@ Command *CommandFactory::createCommand(const char *name, QStringList &arguments)
arguments.append(QString(name));
return new NullCommand(m_page, arguments);
}
void CommandFactory::changeWindow(WebPage *newPage) {
m_page = newPage;
}

View File

@ -10,6 +10,9 @@ class CommandFactory : public QObject {
CommandFactory(WebPage *page, QObject *parent = 0);
Command *createCommand(const char *name, QStringList &arguments);
public slots:
void changeWindow(WebPage *);
private:
WebPage *m_page;
};

View File

@ -33,6 +33,8 @@ void Connection::startCommand() {
if (m_pageSuccess) {
m_runningCommand = new PageLoadingCommand(m_queuedCommand, m_page, this);
connect(m_runningCommand, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *)));
connect(m_queuedCommand, SIGNAL(windowChanged(WebPage *)), this, SLOT(changeWindow(WebPage *)));
connect(m_queuedCommand, SIGNAL(windowChanged(WebPage *)), m_commandFactory, SLOT(changeWindow(WebPage *)));
m_runningCommand->start();
} else {
writePageLoadFailure();
@ -69,3 +71,7 @@ void Connection::writeResponse(Response *response) {
delete response;
}
void Connection::changeWindow(WebPage *newPage) {
m_page = newPage;
}

View File

@ -19,6 +19,7 @@ class Connection : public QObject {
void commandReady(Command *command);
void finishCommand(Response *response);
void pendingLoadFinished(bool success);
void changeWindow(WebPage *);
private:
void startCommand();

View File

@ -1,4 +1,5 @@
#include "WebPage.h"
#include "WebPageManager.h"
#include "JavascriptInvocation.h"
#include "NetworkAccessManager.h"
#include "NetworkCookieJar.h"
@ -23,6 +24,9 @@ WebPage::WebPage(QObject *parent) : QWebPage(parent) {
connect(this, SIGNAL(unsupportedContent(QNetworkReply*)),
this, SLOT(handleUnsupportedContent(QNetworkReply*)));
resetWindowSize();
settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
WebPageManager::getInstance()->append(this);
}
void WebPage::resetWindowSize() {
@ -242,3 +246,8 @@ void WebPage::handleUnsupportedContent(QNetworkReply *reply) {
UnsupportedContentHandler *handler = new UnsupportedContentHandler(this, reply);
Q_UNUSED(handler);
}
QWebPage *WebPage::createWindow(WebWindowType type) {
Q_UNUSED(type);
return new WebPage(this);
}

View File

@ -1,3 +1,5 @@
#ifndef _WEBPAGE_H
#define _WEBPAGE_H
#include <QtWebKit>
class WebPage : public QWebPage {
@ -20,6 +22,7 @@ class WebPage : public QWebPage {
QString consoleMessages();
void resetConsoleMessages();
void resetWindowSize();
QWebPage *createWindow(WebWindowType type);
public slots:
bool shouldInterruptJavaScript();
@ -56,3 +59,5 @@ class WebPage : public QWebPage {
QStringList m_consoleMessages;
};
#endif //_WEBPAGE_H

23
src/WebPageManager.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "WebPageManager.h"
WebPageManager *WebPageManager::m_instance = NULL;
WebPageManager::WebPageManager() {
webPages = new QList<WebPage*>();
}
WebPageManager *WebPageManager::getInstance() {
if(!m_instance)
m_instance = new WebPageManager();
return m_instance;
}
void WebPageManager::append(WebPage *value) {
webPages->append(value);
}
WebPage *WebPageManager::last() {
return (WebPage*) webPages->last();
}

15
src/WebPageManager.h Normal file
View File

@ -0,0 +1,15 @@
#include "WebPage.h"
#include <QList>
class WebPageManager {
public:
static WebPageManager *getInstance();
void append(WebPage *value);
WebPage *last();
private:
WebPageManager();
QList<WebPage*> *webPages;
static WebPageManager *m_instance;
};

25
src/WindowFocus.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "WindowFocus.h"
#include "Command.h"
#include "WebPage.h"
#include "WebPageManager.h"
WindowFocus::WindowFocus(WebPage *page, QStringList &arguments, QObject *parent) : Command(page, arguments, parent) {
}
void WindowFocus::start() {
WebPage *webPage = WebPageManager::getInstance()->last();
if (webPage) {
emit windowChanged(webPage);
success();
} else {
windowNotFound();
}
}
void WindowFocus::windowNotFound() {
emit finished(new Response(false, QString("Unable to locate window. ")));
}
void WindowFocus::success() {
emit finished(new Response(true));
}

19
src/WindowFocus.h Normal file
View File

@ -0,0 +1,19 @@
#include "Command.h"
class WebPage;
class WindowFocus : public Command {
Q_OBJECT
public:
WindowFocus(WebPage *page, QStringList &arguments, QObject *parent = 0);
virtual void start();
signals:
void windowChanged(WebPage *);
private:
void success();
void windowNotFound();
};

View File

@ -28,3 +28,4 @@ CHECK_COMMAND(CurrentUrl)
CHECK_COMMAND(ResizeWindow)
CHECK_COMMAND(IgnoreSslErrors)
CHECK_COMMAND(SetSkipImageLoading)
CHECK_COMMAND(WindowFocus)

View File

@ -39,6 +39,8 @@ HEADERS = \
NullCommand.h \
PageLoadingCommand.h \
SetSkipImageLoading.h \
WebPageManager.h \
WindowFocus.h \
SOURCES = \
IgnoreSslErrors.cpp \
@ -79,6 +81,8 @@ SOURCES = \
NullCommand.cpp \
PageLoadingCommand.cpp \
SetSkipImageLoading.cpp \
WebPageManager.cpp \
WindowFocus.cpp \
RESOURCES = webkit_server.qrc
QT += network webkit