selenium-webdriver - JBehave 如何与 Java 一起工作?

标签 selenium-webdriver bdd jbehave

我有一项似乎无法完成的工作任务,因为我没有完全掌握手头的工具集。我应该将 JBehave 与 Selenium Web Driver 一起使用,以便能够将某本书添加到亚马逊帐户的愿望 list 中。我有一个给定的故事,我应该使用前面提到的工具用于“学习目的”。我知道 JBehave 是 BDD 的框架。所以,我有一些我想测试的故事。然而,让我感到困惑的是我并没有真正理解的配置和“步骤定义”部分。我的问题是我真的不明白如何让所有这些部分协同工作。 Selenium WebDriver 在哪里适合等式?请注意,我已经将 Selenium 与 Java 一起使用,这很容易。

我想给你一个小 cucumber 格式的故事的例子,我很感激任何关于这个主题的见解,也许是关于所有部分如何组合在一起的澄清。

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.

最佳答案

现在你有了你的故事,你需要你的步骤。这些步骤是故事将要执行的 Java 代码。故事中的每一行都映射到一个 Java 步骤。请参阅 Candidate Steps 上的文档.

这里有一个非常简单的例子,说明您的故事和步骤可能是什么样子。但它至少应该让您了解故事和步骤如何联系在一起。

故事

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 

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

接下来,您将需要一个实际查找并运行故事的运行程序。请参阅 Running Stories 上的文档.下面是一个非常简单的运行器,它将作为 JUnit 测试运行。

亚军
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");
  }
}

然后这个运行器将作为 JUnit 测试执行。您可以配置您的 IDE 来运行它,或者使用 Maven 或 Gradle(取决于您的设置)。
mvn test

我发现下面的页面提供了整个设置的一个很好的概述。 JBhave 存储库中的示例也很有用。
  • Automated Acceptance-Testing using JBehave
  • JBehave Configuration Tutorial
  • JBehave Examples
  • 关于selenium-webdriver - JBehave 如何与 Java 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35423268/

    相关文章:

    python - 点击滑溜溜的 "ElementNotVisibleException"按钮selenium webdriver python

    selenium - Selenium 4 alpha 中的 sendDevToolsCommand

    testing - Gherkin 'Then' 语句是否应该引用 'I'

    javascript - 如何在 Mocha 测试中模拟时间的流逝,以便调用 setTimeout 回调?

    rest - 在 API 自动化测试中使用 BDD 是一个好方法吗?

    java - 如何使用 Serenity、JBehave 和 Selenium 设置配置的嵌入器以使用元过滤器 (-skip)

    java - Selenium 如何多次等待具有不同文本的同一个 WebElement

    java - 将 PhantomJS 二进制文件添加到 Maven 项目的更好方法?

    java - JBehave-Junit-Runner 抛出 NullPointerException

    java - 运行 JBehave 的 FileNotFoundException(Maven 项目、IntelliJ)