This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from sbtqa/cucumber-utils
Cucumber utils
- Loading branch information
Showing
5 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/ru/sbtqa/tag/qautils/cucumber/CucumberUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
src/test/java/ru/sbtqa/tag/qautils/cucumber/CucumberUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#language:ru | ||
Функционал: Тестовая фича | ||
|
||
Сценарий: Тестовый сценарий | ||
* открывается страница "Тестовая страница" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Функционал: Тестовая фича | ||
|
||
Сценарий: Тестовый сценарий | ||
* открывается страница "Тестовая страница" |