Skip to content

Commit 5ac1bb8

Browse files
committed
Selenium: Refactor WaitFor* scripts to prevent crashes
The previously used approach with `Timeout.timeout` works. However, it might interrupt the `page.evaluate_script`, which will cause an unhandled exception with the Selenium webdriver which in turn crashes. Once crashed, all further Selenium-based tests will fail, too.
1 parent 64816d6 commit 5ac1bb8

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

spec/support/wait_for_ajax.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
module WaitForAjax
44
def wait_for_ajax
5-
Timeout.timeout(Capybara.default_max_wait_time) do
6-
loop until ajax_requests_finished?
5+
start_time = Time.current
6+
timeout = Capybara.default_max_wait_time
7+
8+
loop do
9+
break if ajax_requests_finished? || (Time.current - start_time) > timeout
10+
11+
sleep 0.1 # Short sleep time to prevent busy waiting
712
end
813
end
914

1015
def ajax_requests_finished?
16+
# This method MUST NOT be interrupted. Hence, Timeout.timeout is not used here.
17+
# Otherwise, Selenium and the browser driver might crash, preventing further tests from running.
1118
page.evaluate_script('jQuery.active').zero?
1219
end
1320
end

spec/support/wait_for_websocket.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
module WaitForWebsocket
44
def wait_for_websocket
5-
Timeout.timeout(Capybara.default_max_wait_time) do
6-
loop until websocket_finished?
5+
start_time = Time.current
6+
timeout = Capybara.default_max_wait_time
7+
8+
loop do
9+
break if websocket_finished? || (Time.current - start_time) > timeout
10+
11+
sleep 0.1 # Short sleep time to prevent busy waiting
712
end
813
end
914

1015
def websocket_finished?
16+
# This method MUST NOT be interrupted. Hence, Timeout.timeout is not used here.
17+
# Otherwise, Selenium and the browser driver might crash, preventing further tests from running.
1118
page.evaluate_script('CodeOceanEditorWebsocket?.websocket?.getReadyState() === WebSocket.CLOSED').present?
1219
end
1320
end

0 commit comments

Comments
 (0)