diff --git a/README.md b/README.md index 6bdeb8b..d488ec9 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,13 @@ If you're using capybara-webkit with Sinatra, don't forget to set Capybara.app = MySinatraApp.new ``` +Offline Application Cache +------------------------- + +The offline application cache needs a directory to write to for the cached files. Capybara-webkit +will look at if the working directory has a tmp directory and when it exists offline application +cache will be enabled. + Non-Standard Driver Methods --------------------------- diff --git a/spec/driver_spec.rb b/spec/driver_spec.rb index 879c0c4..721f617 100644 --- a/spec/driver_spec.rb +++ b/spec/driver_spec.rb @@ -2025,6 +2025,75 @@ describe Capybara::Webkit::Driver do end end + context "offline application cache" do + let(:driver) do + @visited = [] + visited = @visited + + driver_for_app do + get '/8d853f09-4275-409d-954d-ebbf6e2ce732' do + content_type 'text/cache-manifest' + visited << 'manifest' + <<-TEXT +CACHE MANIFEST +/4aaffa31-f42d-403e-a19e-6b248d608087 + TEXT + end + + # UUID urls so that this gets isolated from other tests + get '/f8742c39-8bef-4196-b1c3-80f8a3d65f3e' do + visited << 'complex' + <<-HTML + + + + + + + + HTML + end + + get '/4aaffa31-f42d-403e-a19e-6b248d608087' do + visited << 'simple' + <<-HTML + + + + + HTML + end + end + end + + before { visit("/f8742c39-8bef-4196-b1c3-80f8a3d65f3e") } + + it "has proper state available" do + driver.find_xpath("//*[@id='state']").first.visible_text.should == '0' + sleep 1 + @visited.should eq(['complex', 'manifest', 'simple']), 'files were not downloaded in expected order' + driver.find_xpath("//*[@id='finished']").first.visible_text.should == 'cached' + end + + it "is cleared on driver reset!" do + sleep 1 + @visited.should eq(['complex', 'manifest', 'simple']), 'files were not downloaded in expected order' + driver.reset! + @visited.clear + visit '/4aaffa31-f42d-403e-a19e-6b248d608087' + sleep 1 + @visited.should eq(['simple', 'manifest', 'simple']), 'simple action was used from cache instead of server' + end + end + context "form app with server-side handler" do let(:driver) do driver_for_app do diff --git a/src/WebPage.cpp b/src/WebPage.cpp index 4850053..163447b 100644 --- a/src/WebPage.cpp +++ b/src/WebPage.cpp @@ -40,6 +40,11 @@ WebPage::WebPage(WebPageManager *manager, QObject *parent) : QWebPage(parent) { settings()->setAttribute(QWebSettings::JavascriptCanCloseWindows, true); settings()->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, true); + if(QFileInfo("tmp").isDir()) { + settings()->setAttribute(QWebSettings::OfflineWebApplicationCacheEnabled, true); + settings()->setOfflineWebApplicationCachePath("tmp"); + } + createWindow(); } diff --git a/src/WebPageManager.cpp b/src/WebPageManager.cpp index 54779af..4851521 100644 --- a/src/WebPageManager.cpp +++ b/src/WebPageManager.cpp @@ -124,6 +124,12 @@ void WebPageManager::reset() { WebPage *page = m_pages.takeFirst(); page->deleteLater(); } + + qint64 size = QWebSettings::offlineWebApplicationCacheQuota(); + // No public function was found to wrap the empty() call to + // WebCore::cacheStorage().empty() + QWebSettings::setOfflineWebApplicationCacheQuota(size); + createPage()->setFocus(); }