Skip to content

Instantly share code, notes, and snippets.

@tsilvers
Last active June 1, 2016 20:34
Show Gist options
  • Save tsilvers/761cf7d7ae6cee831e3aecce08e451ce to your computer and use it in GitHub Desktop.
Save tsilvers/761cf7d7ae6cee831e3aecce08e451ce to your computer and use it in GitHub Desktop.
Setup instructions for Javascript testing with Behat / Mink
=== SETUP ===
Instructions were tested with Ubuntu, but should be similar for other Linux distros.
Java is needed to run the Selenium Stabdalone Server:
sudo apt-get install default-jre
Add Selenium Standalone Server via Composer:
composer require --dev se/selenium-server-standalone
Add Mink Selenium2 driver via Composer:
composer require --dev behat/mink-selenium2-driver
Install Firefox:
sudo apt-get install firefox
Install X virtual frame buffer for capturing browser display in memory:
apt-get install xvfb
Update behat.yml. This is a SAMPLE behat.yml, please note the references to selenium2, javascript, and firefox:
default:
formatters:
progress: true
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: http://your-dev-url.com
browser_name: firefox
sessions:
selenium2:
selenium2: ~
symfony2:
symfony2: ~
suites:
default:
paths:
- %paths.base%/features
mink_session: symfony2
mink_javascript_session: selenium2
contexts:
- NotifierDatabaseContext:
dnsprefix: mysql
host: localhost
port: 5432
dbname: dbdev
user: user
password: password
- MinkFeatureContext: ~
Start the Selenium Standalone Server:
Run "start-selenium-server.sh start", which is provided as a separate file with this gist.
The script should be executed from the composer base directory so that path "vendor/bin/selenium-server-standalone" is valid.
=== Sample Usage ===
"@javascript" has to be added before Behat scenarios which require javascript testing.
Sample scenario (requires additions to the Mink Context listed below):
@javascript
Scenario: Deleting a Store
When I follow "Delete"
And I wait for the modal to load
Then I should see "Do you really want to delete this item?"
When I click javascript button "modal-delete-button"
Then I should see "Store, with Code [I_AM_JUST_A_TEST], was successfully deleted."
Two steps added to the Mink Context to support these sample javascript tests:
/**
* @When I wait for the modal to load
*/
public function iWaitForTheModalToLoad()
{
$this->getSession()->wait(
5000,
"$('#modal-delete:visible').length > 0"
);
}
/**
* @Then I click javascript button :button
*/
public function iClickJavascriptButton($button)
{
$this->getSession()->getPage()->findButton($button)->click();
}
#/bin/bash
export DISPLAY=:5.0
XVFB_SEARCH="Xvfb :5 -ac -screen 0 1024x768x8"
XVFB_START="Xvfb :5 -ac -screen 0 1024x768x8"
SELENIUM_SEARCH=selenium-server-standalone
SELENIUM_START="vendor/bin/selenium-server-standalone"
function get_pid() {
let PID=0
let PID=$(ps -ef | grep "$*" | grep -v grep | sed 's/ */ /g' | cut -f2 -d" ") 2> /dev/null
echo $PID
}
function start_proc() {
let PID=$(get_pid "$1")
if [[ $PID -gt 0 ]]
then echo Process is already running as PID $PID: $1
else $2 > /dev/null 2>&1 &
let PID=$(get_pid "$1")
if [[ $PID -gt 0 ]]
then echo Started as PID $PID: $1
else echo Could not start process: $1
fi
fi
}
function stop_proc() {
let PID=$(get_pid "$1")
if [[ $PID -eq 0 ]]
then echo Process is not running, nothing to stop: $1
else kill -9 $PID
sleep 1
let PID=$(get_pid "$1")
if [[ $PID -eq 0 ]]
then echo Stopped process: $1
else echo Failed to stop PID $PID: $1
fi
fi
}
if [[ x$1 == x || x$2 != x || ( $1 != start && $1 != restart && $1 != stop ) ]]
then echo "Usage: $0 {start|restart|stop}"
exit
fi
if [[ $1 == stop || $1 == restart ]]
then stop_proc "$XVFB_SEARCH"
stop_proc "$SELENIUM_SEARCH"
fi
if [[ $1 == start || $1 == restart ]]
then start_proc "$XVFB_SEARCH" "$XVFB_START"
start_proc "$SELENIUM_SEARCH" "$SELENIUM_START"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment