Today we are going to learn cucumber functional automated testing basic concept with example. If you are new to cucumber than please go through my cucumber set-up blog.
Cucumber set-up step
Before starting our testing we will know some basic about

1.How to create feature file?
2.How to create step file corresponding to feature file?
Feature file contains human readable specification. A specification contains various scenarios and all scenarios needs to be executed successfully. A scenario contains at least three part

  • Given
  • When
  • Then

Create LoginTest.feature file inside the cucumber folder and add these lines in that

Feature: Logging in to Application

  Scenario: Successful login as a carer
    Given I go to the start page
    When I enter username "userName" and password "Password1"
    Then I am logged in
    When I logout
    Then I am not logged in

Now use this command to run test case and see what happens

grails test-app functional:cucumber

You will get this

| Running 1 cucumber test...

1 Scenarios (
1 undefined
)
3 Steps (
3 undefined
)
0m
0.237s


You can implement missing steps with the snippets below:

Given (~'^I go to the start page$') { ->;
    // Express the Regexp above with the code you wish you had
    throw new PendingException()
}

When (~'^I enter username "([^"]*)" and password "([^"]*)"$'){String username, String password ->
    // Express the Regexp above with the code you wish you had
    throw new PendingException()
}

Then (~'^I am logged in$') { ->;
    // Express the Regexp above with the code you wish you had
    throw new PendingException()
}

When(~'^I logout$'){->;
    // Express the Regexp above with the code you wish you had
    throw new PendingException()
}

Then (~'^I am not logged in$') { ->;
    // Express the Regexp above with the code you wish you had
    throw new PendingException()
}

| Completed 1 cucumber test, 1 failed in 605ms
| Tests FAILED

Now create a LoginTest_step.groovy class in cucumber folder and add missing steps in that.

import org.junit.Assert
import org.openqa.selenium.By
import static cucumber.api.groovy.EN.*

WebDriver driver = new FirefoxDriver()
Given (~'^I go to the start page$') {->
   driver.get("http://localhost:8080/application")
}

When (~'^I enter username "([^"]*)" and password "([^"]*)"$'){String username, String password ->
   driver.findElement(By.id("j_username")).sendKeys(userName)
   driver.findElement(By.id("j_password")).sendKeys(password)
   driver.findElement(By.id("loginButton")).click()
}

Then (~'^I am logged in$') { ->
   Assert.assertEquals("http://localhost:8080/application/success", driver.getCurrentUrl())
   Assert.assertTrue(driver.findElement(
       By.name("logoutLink")).getText().contains("Logout")
   )
}

When(~'^I logout$'){->
   driver.findElement(By.name("logout")).click()
}

Then (~'^I am not logged in$') {->   
   Assert.assertEquals("http://localhost:8080/application", driver.getCurrentUrl())
}

Now run the test case,it will get passed.
We’re done with our first Cucumber functional automated testing.