cuprite icon indicating copy to clipboard operation
cuprite copied to clipboard

Javascript errors triggered by cuprite on pages with iframes

Open hoffi opened this issue 3 years ago • 1 comments

On pages with iframes cuprite triggers a javascript error when switching into a frame.

I have written a test which shows the errors:

diff --git a/spec/features/driver_spec.rb b/spec/features/driver_spec.rb
index d9f9403..d4c3d7d 100644
--- a/spec/features/driver_spec.rb
+++ b/spec/features/driver_spec.rb
@@ -1555,6 +1555,16 @@ module Capybara
         expect(@session.driver.current_url).to end_with("/cuprite/frames")
       end

+      it "generates no javascript errors with frames" do
+        @driver.browser.evaluate_on_new_document(
+          "window.errors ||= []; window.onerror = function(msg) { window.errors += msg; };" 
+        )
+        @session.visit "/cuprite/frames" 
+
+        @session.within_frame(0) { expect(@session.evaluate_script("window.errors")).to be_empty }
+        expect(@session.evaluate_script("window.errors")).to be_empty
+      end
+
       it "waits for network idle" do
         @session.visit "/cuprite/show_cookies" 
         expect(@session).not_to have_content("test_cookie")

This fails with this message:

Failure/Error: @session.within_frame(0) { expect(@session.evaluate_script("window.errors")).to be_empty }                 
  expected `"Uncaught SyntaxError: Identifier 'InvalidSelector' has already been declared".empty?` to be truthy, got false

However this seems only to happen with Chrome >= 102. When i run this test with Chrome 101 or lower it does not fail.

The identifier InvalidSelector is defined here: https://github.com/rubycdp/cuprite/blob/2bbf3634d92af4e8b8ec3026c2b66ccf1d0590ad/lib/capybara/cuprite/javascripts/index.js. Looks like these extensions are executed in the same context multiple times with frames?

hoffi avatar Jun 08 '22 14:06 hoffi

I have found this line: https://github.com/rubycdp/cuprite/blob/2bbf3634d92af4e8b8ec3026c2b66ccf1d0590ad/lib/capybara/cuprite/page.rb#L119

The extensions are injected every time one switches to a frame. Is that neccessary? Ferrum already seems to inject them once at the page load.

hoffi avatar Jun 10 '22 14:06 hoffi