Skip to end of metadata
Go to start of metadata

Installation 

You will soon be able to install the API wrapper for Perl via CPAN. In the meantime, you can get it from GitHub: 

GitHub Install
git clone http://github.com/testafy/perl.git


Example Test Using the Perl API Wrapper

use WWW::Testafy;
my $te = new WWW::Testafy(
	testafy_username => 'your_username',
	testafy_password => 'your_password',
);
my $id = $te->run_test(
    pbehave  => qq{
        For the url "http://google.com"
        Then pass this test
        },
    );
my $passed  = $te->test_passed($id);
my $planned = $te->test_planned($id);
print "Passed $passed tests out of $planned\n";
print $te->test_results_as_string($id);


Subroutines

 

error

Parameters: None

Return value: A reference to an array of error messages from the server's last response

Description: Call error when you want to view the error messages returned by the last call to the server.

Example

my @errors = $te->error();
foreach my $e (@errors) {
	print "$e\n";
}

 

error_string

Parameters: None

Return value: A formatted string of error messages, separated by newlines

Description: Call error_string when you want to view the error messages returned by the last call to the server, presented as a single string.

Example

my $err1 = $te->error_string();
my $err2 = join("\n",$te->error_string());
print "error_string is a convenience function" if $err1 eq $err2; #This only works if an error exists and is an array.

 

make_api_request

Parameters:

    • api_command: a string containing the request to be made
    • request_vars: a reference to a hash containing the values to be passed with the command

Return value: HTTP::Response object returned by the server

Description: Call make_api_request when you want to perform a specific API call. Please note that this subroutine is used internally, so it is not necessary to call make_api_request in order to access any API functionality.

Example

$te->make_api_request("test/stats/planned",{trt_id => 1});

 

message

Parameters: None

Return value: A string containing the message returned by the server

Description: Call message when you want to view the message returned by the server. This call retrieves the results of phrase_check, among other things.

Example

#Some number of commands
print $te->message() . "\n"; #Get output from the most recent command.

 

phrase_check

Parameters

    • pbehave: the PBehave code which should be checked for errors

Return value: A string describing the validity of the PBehave code and detailing any errors

Description: Call phrase_check when you want to check the validity of a PBehave test. Please note that phrase_check will verify that the commands used in the test exist, but not that they're valid in the particular context of the given test; it makes sure that a test can be run, but not that it will pass. 

Example

my $message = $te->phrase_check(pbehave => "This is not valid pbehave!");# $message eq "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."

 

run_test

Parameters

    • args: A hash of arguments, including:
      • pbehave: the PBehave code that should be run
      • product: the product you want to test (use "generic" if you're not sure)
      • verbose: whether or not additional output should be printed 

Return value

    • trt_id: an integer which identifies this test run on the server

Description: Call run_test when you want to run a test on the server. You can pass the returned trt_id to test_statustest_passedtest_failedtest_plannedtest_results, or test_results_as_string to get more information about the test run.

Example

my $trt_id = $te->run_test(pbehave => "For the url http://testafy.com\nThen pass this test", product => "generic", verbose => 1);

 

test_failed

Parameters:

    • trt_id: the id of the test run to check on, returned by run_test

Return value

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

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

Example

my $failed = $te->test_planned($trt_id); #How many tests have failed so far.

 

test_passed

Parameters

    • trt_id: the id of the test run to check on, returned by run_test

Return value

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

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

Example

my $passed = $te->test_planned($trt_id); #How many tests have been passed so far.

 

test_planned

Parameters:

    • trt_id: the id of the test run to check on, returned by run_test

Return value:

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

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

Example

my $planned = $te->test_planned($trt_id); #How many tests have been attempted so far. 
my $also_planned = $te->test_passed($trt_id) + $te->test_failed($trt_id); #Functionally the same, but using two calls.

 

test_results_as_string

Parameters:

    • trt_id: the id of the test run to check, returned by run_test

Return value

    • results: a string in TAP format containing the results of the test

Description: Call test_results_as_string when you want to view a string containing the results of the given test run, presented in TAP format. Please note that calling test_results_as_string before the test run is complete will return results for only the completed portion of the test.

Example

my $results = $te->test_results_as_string($trt_id);
print $results;
my $also_results = join("\n",map {$_->[1]} $te->test_results($trt_id)); #Effectively the same thing.

 

test_results

Parameters:

    • trt_id: the id of the test run to check, returned by run_test

Return value

    • results: an array containing the test results. Each item in the array is an array ref, pointing to a [result_type, result] pair.

Description: Call test_results when you want to know the results of a test. Please note that calling test_results before the test is complete will return results for only the completed portion of the test.

Example

my @results = $te->test_results($trt_id);
foreach my $pair (@results) {
	my ($code,$statement) {
		print "$code : $statement \n";
	}
}

 

test_status

Parameters

    • trt_id: the id of the test run to check on, returned by run_test

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
      • "completed," which means that the test finished normally
      • "stopped," which means that the test began 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 manually canceled

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

Example

do {
	sleep 1;
	my $status = $te->test_status($trt_id);
} while ($status eq "queued" || $status eq "running");

 

test_screenshots

Parameters

    • trt_id: the id of the test run for which you would like screenshots, returned by run_test

Return value

    • screenshots: an array containing the names of all of the screenshots for this test

Description: Call test_screenshots when you want to view a list of the screenshots taken during the current test run. 

Example

my $screenshots = $te->test_screenshots($trt_id);

 

test_screenshot_as_base64

Parameters

    • trt_id: the id of the test run which has the specified screenshot, returned by run_test
    • screenshot_name: the name of the screenshot in question

Return value

    • screenshot: the base64-encoded content of the screenshot

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

Example

my $screenshot = $te->test_screenshot_as_base64($trt_id, "screenshot-0001.png");

 

save_test_screenshot

Parameters

    • trt_id: the id of the test run which has the specified screenshot, returned by run_test
    • screenshot_name: the name of the screenshot in question
    • local_filename: the location to which the screenshot should be saved

Return value

    • success: whether or not the screenshot was successfully saved

Description: Call save_test_screenshot when you want to save a screenshot locally.

Example

my $success = $te->save_test_screenshot($trt_id, "screenshot-0001.png", "local_name.png");