0

I am just starting out with Jbehave Web with WebDriver and wondered whether it was possible to have the same textual step apply to different step methods.

Say for example you have the following two scenarios

Scenario 1

  • Given I am on the properties to buy page
  • When I click Search
  • Then I should see the results page containing all properties to buy

Scenario 2

  • Given I am on the properties to rent page
  • When I click Search
  • Then I should see the results page containing all properties to rent

If I implemented this using the page object pattern I would have a page object called for example buyProperties and likewise for rental properties a page object called something along the lines of rentProperties (as well as result page objects).

In both scenarios a search button/link is clicked so the step text is the same. However, in reality they are on different pages (and page objects).

How could I implement Jbehave so that for the rental scenario it knows to call the step method implementing clicking the search button on the rentProperties page and for the buy scenario it knows to call the step method implementing clicking the search button on the buyProperties page?

Ben
  • 15
  • 3

2 Answers2

0

Your steps class will have two methods - one annotated with @Given("...rent") and one annotated @Given("...buy"). Each method does it's own thing. If "rent" and "buy" is a variable passed in then do different things based on the value of that variable. I'm not sure I get the question...sorry.

Brian Repko
  • 326
  • 2
  • 9
0

Try

@Given ("I am on the properties to $action page")
public void given_i_am_on_the_properties_action_page(@Named("action") String action) {
    if (action.equalsIgnoreCase("Buy") {
        do something;
    }
    if (action.equalsIgnoreCase("Rent") {
        do something;
    }
}

The 'do something' could be setting up the page object for the next steps. Similarly you can use the same method and a variable for your @Then step.

I have used something similar to select menu items and to wait for the page to load before going on to the next step

@When ("I select menu item $menuItem")
public static void when_i_select_menu_item(@Named("menuItem") String menuItem) {
    String item = "";
    String waitFor = "";
    if (menuItem.equalsIgnoreCase("admin")) {
        item = "Admin";
        waitFor = "admin_page";
    }
    if (menuItem.equalsIgnoreCase("home")) {
        item = "Home";
        waitFor = "home_page";
    }
    if (menuItem.equalsIgnoreCase("search")) {
        item = "Search";
        waitFor = "search_page";
    }

    driver.findElement(By.id(item)).click();
    (new WebDriverWait(driver, timeout)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            return element.findElement(By.id(waitFor)).isDisplayed() || d.findElement(By.id(waitFor)).isEnabled();
        }
    });
}