5

I have a task for work that I can't seem to complete because I don't fully get the toolset at hand. I am supposed to use JBehave along with Selenium Web Driver to be able to add a certain book to a wishlist on an amazon account. I have a given story and I supposed to use the previously mentioned tools to be used for "learning purposes". I understand that JBehave is a framework for BDD. So, I have some sort of story that I want to test. However, what confuses me is the configuration and "step definition" part which I don't really get. My problem is I don't really understand how to get all those parts working together. Where does Selenium WebDriver fit in the equation? Note that I have used Selenium with Java and that was a breeze.

I want to give you an example of a story in gherkin format and I would appreciate any insights on this subject matter, maybe a clarification on how all the pieces 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.
Mike Stockdale
  • 5,256
  • 3
  • 29
  • 33
Ralph
  • 2,959
  • 9
  • 26
  • 49
  • Please have a look at link question, you might get some idea how to configure the jbehave selenium java, [link](http://stackoverflow.com/questions/20733703/very-simple-step-by-step-jbehave-setup-tutorial) – SacTan Feb 16 '16 at 09:07
  • Could you explain more what is the objective of this "task for learning puroses" ? Do you want nothing but to implement this one concrete single story using java+webdriver+jbehave, in order to run it on your computer and examine the results ? Or rather do you want to know if this set of tools can be useful in the long run, for testing hundreds of stories, and how to organize the project and the whole testing framework using these tools to easily and efficiently implement and run a lot of tests, display their results and examine a cause of bugs ? – krokodilko Feb 19 '16 at 13:16
  • Wow there's a blast from the past. JBehave is ancient, the last release was in 2006. I would personally look for something a little bit more up to date and well supported. You're going to have issues if anything goes wrong because nobody really uses it any more. – Ardesco Feb 24 '16 at 08:28
  • @Ardesco you are uninformed, the last release was on February 2016 see a changelog: http://jbehave.org/reference/stable/release-notes.html – krokodilko Feb 24 '16 at 20:21
  • Opps, looks like read the date wrong when I checked maven central, it seems I was uninformed, but that's now all changed. – Ardesco Feb 24 '16 at 21:13

1 Answers1

3

Now that you have your Story you need your Steps. The steps are the Java code that will be executed by the story. Each line in your story gets mapped to a Java Step. See the documentation on Candidate Steps.

Here is a really simple stab at what your story and steps might look like. But it should at least give you an idea of how the stories and steps tie together.

Story

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();
  }
}

Next you will need a runner which actually finds and runs the stories. See the documentation on Running Stories. Below is a very simple runner that would run 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 would 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 have found that the pages below provide a great overview of the whole setup. And the examples from the JBhave repository are useful as well.

Brian S.
  • 363
  • 1
  • 14
  • Thanks, but this is the same exact explanation I see everywhere. My problem is after creating all those, how do I actually run them? I can't seem to figure out how to actually run a scenario as such. Like what am I comparing my results do? I cannot seem to get the full picture here. Note that I have read the Running stories on JBehave, but they skip too many explanations that are vital to get the full picture – Ralph Feb 18 '16 at 02:53
  • Ok, you are looking for some examples or a simple write up that ties it all together. I'll post something that may help with that. – Brian S. Feb 18 '16 at 15:42
  • See my updated answer. Hopefully this will be more helpful to you. – Brian S. Feb 18 '16 at 19:12