From 1eff84017e58ace4750688c952965a95b887d69a Mon Sep 17 00:00:00 2001 From: Steve Day Date: Mon, 10 Sep 2018 10:18:22 +0100 Subject: [PATCH] Swap driver.execute_script for async version in Chrome execute_script runs synchronously, whereas execute_async_script provides a callback argument to the script. This makes the before capture scripts for Chrome more like the ones for Phantom and Casper and allows users to do more advanced things in their before_captures. --- lib/wraith/save_images.rb | 4 ++-- spec/js/global--chrome.js | 2 ++ spec/js/path--chrome.js | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/wraith/save_images.rb b/lib/wraith/save_images.rb index 151e3c6f..b697be06 100644 --- a/lib/wraith/save_images.rb +++ b/lib/wraith/save_images.rb @@ -136,8 +136,8 @@ def capture_image_selenium(screen_sizes, url, file_name, selector, global_before driver.manage.window.resize_to(width, height || 1500) driver.navigate.to url driver.manage.timeouts.implicit_wait = wraith.settle - driver.execute_script(File.read(global_before_capture)) if global_before_capture - driver.execute_script(File.read(path_before_capture)) if path_before_capture + driver.execute_async_script(File.read(global_before_capture)) if global_before_capture + driver.execute_async_script(File.read(path_before_capture)) if path_before_capture resize_to_fit_page(driver) unless height driver.save_screenshot(new_file_name) crop_selector(driver, selector, new_file_name) if selector && selector.length > 0 diff --git a/spec/js/global--chrome.js b/spec/js/global--chrome.js index 63cc23f0..fb1d9f3f 100644 --- a/spec/js/global--chrome.js +++ b/spec/js/global--chrome.js @@ -1,4 +1,6 @@ +var callback = arguments[arguments.length-1]; (function () { document.body.innerHTML = " "; document.body.style['background-color'] = 'red'; + callback(); })(); diff --git a/spec/js/path--chrome.js b/spec/js/path--chrome.js index 2d3ca011..052217bf 100644 --- a/spec/js/path--chrome.js +++ b/spec/js/path--chrome.js @@ -1,4 +1,6 @@ +var callback = arguments[arguments.length-1]; (function () { document.body.innerHTML = " "; document.body.style['background-color'] = 'green'; + callback(); })();