Module: ApiMaker::SpecHelper
Overview
rubocop:disable Metrics/ModuleLength
Defined Under Namespace
Modules: BrowserLogs, ExpectNoBrowserErrors, WaitForExpect, WaitForFlashMessage
Classes: ExecuteCollectionCommand, ExecuteMemberCommand, JavaScriptError, SelectorFoundError, SelectorNotFoundError
Instance Method Summary
collapse
-
#browser_firefox? ⇒ Boolean
-
#chrome_logs ⇒ Object
-
#confirm_accept ⇒ Object
-
#expect_no_browser_window_errors ⇒ Object
-
#expect_no_errors ⇒ Object
-
#expect_not_to_be_able_to(ability, model, permissions) ⇒ Object
-
#expect_to_be_able_to(ability, model, permissions) ⇒ Object
-
#js_fill_in(element_id, with:) ⇒ Object
-
#model_column_selector(model, identifier) ⇒ Object
-
#model_row_selector(model) ⇒ Object
-
#pretty_html ⇒ Object
-
#recorded_browser_logs ⇒ Object
-
#reset_indexeddb ⇒ Object
-
#wait_for_action_cable_to_connect ⇒ Object
-
#wait_for_and_find(selector, *args) ⇒ Object
-
#wait_for_attribute_row(attribute: nil, identifier: nil, label: nil, value: nil, **opts) ⇒ Object
-
#wait_for_browser(delay_sec: 0.2, message: "wait for browser", timeout_sec: 6) ⇒ Object
-
#wait_for_no_selector(selector, *args) ⇒ Object
-
#wait_for_path(expected_path, **args) ⇒ Object
-
#wait_for_selector(selector, *args) ⇒ Object
-
#wait_for_selectors(*selectors) ⇒ Object
#wait_for_flash_message
#wait_for_expect
#expect_no_browser_errors
#browser_logs
Instance Method Details
#browser_firefox? ⇒ Boolean
20
21
22
23
|
# File 'lib/api_maker/spec_helper.rb', line 20
def browser_firefox?
capabilities = page.driver.browser.try(:capabilities)
capabilities && capabilities[:browser_name] == "firefox"
end
|
#chrome_logs ⇒ Object
25
26
27
|
# File 'lib/api_maker/spec_helper.rb', line 25
def chrome_logs
page.driver.browser.manage.logs.get(:browser)
end
|
#confirm_accept ⇒ Object
29
30
31
|
# File 'lib/api_maker/spec_helper.rb', line 29
def confirm_accept
page.driver.browser.switch_to.alert.accept
end
|
#expect_no_browser_window_errors ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/api_maker/spec_helper.rb', line 33
def expect_no_browser_window_errors
errors = execute_script("if (window.errorLogger) { return window.errorLogger.getErrors() }")
return if !errors.is_a?(Array) || errors.empty?
last_error = errors.last
custom_trace = []
custom_trace += last_error.fetch("backtrace") if last_error["backtrace"].is_a?(Array)
custom_trace += caller
error = JavaScriptError.new("#{last_error["errorClass"]}: #{last_error["message"]}")
error.set_backtrace(custom_trace)
raise error
end
|
#expect_no_errors ⇒ Object
49
50
51
52
|
# File 'lib/api_maker/spec_helper.rb', line 49
def expect_no_errors
expect_no_flash_errors
expect_no_browser_errors
end
|
#expect_not_to_be_able_to(ability, model, permissions) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/api_maker/spec_helper.rb', line 67
def expect_not_to_be_able_to(ability, model, permissions)
permissions.each do |permission|
expect(ability).not_to be_able_to permission, model
if model.is_a?(ActiveRecord::Base) && model.persisted?
readable_models = model.class.where(id: model).accessible_by(ability, permission)
expect(readable_models).to be_empty
end
end
end
|
#expect_to_be_able_to(ability, model, permissions) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/api_maker/spec_helper.rb', line 54
def expect_to_be_able_to(ability, model, permissions)
permissions.each do |permission|
expect(ability).to be_able_to permission, model
if model.is_a?(ActiveRecord::Base) && model.persisted?
readable_models = model.class.where(id: model).accessible_by(ability, permission)
expect(readable_models).to eq [model]
end
end
end
|
#js_fill_in(element_id, with:) ⇒ Object
80
81
82
|
# File 'lib/api_maker/spec_helper.rb', line 80
def js_fill_in(element_id, with:)
page.execute_script("document.querySelector(#{element_id.to_json}).value = #{with.to_json}")
end
|
#model_column_selector(model, identifier) ⇒ Object
84
85
86
|
# File 'lib/api_maker/spec_helper.rb', line 84
def model_column_selector(model, identifier)
".#{model.model_name.singular.dasherize}-row[data-model-id='#{model.id}'] .live-table-column[data-identifier='#{identifier}']"
end
|
#model_row_selector(model) ⇒ Object
88
89
90
|
# File 'lib/api_maker/spec_helper.rb', line 88
def model_row_selector(model)
".#{model.model_name.singular.dasherize}-row[data-model-id='#{model.id}']"
end
|
#pretty_html ⇒ Object
92
93
94
95
|
# File 'lib/api_maker/spec_helper.rb', line 92
def pretty_html
require "htmlbeautifier"
HtmlBeautifier.beautify(page.html)
end
|
#recorded_browser_logs ⇒ Object
97
98
99
|
# File 'lib/api_maker/spec_helper.rb', line 97
def recorded_browser_logs
@recorded_browser_logs || []
end
|
#reset_indexeddb ⇒ Object
#wait_for_action_cable_to_connect ⇒ Object
105
106
107
|
# File 'lib/api_maker/spec_helper.rb', line 105
def wait_for_action_cable_to_connect
sleep 0.5
end
|
#wait_for_and_find(selector, *args) ⇒ Object
109
110
111
112
113
114
115
116
|
# File 'lib/api_maker/spec_helper.rb', line 109
def wait_for_and_find(selector, *args)
element = find(selector, *args)
expect_no_browser_errors
element
rescue Capybara::ElementNotFound => e
expect_no_browser_errors
raise ApiMaker::SpecHelper::SelectorNotFoundError, e.message
end
|
#wait_for_attribute_row(attribute: nil, identifier: nil, label: nil, value: nil, **opts) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/api_maker/spec_helper.rb', line 118
def wait_for_attribute_row(attribute: nil, identifier: nil, label: nil, value: nil, **opts)
raise "No attribute or identifier given" if !attribute && !identifier
raise "No label or value was given" if !label && !value
tr_selector = ".component-api-maker-attribute-row"
tr_selector << "[data-attribute='#{attribute.camelize(:lower)}']" if attribute
tr_selector << "[data-identifier='#{identifier}']" if identifier
wait_for_selector "#{tr_selector} > .attribute-row-label", exact_text: label, **opts if label
wait_for_selector "#{tr_selector} > .attribute-row-value", exact_text: value, **opts if value
end
|
#wait_for_browser(delay_sec: 0.2, message: "wait for browser", timeout_sec: 6) ⇒ Object
130
131
132
133
134
135
|
# File 'lib/api_maker/spec_helper.rb', line 130
def wait_for_browser(delay_sec: 0.2, message: "wait for browser", timeout_sec: 6)
WaitUtil.wait_for_condition(message, timeout_sec: timeout_sec, delay_sec: delay_sec) do
expect_no_browser_errors
yield
end
end
|
#wait_for_no_selector(selector, *args) ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/api_maker/spec_helper.rb', line 161
def wait_for_no_selector(selector, *args)
expect(page).to have_no_selector selector, *args
expect_no_browser_errors
rescue RSpec::Expectations::ExpectationNotMetError => e
expect_no_browser_errors
raise ApiMaker::SpecHelper::SelectorFoundError, e.message
end
|
#wait_for_path(expected_path, **args) ⇒ Object
137
138
139
140
141
142
143
144
145
|
# File 'lib/api_maker/spec_helper.rb', line 137
def wait_for_path(expected_path, **args)
args[:ignore_query] = true unless args.key?(:ignore_query)
expect(page).to have_current_path(expected_path, args)
expect_no_browser_errors
rescue RSpec::Expectations::ExpectationNotMetError => e
expect_no_browser_errors
raise e
end
|
#wait_for_selector(selector, *args) ⇒ Object
147
148
149
150
151
152
153
|
# File 'lib/api_maker/spec_helper.rb', line 147
def wait_for_selector(selector, *args)
expect(page).to have_selector selector, *args
expect_no_browser_errors
rescue RSpec::Expectations::ExpectationNotMetError => e
expect_no_browser_errors
raise ApiMaker::SpecHelper::SelectorNotFoundError, e.message
end
|
#wait_for_selectors(*selectors) ⇒ Object
155
156
157
158
159
|
# File 'lib/api_maker/spec_helper.rb', line 155
def wait_for_selectors(*selectors)
selectors.each do |selector|
wait_for_selector(selector)
end
end
|