3

I have jbehave integrated with Selenium. I am running my tests through command line as below C:\eclipse_workspace\MySeleniumTests>mvn clean test -Dwebdriver.firefox.bin="C:\Program Files\Mozilla\Firefox\firefox.exe"

I have used jbehave-maven-plugin. Maven picks up all the Embedder impl (JunitStories in my case) from the source directory and execute them one by one. Configuration for that is <include>**/*Stories.java</include> in pom.xml

It then looks for relevant .story files in the specified dir and executes them. Say, I have two story files one.story and two.story, both of them are executed.

Over a time, number of story files are going to increase I only want to execute specific story files should there be a way to do this? I am thinking to pass specific story file names as run time parameters but don’t know what is required to make that happen.

Lt_Shade
  • 590
  • 1
  • 7
  • 18
Kaizar Laxmidhar
  • 859
  • 1
  • 17
  • 38

2 Answers2

4

I got it working with the below code

mvn clean test -Dwebdriver.firefox.bin="C:\Program Files\Mozilla\Firefox\firefox.exe" -Dstory=myStory.story

Override storyPaths() method in embedder class as below.

public class MyTestStories extends JUnitStories /* InjectableEmbedder */{

    @Override
    protected List<String> storyPaths() {
        List<String> storiesToRun = new ArrayList<String>();
        String storyProperty = System.getProperty("story");

        if (storyProperty == null || storyProperty.isEmpty()) {
           throw new RuntimeException("Please specify which stories to run");
        }

        String[] storyNames = storyProperty.split(",");
        StoryFinder sf = new StoryFinder();
        URL baseUrl = CodeLocations.codeLocationFromClass(this.getClass());

        for (String storyName : storyNames) {
           storiesToRun.addAll(sf.findPaths(baseUrl, storyName, ""));
        }

        return storiesToRun;
    }
Kaizar Laxmidhar
  • 859
  • 1
  • 17
  • 38
  • i tried your code but its returning storiesToRun size as 0. any idea y i am getting that? – SacTan Apr 13 '16 at 19:27
  • @Kaizer, I am using JBehaveRunnerStories extends JUnitStories { } AND override the Configuration configuration(), InjectableStepsFactory stepsFactory() and the method you provided. Its take the stories from parameter from run configuration but when it came at for loop for (String storyName : storyNames), loop executed two times as i have passed only two stories but when its return storiesToRun, it has zero count/size. – SacTan Apr 15 '16 at 13:19
  • if it is possible then can i share my code with you...my email id is stvpr_sachin@yahoo.in – SacTan Apr 15 '16 at 13:22
  • It could be possible that the .story files in your project are not at the correct location hence not in the classpath therefore the StoryFinder might not be able to find them. Can you share your project structure? – Kaizar Laxmidhar Apr 15 '16 at 13:53
  • Could you please help me, Whare and what I have to check that so that I can identify the cause..because of security reasons i can not share code here..I can mail you that, my email id is stvpr_sachin@yahoo.in..Thank you in advance. – SacTan Apr 15 '16 at 15:24
  • @SachinB not the code just the project structure, with .story files location. – Kaizar Laxmidhar Apr 15 '16 at 15:30
  • @Kaizer, I have maven project set up and story files under src/test/resources/stories package. – SacTan Apr 15 '16 at 15:34
  • if you have any working code(sample project) for this, it will be great help if you share that.. – SacTan Apr 16 '16 at 12:40
  • Sorry don't have the code now with me as I changed the job. – Kaizar Laxmidhar Apr 18 '16 at 09:59
1

Try the following:

mvn clean test -Dwebdriver.firefox.bin="C:\Program Files\Mozilla\Firefox\firefox.exe" -Djbehave.story.name=<story filename without extension (wildcards are supported)>

You should also use custom test suite implementation:

public abstract class JBehaveTestSuite extends ThucydidesJUnitStories {

    private static final String STORY_NAME_PATTERN = "**/${jbehave.story.name:*}.story";

    public JBehaveTestSuite() {
        findStoriesCalled(storyNamesFromEnvironmentVariable());
    }

    @Override
    public void run() throws Throwable {
        super.run();
    }

    private String storyNamesFromEnvironmentVariable() {
        return SystemPropertyUtils.resolvePlaceholders(STORY_NAME_PATTERN);
    }
}
ydrozhdzhal
  • 154
  • 1
  • 5