Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JBehave work with Java?

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.
like image 811
Ralph Avatar asked Oct 18 '22 15:10

Ralph


1 Answers

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.

  • Automated Acceptance-Testing using JBehave
  • JBehave Configuration Tutorial
  • JBehave Examples
like image 189
Brian S. Avatar answered Oct 21 '22 16:10

Brian S.