Powered by PHPUnit testing framework, and being BDD compatible, Codeception framework is a widely preferred test automation framework for performing unit testing, functional testing, and acceptance testing of a web application. Codeception framework provides versatile commands for validating structure and data of JSON & XML, so you could easily perform API testing throughout your extensive REST & SOAP API calls. Using Codeception with LambdaTest, you can execute automated cross browser testing across 2000+ browsers through our on-cloud Selenium Grid.
All the code samples in this documentation can be found in the Codeception LambdaTest Repository on GitHub. You can either download or clone the repository to quickly run your tests.
Before you begin automation testing with Selenium and Codeception PHP, be ready with the below essentials:
1 2 3 4 |
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" php -r "if (hash_file('sha384', 'composer-setup.php') === '93b54496392c062774670ac18b134c3b3a95e5a5e5c8f1a9f115f203b75bf9a129d5daa8ba6a13e2cc8a1da0806388a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" php composer-setup.php php -r "unlink('composer-setup.php');" |
Once you have composer installed, you can leverage Composer to download and install Selenium dependencies. For that first create a composer.json file in your directory and add the following code to the json.
1 2 3 4 5 6 |
{ "require": { "phpunit/phpunit-selenium": "*", "facebook/webdriver": "dev-master" } } |
Now, all you would have to do is to run composer to install Selenium. You can do that by running following command in your terminal:
1 |
composer install |
If you are using Xampp or Mamp you may have to be a little more specific.
1 |
php composer.phar install |
Be aware of your LambdaTest authentication credentials i.e. your LambdaTest username, access key and HubURL. You need to set them up as your environment variables. You can retrieve them from your LambdaTest automation dashboard by clicking on the key icon near the help button.
set LT_USERNAME="YOUR_USERNAME"
set LT_ACCESS_KEY="YOUR ACCESS KEY"
export LT_USERNAME="YOUR_USERNAME"
export LT_ACCESS_KEY="YOUR ACCESS KEY"
Now, that we are done with basic set up. We go ahead to run our first automation script through Codeception framework and LambdaTest Integration. Check out our GitHub repository for finding all the code references that will be discussed in this topic.
Below is a sample acceptance test through PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php class FirstCest { public function frontpageWorks(AcceptanceTester $I) { sleep(5); $I->amOnPage('/sample-todo-app'); sleep(2); $I->checkOption('/html/body/div/div/div/ul/li[4]/input'); sleep(2); $I->checkOption('/html/body/div/div/div/ul/li[5]/input'); } } |
Notice the declaration of class name “AcceptanceTester”. We used this class to specify test configuration, port number, browser name, browser version and other desired capabilities.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# Codeception Test Suite Configuration # # Suite for acceptance tests. # Perform tests in browser using the WebDriver or PhpBrowser. # If you need both WebDriver and PHPBrowser tests - create a separate suite. class_name: AcceptanceTester modules: enabled: - WebDriver: url: 'https://lambdatest.github.io/sample-todo-app/' host: '{username}:{token}@hub.lambdatest.com' #provide your LambdaTest credentials port: 80 browser: chrome capabilities: name: 'Codeception Example' build: '1.0' browserName: 'Chrome' # request the latest version of chrome platform: 'ANY' version: '71.0' |
IMPORTANT
In the above code, remember to replace the token and username with respect to your LambdaTest account. You can find these values from LambdaTest dashboard. Your token would be the Access key.
To help you perform cross browser testing of your locally stored web pages, LambdaTest provides an SSH(Secure Shell) tunnel connection with the name Lambda Tunnel. With Lambda Tunnel, you can execute a test of your locally hosted Codeception Selenium instance on cloud to perform automated cross browser testing on 2000+ browsers offered by Selenium Grid on LambdaTest. So you make sure how well your changes look, even before your customers. Curious to know more about Lambda Tunnel?
Follow our documentation on Lambda Tunnel to know it all. OS specific instructions to download and setup tunnel binary can be found at the following links.
Download the binary file of:
Once, the tunnel is successfully set up. You can add the below code to your capabilities for testing internal servers on your network.
Tunnel Capabilities
1 |
tunnel: true |
Execute the below command and you are all good to go!
1 |
./vendor/bin/codecept run --steps |