View
5
Download
0
Category
Preview:
Citation preview
Setup JBehave In the EclipseBehave Plugin Features
By integrating the jBehave plug-in with Eclipse, you get the following features:
Step auto-completion
Syntax highlighting
Step hyperlink detection and link to corresponding Java step method (Ctrl+click)
Step validation, detecting both unimplemented steps and ambiguous steps
You can also go into the jBehave preferences in Eclipse and change:
Syntax color themes
Story language
Console logger levels
For the following example to work, I’ll assume you already have all the jBehave jar files added to your Libraries area in the project Java Build
Path.
How to install the Eclipse JBehave Plug-In
1. In Eclipse, go to Help>Install New Software
2. Click the Add button
3. In the Add Repository dialog, enter:
1. Name: JBehave
2. Location: jBehave – http://jbehave.org/reference/eclipse/updates/
4. Under the Available Software dialog, click on the jBehave option
1. Click Next>
2. Take defaults for the rest of the prompts
3. Restart Eclipse
JBehave New Story Wizard
To verify that the plugin installed, when you right click on a project and selectNew>Other you should now have an option for a jBehave>New Story wizard.
Quick Example – jBehave Plugin in Action
The best way to get started with the jbehave plugin is to create a quick, simple, project to become familiar with how it works. So let’s create a new project in Eclipse and take a look.
Create a new project
1. In Eclipse, Create a New>Java Project
2. In the New Java Project dialog, enter the project name jBehavePlugIn
3. Click Finish
4. Create two new Packages named:
1. com.joe.steps
2. com.joe.features * jBehave calls features—stories. To stay consistent with Gerkin and Cucumber standards we will instead use the term features.
Create a jBehave story
1. Right click on the com.joe.features package and select New>Other and theJBehave>New story wizard
2. In the Story dialog, name the story File name: debugPlugIn.feature
1. Click Finish
2. You should now have a debugPlugIn.feature file that looks like this in the IDE:
Notice how all the Given, When and Then steps have a yellow triangle with an exclamation point next to them. This shows that there are currently no steps in the project that match the Given, When or Then statements. Clicking on one of the triangles would show a No step is matching message:
If you clicked on one of the Scenario statements using Ctrl+Click, you would get the following message:
Create a Steps Class
Let’s create a step class and add a step definition for the first Given statement.
Right click on the com.joe.steps package and select New>Class
In the New Java Class enter the Name: debugPluginSteps
Click Finish
Open the debugPluginSteps.java class and add the following import statements:
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
Create the following Given method using the first Given stepstep represents a precondition to an event that we have in our debugPlugIn.feature file:
@Given("step represents a precondition to an event")public void debugStepGivenTest(){
}
Save the project and return to the debugPlugin.feature file. Notice how the firstGiven step represents a precondition to an event no longer has a yellow triangle next to it. Hold the control key down on your keyboard and click on theGiven step represents line, and – check it out – it takes you directly to its corresponding Java step!
jBehave Plugin Wrap-up
That’s it. It may not seem like much, but this will be a huge time saver; especially when you start having lots of features and lots of steps. It’s also great to get a visual look at a feature file and be able to automatically see which scenarios still need step definition code defined.
Sample Program.
1.Testsuite code
package test;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Properties;
import org.jbehave.core.Embeddable;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.MostUsefulConfiguration;
import org.jbehave.core.i18n.LocalizedKeywords;
import org.jbehave.core.io.CodeLocations;
import org.jbehave.core.io.LoadFromClasspath;
import org.jbehave.core.io.StoryFinder;
import org.jbehave.core.junit.JUnitStories;
import org.jbehave.core.model.ExamplesTableFactory;
import org.jbehave.core.parsers.RegexPrefixCapturingPatternParser;
import org.jbehave.core.parsers.RegexStoryParser;
import org.jbehave.core.reporters.CrossReference;
import org.jbehave.core.reporters.StoryReporterBuilder;
import org.jbehave.core.steps.InjectableStepsFactory;
import org.jbehave.core.steps.InstanceStepsFactory;
import org.jbehave.core.steps.ParameterConverters;
import org.jbehave.core.steps.ParameterConverters.DateConverter;
import org.jbehave.core.steps.ParameterConverters.ExamplesTableConverter;
import steps.GoogleSteps;
import steps.verifyGoogleImage;
import static org.jbehave.core.io.CodeLocations.codeLocationFromClass;
import static org.jbehave.core.reporters.Format.CONSOLE;
import static org.jbehave.core.reporters.Format.HTML;
import static org.jbehave.core.reporters.Format.TXT;
import static org.jbehave.core.reporters.Format.XML;
/**
* <p>
* {@link Embeddable} class to run multiple textual stories via JUnit.
* </p>
* <p>
* Stories are specified in classpath and correspondingly the {@link LoadFromClasspath} story loader is configured.
* </p>
*/
public class TestSuite extends JUnitStories {
public TestSuite() {
configuredEmbedder().embedderControls().doGenerateViewAfterStories(true).doIgnoreFailureInStories(true)
.doIgnoreFailureInView(true).useThreads(2).useStoryTimeoutInSecs(60);
}
@Override
public Configuration configuration() {
Class<? extends Embeddable> embeddableClass = this.getClass();
// Start from default ParameterConverters instance
ParameterConverters parameterConverters = new ParameterConverters();
// factory to allow parameter conversion and loading from external resources (used by StoryParser too)
ExamplesTableFactory examplesTableFactory = new ExamplesTableFactory(new LocalizedKeywords(), new LoadFromClasspath(embeddableClass), parameterConverters);
// add custom coverters
parameterConverters.addConverters(new DateConverter(new SimpleDateFormat("yyyy-MM-dd")),
new ExamplesTableConverter(examplesTableFactory));
return new MostUsefulConfiguration()
.useStoryLoader(new LoadFromClasspath(embeddableClass))
.useStoryParser(new RegexStoryParser(examplesTableFactory))
.useStoryReporterBuilder(new StoryReporterBuilder()
.withCodeLocation(CodeLocations.codeLocationFromClass(embeddableClass))
.withDefaultFormats()
.withFormats(CONSOLE, TXT, HTML, XML))
.useParameterConverters(parameterConverters);
}
@Override
public InjectableStepsFactory stepsFactory() {
return new InstanceStepsFactory(configuration(), new GoogleSteps(),new verifyGoogleImage());
}
@Override
protected List<String> storyPaths() {
return new StoryFinder().findPaths(codeLocationFromClass(this.getClass()), "**/*.story", "**/excluded*.story");
}
}
2.Test story
sample story
Narrative:In order to communicate effectively to the business some functionalityAs a development teamI want to use Behaviour-Driven Development
Scenario: A sample scenario for Jbeahve with selenium Given I am in FFAnd I navigate to googleThen I verify search field
Scenario: A sample scenario for Jbeahve with selenium Given I am in FFAnd I navigate to googleThen I verify google image
3.Test steps
package steps;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class verifyGoogleImage {
static WebDriver driver;
@Given("I am in FF")
public void invokeBrowser()
{
driver = new FirefoxDriver();
}
@Given("I navigate to google")
public void navigate()
{
driver.get("http://www.google.co.in");
}
@Then("I verify google image")
public void verify()
{// verifying search element in google home page
driver.findElement(By.id("hplogo")).isDisplayed();
driver.quit();
}
}
Paramteriasation
Then validate the Title contains <Etsy>
@Then("validate the Title contains $title") public void verify(String title) {// verifying search element in google home page String Title= driver.getTitle(); if(Title.contains("Etsy")){ Reporter.log("Successfully Navigated the respected page"); } else{ Reporter.log("Not Navigated the respected page"); } }
Recommended