Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Behat steps for the Achievements module
// Snippet method in FeatureContext class.
* @Then /^I should see the "(?P<achievement>[^"]*)" achievement "(?P<status>[^"]*)"$/
public function iShouldSeeTheAchievement($achievement, $status) {
if (!in_array($status, array('unlocked', 'locked'))) {
throw new \Exception(sprintf("Achievement status should either be 'locked' or 'unlocked', '%s' given instead.", $status));
// Locate all achievements.
$page = $this->getSession()->getPage();
$achievements = $page->findAll('css', '.achievement');
if (!$achievements) {
throw new \Exception(sprintf('No achievements are displayed on this page (%s).', $this->getSession()->getCurrentUrl()));
// Verify achievement is present on the page.
$achievement_found = FALSE;
foreach ($achievements as $item) {
if (FALSE !== strpos($item->getText(), $achievement)) {
// This is the achievement we want to check status on.
$achievement_found = $item;
if (!$achievement_found) {
throw new \Exception(sprintf('The achievement "%s" was not found on this page (%s).', $achievement, $this->getSession()->getCurrentUrl()));
if ($status === 'locked') {
// The class 'achievement-locked' should be present.
$class = 'achievement-locked';
else {
$class = 'achievement-unlocked';
if (!$achievement_found->hasClass($class)) {
throw new \Exception(sprintf('The achievement "%s" was found on the page (%s) but not with the "%s" status.', $achievement, $this->getSession()->getCurrentUrl(), $status));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment