Ruby

To integrate your Ruby script with Perfecto Smart Reporting, perform the steps provided on this page.

Download

Download the Reporting SDK client for your programming language and framework.

Smart Reporting setup

The following code changes are required to include reporting in your test.

Important: The following snippets are a standalone script that does not use any execution testing framework.

Mandatory

Requires

Add the following require statement to the test script:

Copy
require 'perfecto-reporting'

Create an instance of the reporting client

Create an instance of the ReportiumClient used to log test start/step/end events:

Copy
def create_reportium_client
  perfectoExecutionContext = PerfectoExecutionContext.new(
    PerfectoExecutionContext::PerfectoExecutionContextBuilder
      .withWebDriver(@driver)
      .build)

  @reportiumClient = PerfectoReportiumClient.new(perfectoExecutionContext)
end

Optional changes (but highly recommended)

Add reporting tags

Add Tags are used as a freestyle text that is used for filtering in the Reporting app.
For example: .withContextTags('Test tag1', 'Test tag2', 'Test tag3')

Copy
def create_reportium_client
 cf = CustomField.new('fKey', 'fValue')
 perfectoExecutionContext = PerfectoExecutionContext.new(
    PerfectoExecutionContext::PerfectoExecutionContextBuilder
      .withProject(Project.new('Reporting SDK Ruby', '1')) # Optional
      .withJob(Job.new('Ruby Job', 1)) # Optional
      .withContextTags('Test tag1', 'Test tag2', 'Test tag3') # Optional
      .withCustomFields(cf)  # Optional
      .withWebDriver(@driver)
      .build)

  @reportiumClient = PerfectoReportiumClient.new(perfectoExecutionContext)
end

Add test report and steps

Start a new test

A TestContext may be defined to include Tags and Custom Fields that are relevant to identify a specific test within a full test suite (defined by the PerfectoExecutionContext used by the PerfectoReportiumClient). The test-level tags will be added to the client-level tags associated with the test suite. The test-level Custom Fields are added to the client-level ones, overwriting any custom field values for the same key.

Copy
tcf = CustomField.new('tfKey', 'tfValue')
tc = TestContext::TestContextBuilder
        .withTestExecutionTags('ttag1', 'ttag2' )
        .withCustomFields(tcf)
        .build()
@reportiumClient.testStart('Test Name', tc)

Add test steps

Separate your test into actions using test steps.

Copy
@reportingClient.stepStart 'Searching PerfectoCode GitHub'
@driver.find_element(:name => 'q').send_keys('PerfectoCode GitHub')
@reportingClient.stepEnd # The message argument is optional

Add assertions to the Execution Report

At various points within a test execution, the script may perform verification of different test conditions. The result of these verification may be added to the Test Report by using the reportiumAssertion() method of the ReportiumClient instance. When using this method, the script includes two parameters:

  • A message string - that will be used to label the assertion.
  • A Boolean - indicates the result of the verification operation.
Copy
@reportingClient.stepStart 'Navigate to google'
@driver.get 'http://google.com'
@reportingClient.reportiumAssert 'Assertion message', driver.find_element(:name => 'q').nil?
@reportingClient.stepEnd 'End step'

Stop the test

When the test is completed - supply an indication of the final outcome of the test by generating a TestResult instance. The TestResultFactory class supports:

  • createSuccess method - that notifies the reporting server that the test resulted in a successful status.
  • createFailure method - that notifies the reporting server that the test resulted in a unsuccessful status and supports adding a notification message that is displayed in the test report.

Copy
...//selenium code
if self.passed?
  @reportiumClient.testStop(TestResultFactory.createSuccess)
else
  @reportiumClient.testStop(TestResultFactory.createFailure(@exception.message, @exception, self.failureReason))
end

In addition to providing the status of the test result, it is possible to provide additional tags and custom fields to the test - this may be used, for example, to add indications of the paths that caused the result or the reason for stopping the test. Use the TestContext to add additional tags and custom fields:

Copy
cfe1 = CustomField.new(name[0], value[0])
cfe2 = CustomField.new(name[1], value[1])

tec = TestContext::TestContextBuilder
           .withTestExecutionTags(endTags[0] , endTags[1])
           .withCustomFields(cfe1, cfe2)
           .build()

reportingClient.testStop(TestResultFactory.createFailure(self.status.message, nil, self.failureReason), tec)

Adding the TestContext to the testStop is optional.

Get the report URL

Copy
//retrieve the report URL
@reportiumClient.getReportUrl

GitHub samples

Browse the Perfecto GitHub repo for complete Ruby Reporting samples.