Skip to end of metadata
Go to start of metadata

Installation

The Ruby API wrapper consists of a single module, Testafy, containing a single class, TestafyTest.
You will soon be able to install this API wrapper as a Ruby gem by running "gem install testafy." 

For now, you can get the code from GitHub:

Ruby Install
git clone http://github.com/testafy/ruby.git

 

Example Test Using the Ruby API Wrapper

require 'testafy'

test = TestafyTest.new "user", "password"
test.pbehave = "For the url http://www.google.com \n Then pass this test"


# Run a test and wait for it to complete
test.run_and_wait
puts "passed: #{test.passed}, failed: #{test.failed}, total: #{test.planned}"
puts "\nresults:\n" + test.results_string

# Run a test and do other stuff while it runs
test.run
# Do some other stuff
sleep 1 until test.done?
puts "\nresults: \n" + test.results_string

 

 

Class Methods

new

Parameters

    • username: the username to be used for logging in to the API server.
    • password: the password to be used for logging in to the API server.
    • pbehave: the PBehave code to be run, as a string.

Return value:

    • test: a new instance of the TestafyTest class.

Description: Call new when you want to create a new test.

Example

require 'testafy'
 
test = Testafy::Test.new "username", "password"
# test = #<Testafy::Test:0x10fa728c0>
test2 = Testafy::Test.new "username", "password", "for the url http://google.com\n then pass this test"
# test2 = #<Testafy::Test:0x11079c0f0>

 

try_it_now

Parameters: None.

Return value:

    • try_it_now_test: a new instance of the TestafyTest class which can be run without logging in.

Description: Call try_it_now if you want to run a test without registering. try_it_now tests can be a maximum of 3 lines of PBehave and automatically include a 2 second delay between steps.

Example

require 'testafy'
 
test = Testafy::Test.try_it_now
# test = #<Testafy::Test:0x10fa728c0>
test.pbehave = "For the url http://testafy.com \n When the "About Us" link is clicked \n then the text "Simplify. Testafy." is present"
test.run_and_wait
puts test.results_string
# For the url http://testafy.com
# When the "About Us" link is clicked
#    WARNING: '"About Us" link' matches multiple elements
# Then the text "Simplify. Testafy." is present
#    Test 1 - Passed

 

Instance Methods

failed

Parameters: None.

Return value:

    • failed: the number of "Then" statements in the test's PBehave code that have failed so far.

Description: Call failed when you want to know how many "Then" statements in the test's PBehave code have failed so far.

Example

require 'testafy'

t = Testafy::Test.new "user", "pass"
t.run
puts t.failed
# prints 0
 
t.pbehave = "for the url http://google.com\n then fail this test"
t.run
puts t.failed
# prints 1

 

passed

Parameters: None.

Return value:

    • passed: the number of "Then" statements in the test's PBehave code that have passed so far.

Description: Call passed when you want to know how many "Then" statements in the test's PBehave code have passed so far.

Example

require 'testafy'
 
t = Tesfay::Test.new "user", "pass"
t.run
puts t.passed
# prints 1
 
t.pbehave = "then fail this test"
t.run
puts t.passed
# prints 0

 

phrase_check

Parameters: None.

Return value:

    • result: a string describing the validity of the PBehave code and detailing any errors found therein. 

Description: Call phrase_check when you want to make sure that all of the commands in the test's PBehave code are valid and can be run on the server. Note that this call verifies that the test can be run, but not that the test will pass. 

Example

require 'testafy'
t = Testafy::Test.new "user", "pass"
puts t.phrase_check
# prints "No errors found"
t.pbehave = "This is not valid pbehave!"
puts t.phrase_check
# prints "Cannot determine type of phrase for 'This is not valid pbehave!.' Phrases must start with For, Given, When, Then, If, Elsif, Else, End, or And."

 

planned

Parameters: None.

Return value:

    • planned: the total number of "Then" statements in the test's PBehave code.

Description: Call planned when you want to know how many "Then" statements are in the test's PBehave code.

Example

require 'testafy'
 
t = Testafy::Test.new "user", "pass"
t.pbehave = "then pass this test\n then fail this test"
 
t.run
puts t.planned
# prints 2

 

results

Parameters: None.

Return value:

    • results: An array of 2-element arrays, where each inner array is a [line type, line] pair. The lines are in TAP format.

Description: Call results when you want to know the results of the current test run. 

Example

require 'testafy'
 
t = Testafy::Test.new "user", "pass"
t.run
puts t.results.inspect
# prints
# [["comment", "# For the url http://www.google.com"], ["plan", "1..1"], ["comment", "# Given the url http://www.google.com"], ["success", "ok 1 - pass this test"]]

 

run

Parameters: None.

Return value

    • trt_id: the id of the test run on the server.

Description: Call run when you want to run a test on the server. Please note that if you call run more than once on the same TestafyTest instance, the test will be run again, and calls to get information about the test will refer only to the latest run.

Example

require 'testafy'
 
t = Testafy::Test.new "user", "password
# synchronous request (returns when the test has completed):
test_run_id = t.run
# test_run_id = 69202
 
# running again yields a new id (and runs the code again)
test_run_id = t.run
# test_run_id = 69203
 
# asynchronous test (returns when the test has been accepted by the server):
test_run_id = t.run
# test_run_id = 69204

 

status

Parameters: None.

Return value

    • status: a string describing the status of the test. The status will be one of the following:
      • "queued," which means that the test has not yet started.
      • "running," which means that the test is currently running but has not yet completed.
      • "completed," which means that the test finished normally.
      • "stopped," which means that the test began running but could not complete normally. This status might appear if, for example, the test included invalid PBehave.
      • "skipped," which means that the test was skipped.
      • "canceled," which means that the test was canceled manually. 

Description: Call status when you want to know the current status of a test.

Example

require 'testafy'
 
t = Testafy::Test.new "user", "pass"
# Run a test asynchronously, so we can check the status as it runs 
t.run
 
puts t.status and sleep 1 until t.done?
 
puts t.status

# Prints: 
# queued
# queued
# queued
# queued
# queued
# queued
# queued
# running
# running
# running
# running
# running
# running
# running
# running
# running
# running
# running
# completed

 

screenshots

Parameters: None.

Return value

    • screenshots: an array containing the filenames of all of the screenshots for the current test run.

Description: Call screenshots when you want to know the filenames of all of the screenshots for the current test run.

Example

require 'testafy'
 
t = Testafy::Test.new "user", "password"
t.pbehave = "for the url testafy.com \n then save a screenshot as 1.png \n when the 'About Us' link is clicked \n then save a screenshot as 2.png"


t.run  t.screenshots
# ["1.png","2.png"]

 

screenshot_as_base64

Parameters:

    • screenshot_name: the name of the screenshot in question.

Return value

    • screenshot: a string containing the base64 encoding of the image.

Description: Call screenshot_as_base64 when you want to know the base64 encoding of a specific screenshot.

Example

require 'testafy'
 
t = Testafy::Test.new "user", "password"
t.pbehave = "for the url testafy.com \n then save a screenshot as 1.png \n when the 'About Us' link is clicked \n then save a screenshot as 2.png"


t.run  t.screenshot_as_base64("1.png")
# "iVBORw0KGgoAAAANSUhEUgAAA[... 193,029 chars removed ...]Z6wo5ZiMAAAAASUVORK5CYII="

 

all_screenshots_as_base64

Parameters: None.

Return value

    • screenshots: An associative array mapping filenames (matching those returned by the screenshots instance method) to base64-encoded images, expressed as strings.

Description: Call all_screenshots_as_base64 when you want an expression of all images, base64-encoded, as strings.

Example

require 'testafy'
 
t = Testafy::Test.new "user", "password"
t.pbehave = "for the url testafy.com \n then save a screenshot as 1.png \n when the 'About Us' link is clicked \n then save a screenshot as 2.png"
 
t.run  t.all_screenshots_as_base64
# { 
#	"1.png" => "iVBORw0KGgoAAAANSUhEUgAAA[... 193,029 chars removed ...]Z6wo5ZiMAAAAASUVORK5CYII=",
# 	"2.png" => "iVBORw0KGgoAAAANSUhEUgAAA[... 193,029 chars removed ...]SABIr/ejRAAAAAElFTkSuQmCC"
# } 

 

save_screenshot

Parameters:

    • screenshot_name: the name of the screenshot in question.
    • local_filename: the name of the file to which the image will be saved.

Return value

    • true if the image was successfully saved as local_filename.

Description: Call save_screenshot when you want to save a screenshot as a local file.

Example

require 'testafy'
 
t = Testafy::Test.new "user", "password"
t.pbehave = "for the url testafy.com \n then save a screenshot as 1.png \n when the 'About Us' link is clicked \n then save a screenshot as 2.png"
 
t.run  t.save_screenshot("1.png", "local1.png")# true

 

save_all_screenshots

Parameters:

    • localdir: the directory in which the screenshots should be saved.

Return value

    • true if all screenshots were successfully saved

Description: Call save_all_screenshots when you want to save locally all of the screenshots from this test run. Please note that the specified directory will be created if it does not exist and that the screenshots will be saved with the names they have on the server.

Example

require 'testafy'
 
t = Testafy::Test.new "user", "password"
t.pbehave = "for the url testafy.com \n then save a screenshot as 1.png \n when the 'About Us' link is clicked \n then save a screenshot as 2.png"
 
t.run  t.save_all_screenshots("testafy_screenshots/")
# true

 

Attributes

login_name

A string containing the username to be used when logging in to the API server.

password

A string containing the password to be used when logging in to the API server.

base_uri

A string containing the URI where the API server is located. Please note that API commands are appended to this URI. You should not need to change this attribute unless you are accessing a different version of the API.

pbehave

A string containing the PBehave code to be run when run is called.

test_id

The id of the test run returned by the server, expressed as an integer. In some contexts, the test_id is called trt_id. Please note that the test_id is usually set by run and does not need to be changed.

message

A string containing the message returned by the server regarding the last API call made (for successful calls).

error

An array of strings describing any server-side errors that occurred with the last API call.