Skip to content

Commit

Permalink
Support freezing the page with Chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Jun 13, 2019
1 parent 411f3bf commit b2afe0c
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ matrix:
addons:
homebrew:
taps: homebrew/cask-versions
casks: microsoft-edge-canary
casks: microsoft-edge-dev
before_install:
- wget https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver/76.0.168.0/edgedriver_mac64.zip
- unzip edgedriver_mac64.zip -d /usr/local/bin
Expand Down
8 changes: 8 additions & 0 deletions lib/capybara/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ def go_forward
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#go_forward'
end

def freeze_page
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#freeze_page'
end

def thaw_page
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#thaw_page'
end

def execute_script(script, *args)
raise Capybara::NotSupportedByDriverError, 'Capybara::Driver::Base#execute_script'
end
Expand Down
1 change: 1 addition & 0 deletions lib/capybara/selenium/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def browser
end
end
processed_options = options.reject { |key, _val| SPECIAL_OPTIONS.include?(key) }

@browser = Selenium::WebDriver.for(options[:browser], processed_options)

specialize_driver
Expand Down
9 changes: 9 additions & 0 deletions lib/capybara/selenium/driver_specializations/chrome_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ def reset!
execute_cdp('Storage.clearDataForOrigin', origin: '*', storageTypes: storage_types_to_clear)
end

def freeze_page
bridge.http.call(:post, "session/#{bridge.session_id}/goog/page/freeze", {})
end

def thaw_page
bridge.http.call(:post, "session/#{bridge.session_id}/goog/page/resume", {})
evaluate_script('1==1') # Ensure page has restarted
end

private

def storage_types_to_clear
Expand Down
2 changes: 2 additions & 0 deletions lib/capybara/spec/public/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ $(function() {
});
$('#clickable').click(function(e) {
var link = $(this);
$(link).after('<a id="clickable-processing" href="#">Processing<a>');
setTimeout(function() {
$(link).after('<a id="has-been-clicked" href="#">Has been clicked</a>');
$(link).after('<input type="submit" value="New Here">');
$(link).after('<input type="text" id="new_field">');
$('#change').remove();
$('#clickable-processing').remove();
}, 1000);
return false;
});
Expand Down
30 changes: 30 additions & 0 deletions lib/capybara/spec/session/driver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

Capybara::SpecHelper.spec 'Driver' do
context 'freeze_page', requires: %i[freeze js] do
it 'can pause a page' do
@session.visit('/with_js')
@session.find(:css, '#clickable').click
sleep 0.1
@session.driver.freeze_page

expect(@session).to have_css('#clickable-processing')
sleep 3 # Time needs to be longer than click action delay
expect(@session).not_to have_css('#has-been-clicked')
expect(@session).to have_css('#clickable-processing')

@session.driver.thaw_page
expect(@session).to have_css('#has-been-clicked').and(have_no_css('#clickable-processing'))
end

it "doesn't prevent driver JS" do
@session.visit('/with_js')
@session.find(:css, '#clickable')
@session.driver.freeze_page

expect(@session.evaluate_script('1==1')).to eq true

@session.driver.thaw_page
end
end
end
2 changes: 1 addition & 1 deletion spec/selenium_spec_firefox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module TestSessions
SeleniumFirefox = Capybara::Session.new(:selenium_firefox, TestApp)
end

skipped_tests = %i[response_headers status_code trigger]
skipped_tests = %i[response_headers status_code trigger freeze]

Capybara::SpecHelper.log_selenium_driver_version(Selenium::WebDriver::Firefox) if ENV['CI']

Expand Down
2 changes: 1 addition & 1 deletion spec/selenium_spec_firefox_remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module TestSessions
str if File.exist?(str)
end

skipped_tests = %i[response_headers status_code trigger download]
skipped_tests = %i[response_headers status_code trigger download freeze]

Capybara::SpecHelper.run_specs TestSessions::RemoteFirefox, FIREFOX_REMOTE_DRIVER.to_s, capybara_skip: skipped_tests do |example|
case example.metadata[:full_description]
Expand Down
2 changes: 1 addition & 1 deletion spec/selenium_spec_safari.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module TestSessions
Safari = Capybara::Session.new(SAFARI_DRIVER, TestApp)
end

skipped_tests = %i[response_headers status_code trigger windows drag]
skipped_tests = %i[response_headers status_code trigger windows drag freeze]

Capybara::SpecHelper.log_selenium_driver_version(Selenium::WebDriver::Safari) if ENV['CI']

Expand Down

0 comments on commit b2afe0c

Please sign in to comment.