Driving from CSV with SaunterPHP
This is not what I was expecting to release today, but it is still important. Release 1.0.1 of SaunterPHP [1] has grown a CSV Provider. What this means for script creators is that you can [nicely] read input into scripts based on values stored externally to the script — in a csv.
Because SaunterPHP is opinionated, csv files go in the support/csv directory which isn’t going to exist on current projects, so you’ll have to make it. Once that is in place, it is just a matter of passing in the name of the file to the provider.
Here is an example from the Sauce Labs example.
<pre lang="php">namespace RemoteControl;
require_once 'tailored/SauceTestCase.php';
require_once 'pages/LandingPage.php';
require_once 'SaunterPHP/Framework/Providers/CSV.php';
class ProviderTest extends SauceTestCase {
/**
* @test
* @group shallow
* @group authentication
* @group provider
*/
public function basic_csv_example() {
$csv = new \SaunterPHP_Framework_Providers_CSV("invalid_usernames.csv");
$row = $csv->random_row();
$landing = new LandingPage();
$landing->open_default_base_url();
$form = $landing->open_sign_in_form();
$form->username = $row["username"];
$form->password = $row["password"];
$form->login(False);
$this->verifyEquals($form->error_message, "Incorrect username or password.");
}
}
Right now, the only method on the provider is random_row(), but all the information is stored in a data member variable so any interesting manipulations you want to do can be done to it. Perhaps like iterating over it.
<pre lang="php">$csv = new \SaunterPHP_Framework_Providers_CSV("invalid_usernames.csv");
foreach ($csv->data as $row) {
// do stuff
}
Oh, and the CSV needs to include the headers in it. This is so the row information is returned as an associated array.
[1] Just ignore the crazy bump in version numbers; this is to make PEAR whinge less.