Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from sbtqa/cucumber-utils
Browse files Browse the repository at this point in the history
Cucumber utils
  • Loading branch information
kosteman authored Apr 3, 2018
2 parents 503653d + 0aaaff8 commit 502ed4c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
24 changes: 20 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<version.commons-lang3>3.3.2</version.commons-lang3>
<version.cucumber>2.4.0</version.cucumber>
<version.junit>4.12</version.junit>
<version.slf4j>1.7.22</version.slf4j>
</properties>
<distributionManagement>
<snapshotRepository>
Expand All @@ -52,27 +56,39 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
<version>${version.commons-lang3}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${version.cucumber}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
<version>${version.slf4j}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
<version>${version.slf4j}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/ru/sbtqa/tag/qautils/cucumber/CucumberUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.sbtqa.tag.qautils.cucumber;

import cucumber.api.Scenario;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.model.CucumberFeature;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CucumberUtils {

private static final Logger LOG = LoggerFactory.getLogger(CucumberUtils.class);
private static final String DEFAULT_LANGUAGE = "en";

private CucumberUtils() {
}

public static Locale getLocale(Scenario scenario) {
List<String> scenarioPaths = new ArrayList<>();
scenarioPaths.add(scenario.getUri());

MultiLoader multiLoader = new MultiLoader(ClassLoader.getSystemClassLoader());
String language = DEFAULT_LANGUAGE;
try {
CucumberFeature cucumberFeature = CucumberFeature.load(multiLoader, scenarioPaths).get(0);
language = cucumberFeature.getGherkinFeature().getFeature().getLanguage();
} catch (Exception e) {
LOG.warn("Error while reading feature with uri {}. Using default language {} as fallback", scenario.getUri(), DEFAULT_LANGUAGE, e);
}

return new Locale(language);
}
}
34 changes: 34 additions & 0 deletions src/test/java/ru/sbtqa/tag/qautils/cucumber/CucumberUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.sbtqa.tag.qautils.cucumber;

import cucumber.api.Scenario;
import cucumber.runtime.ScenarioImpl;
import java.util.Locale;
import org.junit.Assert;
import org.junit.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class CucumberUtilsTest {

@Test
public void returnCorrectLanguageLocaleTest() {
Scenario scenario = mock(ScenarioImpl.class);
when(scenario.getUri()).thenReturn("src/test/resources/features/Correct.feature");
Locale expectedLocale = new Locale("ru");

Locale actualLocale = CucumberUtils.getLocale(scenario);

Assert.assertEquals(expectedLocale, actualLocale);
}

@Test
public void fallbackToDefaultLanguageTest() {
Scenario scenario = mock(ScenarioImpl.class);
when(scenario.getUri()).thenReturn("src/test/resources/features/Empty.feature");
Locale expectedLocale = new Locale("en");

Locale actualLocale = CucumberUtils.getLocale(scenario);

Assert.assertEquals(expectedLocale, actualLocale);
}
}
5 changes: 5 additions & 0 deletions src/test/resources/features/Correct.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#language:ru
Функционал: Тестовая фича

Сценарий: Тестовый сценарий
* открывается страница "Тестовая страница"
4 changes: 4 additions & 0 deletions src/test/resources/features/Empty.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Функционал: Тестовая фича

Сценарий: Тестовый сценарий
* открывается страница "Тестовая страница"

0 comments on commit 502ed4c

Please sign in to comment.