How does JBehave work with Java?

I have a task for work that I cannot complete because I do not fully receive the toolbox. I have to use JBehave with the Selenium Web Driver to be able to add a specific book to my wishlist on my Amazon account. I have a specific story, and I must use the previously mentioned tools that will be used for "training purposes." I understand that JBehave is the basis for BDD. So, I have some kind of story that I want to check. However, I am confused by the configuration and part of the step definition, which I really don't get. My problem is that I really don't understand how to get all these parts to work together. Where does Selenium WebDriver fit into the equation? Note that I used Selenium with Java, and it was a breeze.

I want to give you an example of a story in the gherkin format, and I would appreciate any ideas on this subject, perhaps an explanation of how all the parts fit together.

Given user <username> with password <password> has a valid amazon.com account And has a wish list And wants to purchase book <title> at a later date When a request to place the book in the wish list is made Then the book is placed in the wish list And the book <title> appears in the wish list when <username> logs in at a later date. 
+5
source share
1 answer

Now that you have your own story, you need your steps. Steps - This is the Java code that will be executed over the history. Each line in your story is mapped to a Java step. See the Documentation for Candidate Steps .

Here's a really easy hit on what your story and steps might look like. But that should at least give you an idea of ​​how stories and steps come together.

History

 Given user username with password passcode is on product page url When the user clicks add to wish list Then the wish list page is displayed And the product title appears on the wish list 

Steps

 public class WishlistSteps { WebDriver driver = null; @BeforeScenario public void scenarioSetup() { driver = new FirefoxDriver; } @Given("user $username with password $passcode is on product page $url") public void loadProduct(String username, String passcode, String url) { doUserLogin(driver, username, passcode); // defined elsewhere driver.get(url); } @When("the user clicks add to wishlist") public void addToWishlist() { driver.findElement(By.class("addToWishlist")).click(); } @Then("the wish list page is displayed") public void isWishlistPage() { assertTrue("Wishlist page", driver.getCurrentUrl().matches(".*/gp/registry/wishlist.*")); } @Then("the product $title appears on the wish list") public void checkProduct(String title) { // check product entries // assert if product not found } @AfterScenario public void afterScenario() { driver.quit(); } } 

Then you need a runner who actually finds and runs the stories. See the documentation for Running History . Below is a very simple runner that will work as a JUnit test.

Runner

 public class JBehaveRunner extends JUnitStories { public JBehaveRunner() { super(); } @Override public injectableStepsFactory stepsFactory() { return new InstanceStepsFactory( configuration(), new WishlistSteps() ); } @Override protected List<String> storyPaths() { return Arrays.asList("stories/Wishlist.story"); } } 

This runner will then be executed as a JUnit test. You can configure your IDE to run it or use Maven or Gradle (depending on your setup).

 mvn test 

I found that the pages below give an excellent overview of the whole setup. And examples from the JBhave repository are also useful.

+2
source

Source: https://habr.com/ru/post/1243101/


All Articles