0

我刚开始使用带有 WebDriver 的 Jbehave Web,想知道是否可以将相同的文本步骤应用于不同的步骤方法。

比如说你有以下两种情况

方案 1

  • 鉴于我在要购买的房产页面上
  • 当我点击搜索
  • 然后我应该看到包含所有要购买的属性的结果页面

方案 2

  • 鉴于我在要出租的物业页面上
  • 当我点击搜索
  • 然后我应该会看到包含所有要出租的房产的结果页面

如果我使用页面对象模式实现这一点,我将拥有一个名为 buyProperties 的页面对象,同样对于出租属性,一个页面对象称为类似rentProperties 的东西(以及结果页面对象)。

在这两种情况下,都会单击搜索按钮/链接,因此步骤文本是相同的。然而,实际上它们位于不同的页面(和页面对象)上。

我怎样才能实现 Jbehave 以便它知道调用 step 方法来实现单击rentProperties 页面上的搜索按钮的租赁场景,而对于购买场景它知道调用实现单击 buyProperties 页面上的搜索按钮的 step 方法?

4

2 回答 2

0

尝试

@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;
    }
}

“做某事”可能是为后续步骤设置页面对象。同样,您可以为 @Then 步骤使用相同的方法和变量。

我使用了类似的东西来选择菜单项并等待页面加载,然后再继续下一步

@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();
        }
    });
}
于 2013-02-13T13:18:57.107 回答
0

您的步骤类将有两种方法 - 一种用 @Given("...rent") 注释,另一种用 @Given("...buy") 注释。每种方法都做自己的事情。如果“rent”和“buy”是传入的变量,则根据该变量的值做不同的事情。我不确定我得到这个问题......对不起。

于 2012-05-09T21:20:35.583 回答