Run Codeception tests on BrowserStack. Sequentially and in parallel.
-
Add codecept.phar to a sample project folder by excuting the following command:
wget http://codeception.com/codecept.phar
orcurl -O http://codeception.com/codecept.phar
-
Execute
php codecept.phar bootstrap
-
Execute
php codecept.phar generate:cept acceptance Welcome
-
To configure the test, open and edit ./tests/acceptance.suite.yml and copy the file from Sample Tests/acceptance.suite.yml.
-
Execute
php codecept.phar build
. -
Lets write a test to search BrowserStack on Google. Open and edit file ./tests/acceptance/WelcomeCept.php and copy the file from Sample Tests/WelcomeCept.php.
-
To run a single test (on say Firefox), execute the command
php codecept.phar run --env firefox
.
-
You can make use of Environments, to execute tests on different browser and OS combinations.
-
Check the acceptance.suite.yml to see how the capabilities and environments are mentioned.
-
To run sequentially of 4 environments, execute the command
php codecept.phar run --env chrome --env firefox --env ie --env safari
To run sample test in parallel, you need the following two things:
- Robo, a task runner that executes your tests in parallel
- robo-paracept - Codeception tasks for parallel execution.
You can follow these steps:
-
Install 'Composer' if you haven't yet :
curl -sS https://getcomposer.org/installer | php
-
Add the composer.json file present under Parallel Testing folder to the root directory of the project.
-
Execute the command
php composer.phar install
to install the two components mentioned above. -
Execute
Robo.phar
and press 'Y' to create an empty RoboFile.php. -
Copy the RoboFile.php file under Parallel Testing to the empty RoboFile.php.
-
Change the env names to
p1
,p2
... instead ofchrome
,firefox
..., as given in acceptance.suite.yml file in Parallel testing folder -
The following function in RoboFile executes tests in parallel:
public function parallelRun()
{
$parallel = $this->taskParallelExec();
for ($i = 1; $i <= 4; $i++) {
$parallel->process(
$this->taskCodecept() // use built-in Codecept task
->suite('acceptance') // run acceptance tests
->env("p$i") // in its own environment
->xml("tests/_log/result_$i.xml")
);
}
return $parallel->run();
}
Execute the commmand robo.phar parallel:run
to execute tests in parallel.
It would be very confusing to have logs of multiple tests coming at once. Hence it is necessary to merge them. This is where robo-paracept's MergeReports comes into play which merges all results into one file. The following function merges all the results:
use \Codeception\Task\MergeReports;
function parallelMergeResults()
{
$merge = $this->taskMergeXmlReports();
for ($i=1; $i<=4; $i++) {
$merge->from("tests/_output/tests/_log/result_$i.xml");
}
$merge->into("tests/_output/tests/_log/result.xml")
->run();
}
Execute the commmand robo.phar parallel:merge-results
to merge results.
You can put the two fuctions together in one function as follows:
function parallelAll()
{
$result = $this->parallelRun();
$this->parallelMergeResults();
return $result;
}
Execute the commmand robo.phar parallel:all
to execute Codeception tests in parallel as well as merge results.
More details on parallel execution in Codeception available here.