Bruno Pinheiro
Bruno Pinheiro

Reputation: 91

How to use cucumber/gherkin in different language?

I'm setting up a test environment with Cucumber, Sikuli and Eclipse in my company. I've managed to get everything working just fine, but now I need to make the .feature files work in portuguese.

As far as I know, all I had to do was put the comment #language: pt at the beginning of the .feature file, but it's not working.

I also ran the commands cucumber --i18n help and cucumber --i18n pt to check if the language is really there, and it is.

Is there another configuration I'm not aware of?

I'm using these jars:

Upvotes: 6

Views: 10095

Answers (3)

Bruno Pinheiro
Bruno Pinheiro

Reputation: 91

First of all, thanks to everyone who helped me.

After quite some time I finally found out the problem.

Sébastien Le Callonnec's answer on this thread made it clear to me. The problem was with the Natural plugin.

Everything was working correctly from the start but it turns out that Natural only supports english. Thats why it was throwing errors but the test was executing. After uninstalling the plugin the errors disappeared.

Upvotes: 1

Bruno Pinheiro
Bruno Pinheiro

Reputation: 91

I opened the .feature file with a different hex viewer, this seems more correct: enter image description here

The weird part is, I tried to execute the code despite the errors and it worked. I really don't know why. I only changed the keywords to portuguese for testing purposes. The code is showing the errors but executed all the steps as if everything was alright.

enter image description here

The only thing eclipse says about the error is missing 'Feature:' at 'Funcionalidade:'

Upvotes: 0

SubOptimal
SubOptimal

Reputation: 22963

You can control the language of the Gherkin keyword by adding # language: xx as first line.

For Portuguese it would be

# language: pt
Funcionalidade: um exemplo

A list of all supported spoken languages can be found at https://docs.cucumber.io/gherkin/reference/#spoken-languages. The localized keywords you might lookup for example in gherkin-languages.json

A simple Maven project.

structure

pom.xml
src/test/java/features/example.feature
src/test/java/runner/TestRunner.java

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.suboptimal</groupId>
    <artifactId>cuke-test-13.so</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.cucumber>3.0.2</version.cucumber>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${version.cucumber}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${version.cucumber}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

src/test/java/features/example.feature

# language: pt
Funcionalidade: um exemplo

  Cenario: foo
  Dado something given

src/test/java/runner/TestRunner.java

package runner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/java/features/example.feature"
        )
public class TestRunner {
}

running the test with mvn compile test will produce the following error.

Undefined scenarios:
src/test/java/features/example.feature:4 # foo

1 Scenarios (1 undefined)
1 Steps (1 undefined)
0m0.036s


You can implement missing steps with the snippets below:

@Dado("something given")
public void something_given() {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

edit When the feature file is saved on Windows with Notepad as UTF-8 the BOM characters (EF BB BD) are inserted at the beginning.

$ hexdump -C example.feature
00000000 ef bb bf 23 20 6c 61 6e 67 75 61 67 65 3a 20 70 |...# >language: p|

If running mvn test the execution fails with exception.

cucumber.runtime.CucumberException: gherkin.ParserException$CompositeParserException: Parser errors:
(1:1): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got '# language: pt'
(2:1): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Funcionalidade: um exemplo'
(4:3): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Cenario: foo'
(5:3): expected: #EOF, #Language, #TagLine, #FeatureLine, #Comment, #Empty, got 'Dado something given'

Upvotes: 5

Related Questions