Class: Capybara::Lockstep::Client
- Inherits:
-
Object
- Object
- Capybara::Lockstep::Client
show all
- Includes:
- Logging, PageAccess
- Defined in:
- lib/capybara-lockstep/client.rb
Constant Summary
collapse
- ERROR_SNIPPET_MISSING =
'Cannot synchronize: capybara-lockstep JavaScript snippet is missing'
- ERROR_PAGE_MISSING =
'Cannot synchronize with empty page'
- ERROR_ALERT_OPEN =
'Cannot synchronize while an alert is open'
- ERROR_WINDOW_CLOSED =
'Cannot synchronize with closed window'
- ERROR_NAVIGATED_AWAY =
"Browser navigated away while synchronizing"
- SYNCHRONIZED_IVAR =
:@lockstep_synchronized_client
Instance Method Summary
collapse
Methods included from PageAccess
#alert_present?, #javascript_driver?, #page
Methods included from Logging
#log
Instance Method Details
#synchronize ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/capybara-lockstep/client.rb', line 36
def synchronize
self.synchronized = false
if alert_present?
log ERROR_ALERT_OPEN
return
end
start_time = Util.current_seconds
begin
Util.with_max_wait_time(timeout) do
message_from_js = evaluate_async_script(" let done = arguments[0]\n let synchronize = () => {\n if (window.CapybaraLockstep) {\n CapybaraLockstep.synchronize(done)\n } else {\n done(\#{ERROR_SNIPPET_MISSING.to_json})\n }\n }\n const emptyDataURL = /^data:[^,]*,?$/\n if (emptyDataURL.test(location.href) || location.protocol === 'about:') {\n done(\#{ERROR_PAGE_MISSING.to_json})\n } else if (document.readyState === 'complete') {\n // WebDriver always waits for the `load` event after a visit(),\n // unless a different page load strategy was configured.\n synchronize()\n } else {\n window.addEventListener('load', synchronize)\n }\n JS\n\n case message_from_js\n when ERROR_PAGE_MISSING\n log(message_from_js)\n when ERROR_SNIPPET_MISSING\n log(message_from_js)\n else\n log message_from_js\n end_time = Util.current_seconds\n ms_elapsed = ((end_time.to_f - start_time) * 1000).round\n log \"Synchronized client successfully [\#{ms_elapsed} ms]\"\n self.synchronized = true\n end\n end\n rescue ::Selenium::WebDriver::Error::ScriptTimeoutError\n timeout_message = \"Could not synchronize client within \#{timeout} seconds\"\n log timeout_message\n if timeout_with == :error\n raise Timeout, timeout_message\n else\n # Don't raise an error, this may happen if the server is slow to respond.\n # We will retry on the next Capybara synchronize call.\n end\n rescue ::Selenium::WebDriver::Error::UnexpectedAlertOpenError\n log ERROR_ALERT_OPEN\n # Don't raise an error, this will happen in an innocent test where a click opens an alert.\n # We will retry on the next Capybara synchronize call.\n rescue ::Selenium::WebDriver::Error::NoSuchWindowError\n log ERROR_WINDOW_CLOSED\n # Don't raise an error, this will happen in an innocent test where a click closes a window.\n # We will retry on the next Capybara synchronize call.\n rescue ::Selenium::WebDriver::Error::JavascriptError => e\n # When the URL changes while a script is running, my current selenium-webdriver\n # raises a Selenium::WebDriver::Error::JavascriptError with the message:\n # \"javascript error: document unloaded while waiting for result\".\n # We will retry on the next Capybara synchronize call, by then we should see\n # the new page.\n if e.message.include?('unload')\n log ERROR_NAVIGATED_AWAY\n else\n unhandled_synchronize_error(e)\n end\n rescue StandardError => e\n unhandled_synchronize_error(e)\n end\nend\n")
|
#synchronized=(value) ⇒ Object
32
33
34
|
# File 'lib/capybara-lockstep/client.rb', line 32
def synchronized=(value)
page.instance_variable_set(SYNCHRONIZED_IVAR, value)
end
|
#synchronized? ⇒ Boolean
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/capybara-lockstep/client.rb', line 15
def synchronized?
value = page.instance_variable_get(SYNCHRONIZED_IVAR)
value.nil? ? true : value
end
|