Codeception web testing using phantomjs and selenium2 webdriver
Pre Requisites – PHP 5.3, PHP UNIT
What is phantomjs
This is used for headless webtesting
Run functional tests with frameworks such as Jasmine, QUnit, Mocha, Capybara, WebDriver, and many others
for screen capture
Programmatically capture web contents, including SVG and Canvas. Create web site screenshots with thumbnail preview
for Page automation
Access and manipulate webpages with the standard DOM API, or with usual libraries like jQuery.
for network monitoring
Monitor page loading and export as standard HAR files. Automate performance analysis using YSlow and Jenkins.
How to test websites using codeception and phantomjs and using selenium2 webdriver.
1. Download latest selenium2 webdriver from http://code.google.com/p/selenium/downloads/list?q=selenium-server-standalone-2
2. Download Phantomjs from http://phantomjs.org/download.html
3. Download codeception from http://codeception.com/install
or using
wget http://codeception.com/codecept.phar .
4. It’s a phar file and go to the folder where the phar file is place and run below command in the terminal. Actually its better to be in your project root director for easy access.
php codecept.phar bootstrap
5. Above command will create some folder structures for codeception.
What is codeception?
It’s a testing framework that allows you to implement different types of testing sequences. In the folders that created by codeception installation. You can see three different modules.
Simply does the Acceptance testing, Functional testing (using selenium, phantomjs, zombiejs, phpbrowser etc.. Unit testing, DB testing and also for webservices (API).
Lets go back to our steps.
6. Run phantomjs and Selenium2 from terminal.
– Go to the folder where it’s downloaded run the commands
To run selenium (make sure 4444 port is available) type
java -jar selenium-server-standalone-2.xx.xxx.jar
To run phantomjs type
./phantomjs – – webdriver=5555 (linux) (run in another port which is not used)
Create a simple acceptance test using selenium2 and run it using phantomjs
Configure codeception
1. The default configuration
– Go to test folder codeception.
– Open acceptance.suite.yml file with notepad
————————————————————————–
# Codeception Test Suite Configuration
# suite for acceptance tests.
# perform tests in browser using the Selenium-like tools.
# powered by Mink (http://mink.behat.org).
# (tip: that’s what your customer will see).
# (tip: test your ajax and javascript by one of Mink drivers).
# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
class_name: WebGuy
modules:
enabled:
– PhpBrowser
– WebHelper
config:
PhpBrowser:
url: ‘http://localhost/myapp/’
——————————————————————————
2. Configure phantomjs.
– Go to test folder codeception.
– Open acceptance.suite.yml file with notepad and modify like this.
– Make sure to run build command after updating configuration.
– From the codeception folder where the codeception.phar is located run.
‘php codecept.phar build’
——————————————————–
Modified file will look like this.
class_name: WebGuy
modules:
enabled: [Selenium2]
config:
Selenium2:
url: ‘http://google.com’
browser: phantomjs
capabilities:
unexpectedAlertBehaviour: ‘accept’
——————————————————————-
After build you will see this in terminal some updates in the files.
Building Guy classes for suites: functional, unit, acceptance
TestGuy includes modules: Filesystem, TestHelper
TestGuy.php generated successfully. 11 methods added
CodeGuy includes modules: CodeHelper
CodeGuy.php generated successfully. 0 methods added
WebGuy includes modules: Selenium2
WebGuy.php generated successfully. 58 methods added
———————————————————————
The configuration file is now updated with selenium2 module and it will run in browser phantomjs
url required – start url for your app
browser required – browser that would be launched
host – Selenium server host (localhost by default)
port – Selenium server port (4444 by default)
delay – set delay between actions in milliseconds (1/1000 of second) if they run too fast
capabilities – sets Selenium2 desired capabilities http://code.google.com/p/selenium/wiki/DesiredCapabilities . Should be a key-value array.
—————————————————————————–
3. Lets create a simple acceptance test.
php codecept.phar generate:cept acceptance google
This will create an acceptance test cept file “googleCept.php” in acceptance folder.
– Open googleCept.php file and write test to check google search.
– You can see the file already have some steps
————————————
wantTo(‘perform actions and see result’);
——————————–
$I = new WebGuy($scenario); //calls webguy class and its method
$I->wantTo(‘perform actions and see result’); //verbose to display what action you are going to test
—————————
Add script
wantTo(‘Test Google Search’);
//since the configuration file has base url this will be google.com/
$I->amOnPage(‘/’);
/*
fill the google search field, am using it’s name ‘q’ and value ‘rajeevprabhakaran.wordpress.com’. use firebug to get the name,id etc.
fillfield Fills a text field or textarea with value.
param $field
param $value
*/
$I->fillField(‘q’, ‘rajeevprabhakaran.wordpress.com’);
$I->click(‘Google Search’);
//checking whether the
$I->see(‘Software Testing’);
————————————–
Run the script using command
php codecept.phar run acceptance
—————————————–
You will see the result something like this
Codeception PHP Testing Framework v1.6.3.1
Powered by PHPUnit 3.7.21 by Sebastian Bergmann.
Suite acceptance started
Trying to test google search (googleCept.php) – Ok
Time: 5 seconds, Memory: 5.00Mb
OK (1 test, 1 assertion)
—————————
courtesy:
http://codeception.com/docs/modules/Selenium2
http://codeception.com/05-13-2013/phantom-js-headless-testing.html
No images available.