Selenium + PHP
Introduction to Selenium Webdriver Installation and Environment set up Chrome Chrome with options Chrome in Mobile Emulation IE Firefox Element identification methods Assertions in Selenium in PHP Interacting with elements in Selenium in PHP Basic Browser window automation Sending keys in Selenium in PHP Synchronization in Selenium Check if Element exists Working with Tables using Selenium Performing advanced actions using Selenium in PHP Executing JavaScript in Selenium in PHP Working with multiple Browser Windows or tabs Working with multiple frames Handling alerts Taking a screenshot in selenium Integration with BDD framework like behatbehat in Selenium in php
This topic covers how to write the Selenium tests in behat (BDD framework just like cucumber). Just follow below steps.- Create composer.json file and mention all dependencies. Then install all dependencies.
- Create a features directory and store features in it.
- Create step definitions.
Installing all dependencies
Create composer.json file with below contents.
{
“require-dev”: {
“phpunit/phpunit”: “*”,
“facebook/webdriver”: “dev-master”,
“behat/behat”: “2.4.*@stable”
},
“minimum-stability”: “dev”,
“config”: {
“bin-dir”: “bin/”
}
}
Then install the dependencies using composer.
php composer.phar install
Creating feature file
Then Create features directory and create a feature file in it.
Feature: Title check of pages
Scenario: Check that the title of the home page is “Tutorial”
Given I am on the “https://www.softpost.org” webpage
Then I verify that title contains “Tutorial”
Creating step definitions
Then create a bootstrap directory inside features directory and store FeatureContext.php file in it. This file contains step definitions.
<?php
use BehatBehatContextBehatContext,
BehatBehatExceptionPendingException;
use BehatGherkinNodePyStringNode,
BehatGherkinNodeTableNode;
class FeatureContext extends BehatContext {
protected $driver;
/** @BeforeScenario */
public function before($event)
{
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => ‘chrome’);
$this->driver = RemoteWebDriver::create(‘https://localhost:4444/wd/hub’, $capabilities);
}
/**
* @Given /^I am on the “([^”]*)” webpage$/
*/
public function iAmOnTheWebpage($page)
{
$this->driver->get($page);
}
/**
* @Then /^I verify that title contains “([^”]*)”$/
*/
public function iVerifyThatTitleContains($title)
{
// checking that page title contains word ‘Tutorial’
PHPUnit_Framework_TestCase::assertContains($title, $this->driver->getTitle());
}
/** @AfterScenario */
public function after($event)
{
$this->driver->quit();
}
}
?>
To execute the features, you can use below syntax.
bin/behat
Web development and Automation testing
solutions delivered!!