Avoiding redundancy in test automation with Codeception

Here’e the next part of the article from last week, which you can read here. Today we learn how to avoid redundancy in test automation with Codeception.

Once I had a few tests that were run regularly, I realized that login/sign-up scripts were the most frequently used of all. I started looking for a way to improve and reuse sections of code. The best way to do this is to do the following:

1. Add custom functions in the AcceptanceHelper file. You can add extra methods to your tests and use them just like you use the regular ones that are built-in the suite. All you have to do is open the AcceptanceHelper.php file found in the _support folder and add your method.

Here’s a good example of a useful method, which will allow you to change any suite settings while a test is running. You can change any parameter of a loaded module:

public function changeParameter ($param, $value) {

$this->getModule(‘WebDriver’)->_reconfigure(array($param => $value));

}

Add it to AcceptanceHelper. php and save it, then do a codecept.phar build. When you want to use the method, you should just do so as it were a regular action, but don’t forget to define the variables:

$param = ‘url’;

$value = ‘www.google.com’;

$I->changeParameter($param, $value);

After this runs, the next ‘$I->amOnPage(‘/’);’ will take you to ‘www.google.com’.

2. Using StepObjects you can save series of actions and reuse them whenever you please. To generate a StepObject file, run php codecept.phar generate:stepobject acceptance MyFirstStepObject

Then, you will be prompted to name your stepObject class:

php codecept.phar generate:stepobject acceptance MyFirstStepObject

Add action to StepObject class (ENTER to exit): login

Add action to StepObject class (ENTER to exit):

StepObject was created in <you path>/tests/acceptance/_steps/MemberSteps.php

After you open the file, you will notice that a class was created. Write your function between the parantheses. Let’s say you want to wait for an element and then click it. So that you don’t have to type these two actions every time, you can save them in a function, like this:

<?php

namespace AcceptanceTester;

class MyFirstStepObjectSteps extends \AcceptanceTester

{

public function waitAndClick($element) {

       $I->waitForElement($element, 60);

       $I->click($element);

       }

}

?>

When you decide you want to run these actions in a test, you just need to add the StepObject file name to the AcceptanceTester class:

$I = new AcceptanceTester\MyFirstStepObjectSteps($scenario);

$I->waitAndClick (‘MyButtonPath’);

If you don’t want to use StepObjects, you could save these parts of code in the helper file. Open it up and instead of defining the function with “$I”, use “$webDriver”. You will not be forced to add the stepObject name to $I, you will just need to build codecept.phar again. This method will then be available throughout all files using AcceptanceTester.

public function waitAndClick($element) {

       $webDriver = $this->getModule(‘WebDriver’);

       $webDriver->waitForElement($element, 60);

       $webDriver->click($element);

}

When using it, it will be easier than StepObjects.

$I = new AcceptanceTester($scenario);

$I->waitAndClick (‘MyButtonPath’);

3. Save pieces of code you use regularly in separate files. You can use this in case you have a miscellaneous function you use from time to time, for example a random e-mail generator. You just need to save it as a function. Create a php file in your suite’s folder (you can also place it in a subfolder) and add the path to it in the _bootstrap.php file placed in either the tests folder (if you want the file to be used globally) or the suite folder (if you want the file to be used for that suite only).

<?php

require_once __DIR__ . ‘/yourFolder/file1.php;

require_once __DIR__ . ‘/yourFolder/file2.php;

and so on.

Your custom function might look like this:

function randomString($length) {

   $key = ”;

   $keys = array_merge(range(1, 9), range(‘a’, ‘z’));

   for ($i = 0; $i < $length; $i++) {

       $key .= $keys[array_rand($keys)];

   }

   return $key;

}

When you want to run it in a test, you just need to define the variables and call the function.

$I->fillField(‘myField’, randomString(5));

You don’t have to build codecept.phar again. Just don’t forget to add the path to the file in your _bootstrap.php.

In the following article we will discuss how to efficiently identify and use the page elements (CSS selectors, xpaths) in your tests, and how to use class files to define common variables in frequently used functions. Subscribe to the newsletter and don’t miss out!

Leave a Comment

Your email address will not be published.

Scroll to Top