Permalink
Browse files

Close new windows on reset

  • Loading branch information...
1 parent e7ff6d4 commit 126411337e60fe6b556e8f9ec018408022cb0bcc @mhoran mhoran committed May 15, 2012
Showing with 46 additions and 44 deletions.
  1. +15 −2 spec/driver_spec.rb
  2. +1 −1 src/Connection.cpp
  3. +7 −6 src/PageLoadingCommand.cpp
  4. +3 −3 src/PageLoadingCommand.h
  5. +1 −16 src/Reset.cpp
  6. +0 −3 src/Reset.h
  7. +5 −11 src/WebPage.cpp
  8. +0 −2 src/WebPage.h
  9. +11 −0 src/WebPageManager.cpp
  10. +3 −0 src/WebPageManager.h
View
@@ -211,9 +211,9 @@
subject.find("//*[contains(., 'hello')]").should be_empty
end
- it "has a location of 'about:blank' after reseting" do
+ it "has a blank location after reseting" do
subject.reset!
- subject.current_url.should == "about:blank"
+ subject.current_url.should == ""
end
it "raises an error for an invalid xpath query" do
@@ -1627,5 +1627,18 @@ def which_for(character)
expect { subject.within_window('myWindowDoesNotExist') }.
to raise_error(Capybara::Driver::Webkit::WebkitInvalidResponseError)
end
+
+ it "has a number of window handles equal to the number of open windows" do
+ subject.window_handles.size.should == 1
+ subject.visit("/new_window")
+ subject.window_handles.size.should == 2
+ end
+
+ it "closes new windows on reset" do
+ subject.visit("/new_window")
+ last_handle = subject.window_handles.last
+ subject.reset!
+ subject.window_handles.should_not include(last_handle)
+ end
end
end
View
@@ -32,7 +32,7 @@ void Connection::commandReady(Command *command) {
void Connection::startCommand() {
m_commandWaiting = false;
if (m_pageSuccess) {
- m_runningCommand = new PageLoadingCommand(m_queuedCommand, currentPage(), this);
+ m_runningCommand = new PageLoadingCommand(m_queuedCommand, m_manager, this);
connect(m_runningCommand, SIGNAL(finished(Response *)), this, SLOT(finishCommand(Response *)));
m_runningCommand->start();
} else {
@@ -1,15 +1,16 @@
#include "PageLoadingCommand.h"
#include "Command.h"
#include "WebPage.h"
+#include "WebPageManager.h"
-PageLoadingCommand::PageLoadingCommand(Command *command, WebPage *page, QObject *parent) : QObject(parent) {
- m_page = page;
+PageLoadingCommand::PageLoadingCommand(Command *command, WebPageManager *manager, QObject *parent) : QObject(parent) {
+ m_manager = manager;
m_command = command;
m_pageLoadingFromCommand = false;
m_pageSuccess = true;
m_pendingResponse = NULL;
- connect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
- connect(m_page, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
+ connect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
+ connect(m_manager, SIGNAL(pageFinished(bool)), this, SLOT(pendingLoadFinished(bool)));
}
void PageLoadingCommand::start() {
@@ -25,7 +26,7 @@ void PageLoadingCommand::pendingLoadFinished(bool success) {
if (m_pageSuccess) {
emit finished(m_pendingResponse);
} else {
- QString message = m_page->failureString();
+ QString message = m_manager->currentPage()->failureString();
emit finished(new Response(false, message));
}
}
@@ -37,7 +38,7 @@ void PageLoadingCommand::pageLoadingFromCommand() {
}
void PageLoadingCommand::commandFinished(Response *response) {
- disconnect(m_page, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
+ disconnect(m_manager, SIGNAL(loadStarted()), this, SLOT(pageLoadingFromCommand()));
m_command->deleteLater();
if (m_pageLoadingFromCommand)
m_pendingResponse = response;
@@ -3,7 +3,7 @@
class Command;
class Response;
-class WebPage;
+class WebPageManager;
/*
* Decorates a Command by deferring the finished() signal until any pending
@@ -19,7 +19,7 @@ class PageLoadingCommand : public QObject {
Q_OBJECT
public:
- PageLoadingCommand(Command *command, WebPage *page, QObject *parent = 0);
+ PageLoadingCommand(Command *command, WebPageManager *page, QObject *parent = 0);
void start();
public slots:
@@ -31,7 +31,7 @@ class PageLoadingCommand : public QObject {
void finished(Response *response);
private:
- WebPage *m_page;
+ WebPageManager *m_manager;
Command *m_command;
Response *m_pendingResponse;
bool m_pageSuccess;
View
@@ -1,30 +1,15 @@
#include "Reset.h"
#include "WebPage.h"
#include "WebPageManager.h"
-#include "NetworkAccessManager.h"
-#include "NetworkCookieJar.h"
Reset::Reset(WebPageManager *manager, QStringList &arguments, QObject *parent) : Command(manager, arguments, parent) {
}
void Reset::start() {
page()->triggerAction(QWebPage::Stop);
- NetworkAccessManager* networkAccessManager = qobject_cast<NetworkAccessManager*>(page()->networkAccessManager());
- networkAccessManager->setCookieJar(new NetworkCookieJar());
- networkAccessManager->resetHeaders();
+ manager()->reset();
- page()->setUserAgent(NULL);
- page()->resetResponseHeaders();
- page()->resetConsoleMessages();
- page()->resetWindowSize();
- resetHistory();
emit finished(new Response(true));
}
-void Reset::resetHistory() {
- // Clearing the history preserves the current history item, so set it to blank first.
- page()->currentFrame()->setUrl(QUrl("about:blank"));
- page()->history()->clear();
-}
-
View
@@ -6,8 +6,5 @@ class Reset : public Command {
public:
Reset(WebPageManager *, QStringList &arguments, QObject *parent = 0);
virtual void start();
-
- private:
- void resetHistory();
};
View
@@ -13,6 +13,7 @@ WebPage::WebPage(WebPageManager *manager, QObject *parent) : QWebPage(parent) {
m_loading = false;
m_manager = manager;
m_uuid = QUuid::createUuid().toString();
+ m_lastStatus = 0;
setForwardUnsupportedContent(true);
loadJavascript();
@@ -28,6 +29,8 @@ WebPage::WebPage(WebPageManager *manager, QObject *parent) : QWebPage(parent) {
this, SLOT(handleUnsupportedContent(QNetworkReply*)));
connect(this, SIGNAL(pageFinished(bool)),
m_manager, SLOT(emitPageFinished(bool)));
+ connect(this, SIGNAL(loadStarted()),
+ m_manager, SLOT(emitLoadStarted()));
resetWindowSize();
settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows, true);
@@ -39,8 +42,8 @@ void WebPage::resetWindowSize() {
}
void WebPage::setCustomNetworkAccessManager() {
- NetworkAccessManager *manager = new NetworkAccessManager();
- manager->setCookieJar(new NetworkCookieJar());
+ NetworkAccessManager *manager = new NetworkAccessManager(this);
+ manager->setCookieJar(new NetworkCookieJar(this));
this->setNetworkAccessManager(manager);
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
connect(manager, SIGNAL(sslErrors(QNetworkReply *, QList<QSslError>)),
@@ -229,15 +232,6 @@ int WebPage::getLastStatus() {
return m_lastStatus;
}
-void WebPage::resetResponseHeaders() {
- m_lastStatus = 0;
- m_pageHeaders = QString();
-}
-
-void WebPage::resetConsoleMessages() {
- m_consoleMessages.clear();
-}
-
QString WebPage::pageHeaders() {
return m_pageHeaders;
}
View
@@ -15,13 +15,11 @@ class WebPage : public QWebPage {
QString userAgentForUrl(const QUrl &url ) const;
void setUserAgent(QString userAgent);
int getLastStatus();
- void resetResponseHeaders();
void setCustomNetworkAccessManager();
bool render(const QString &fileName);
virtual bool extension (Extension extension, const ExtensionOption *option=0, ExtensionReturn *output=0);
void setSkipImageLoading(bool skip);
QString consoleMessages();
- void resetConsoleMessages();
void resetWindowSize();
QWebPage *createWindow(WebWindowType type);
QString uuid();
@@ -34,10 +34,21 @@ void WebPageManager::emitPageFinished(bool success) {
emit pageFinished(success);
}
+void WebPageManager::emitLoadStarted() {
+ if (currentPage() == sender())
+ emit loadStarted();
+}
+
void WebPageManager::setIgnoreSslErrors(bool value) {
m_ignoreSslErrors = value;
}
bool WebPageManager::ignoreSslErrors() {
return m_ignoreSslErrors;
}
+
+void WebPageManager::reset() {
+ m_pages.first()->deleteLater();
+ m_pages.clear();
+ createPage(this)->setFocus();
+}
@@ -17,12 +17,15 @@ class WebPageManager : public QObject {
WebPage *createPage(QObject *parent);
void setIgnoreSslErrors(bool);
bool ignoreSslErrors();
+ void reset();
public slots:
void emitPageFinished(bool);
+ void emitLoadStarted();
signals:
void pageFinished(bool);
+ void loadStarted();
private:
QList<WebPage *> m_pages;

0 comments on commit 1264113

Please sign in to comment.