Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait update #139

Merged
merged 26 commits into from
Mar 18, 2015
Merged

Wait update #139

merged 26 commits into from
Mar 18, 2015

Conversation

sgreen-r7
Copy link
Contributor

Timeout Errors:

Currently using all of the wait methods, if the Timeout is reached, the error gets swallowed up by the surrounding code and errors do not get printed to the logs.

This causes the tests using these methods to continue with their test as if nothing bad has happened.

Example:

When /^the user configures a report on the asset while still in that state$/ do
  config = Nexpose::ReportConfig.new("cucumber report-#{@site.id}.csv", NSC.get_csv_template(@console), 'csv')
  config.add_filter('site', @site.id)
  config.save(@console, true)
  at_scenario_exit { @console.delete_report_config(config.id) }
  NSC.wait_for_report(@console, config.id)
  @tmp_report = NSC.download_report(@console, @site.id, config.id)
end

In the above example, if the wait_for_report hits a timeout, the next line download_report executes as if everything is okay.


Current Landscape:

wait_until (71)

scanning status and scan integration, as well as waiting on UI elements.

wait_for_report (37)

generally only used when the next step is to download a CSV report, or verifying a report's status.

wait_for_integration (84)

used to wait for scan_status to become 'finished', or other passed in status.

sleep (35)

mostly seems to be used to slow down the UI after click events. however, other instances are used instead of the above methods.


Proposed Enhancements:

The Nexpose-client gem can contain the following Wait object, to help deal with the Timeout concerns.

Currently Nexpose::Wait has the following methods:
for_report
for_integration
for_judgement

for_report would replace wait_for_report
for_integration would replace wait_for_integration
for_judgement would hopefully mostly replace wait_until

So our example at the beginning for waiting on reports would look something like this now:

When /^the user configures a report on the asset while still in that state$/ do
 config = Nexpose::ReportConfig.new("cucumber report-#{@site.id}.csv", NSC.get_csv_template(@console), 'csv')
 config.add_filter('site', @site.id)
 config.save(@console, true)
 at_scenario_exit { @console.delete_report_config(config.id) }

 report_wait = Nexpose::Wait.new
 report_wait.for_report( nsc: @console, report_id: config.id )
 unless report_wait.is_ready?
   expect(report_wait).to be_true, report_wait.error_message
 end

 @tmp_report = NSC.download_report(@console, @site.id, config.id)
end

Or you could wrap is_ready? in an if block (shown with adjusting the timeout, and having it retry if it fails):

When /^the user configures a report on the asset while still in that state$/ do
 config = Nexpose::ReportConfig.new("cucumber report-#{@site.id}.csv", NSC.get_csv_template(@console), 'csv')
 config.add_filter('site', @site.id)
 config.save(@console, true)
 at_scenario_exit { @console.delete_report_config(config.id) }

 report_wait = Nexpose::Wait.new(timeout: 60, retry: 2)
 report_wait.for_report( nsc: @console, report_id: config.id )
 if report_wait.is_ready?
    @tmp_report = NSC.download_report(@console, @site.id, config.id)
 else
   expect(report_wait).to be_true, report_wait.error_message
 end

end

Nexpose::Wait.new

Calling a new Nexpose::Wait object will allow you to initialize with the following options, which uses Ruby keyword arguments:

error_message: Overwrite the default error message.
retry_count: The number of times to retry the wait if we hit the timeout limit.
timeout: Number of seconds before we consider the request timed out.
polling_interval: Number of seconds to sleep before we retry to execute for the desired result.

Nexpose::Wait.new( retry_count: 3, timeout: 120, polling_interval: 1 )

for_report
pass in nsc connection, and report config id.
for_integration
pass in nsc connection, scan id, and status that you're looking for.
for_judgement
pass in a proc, and an optional description to display and output if an error or timeout occurs.

All for_ methods support outputting an error message for timeouts, and additionally support API errors, and NoMethodErrors if reports or scans don't exist.


Quick Win for Testing UI:

When using find during UI testing, pass in wait to set timeout for finding that element.
If it reaches the timeout, it will fail the test and log that it could not find the element in the allotted time.

Instead of this:

NSC.wait_until(:on_timeout => 'Timed out waiting for element to be visible') {
    element = find( css, visible: false )
    element.visible?
}

Try this:

element = find( css, visible: false, wait: 15 )
element.visible?

Capybara also supports a number of other options when using find

gschneider-r7 added a commit that referenced this pull request Mar 18, 2015
@gschneider-r7 gschneider-r7 merged commit 852e806 into rapid7:staging/v1 Mar 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants