Behat Create Feature file in language other than English

Profile picture for user devraj

In Behat, Support is provided for other languages so that stakeholders can express themselves in language they are comfortable with. You can write your Gherkin Feature file in 60+ Languages in Behat.

Consider multilingual website which exist in different languages and you have to assert and find text in that language only. In that case you would need support for other languages than English.

In a single project, we can have Feature files in multiple languages; but for one Feature file, only one language will work. For step definition, no languages configuration or settings are required.

To check if Behat and Gherkin support your language (for example, Spanish), run:

behat --story-syntax --lang=es

And to check Behat-Mink translator exist or you can say Mink predefined step definition mapping exist for other languages than English, you can refer:

This pirrotta.it website is very useful to check Keyword for particular language. Visit their website and select Behat-Gherkin Translator language, For example below it is showing for Spanish Translation.

You can also check corresponding step definition pattern, here. Select the language you want to translate in Behat-Mink Translator

Here is my feature in English Language:

# language: en
Feature: Test Login
  Scenario: My First login
    Given I am on homepage
    When I follow "English"
    Then I should see "Log in"

And this is Spanish translated Feature for same:

# language: es
Característica: Prueba de inicio de sesión
  Escenario: Mi primer inicio de sesión
    Dadas estoy en la página de inicio
    Entonces sigo "Español"
    Entonces debo ver "Iniciar sesión"

Note: Keep in mind that any language different from en should be explicitly marked with a # language: languageCode comment at the beginning of your *.feature file.

You don't need to create any additional step definition for above Spanish feature file because those steps already exist in Mink Context. You can generate the new step the same you do it in English. You can write multiple language specific annotation for same method.

/**
 * @Then this is my test step
 * @Then este es mi paso de prueba
*/
public function thisIsMyTestStep()
{
}

Above Method is common for both step definition which are written in English and Spanish.

Tags