如何通过 TestNG 运行 Jbehave class

How to run the Jbehave class through TestNG

我刚刚创建了打开的特征文件 google 并在搜索引擎中输入了一些值并验证了搜索结果。另外,我创建了一个步骤 class 文件,其中包含 given、when 和 then 注释。现在,我想创建一个测试运行器,用于根据故事文件驱动步骤 class 文件。但就我而言,我想使用 TestNG 而不是 Junit 框架。我 googled 有几个站点解释了如何集成 Jbehave+Junit。但是,到目前为止,我还没有找到 Jbehave+TestNG 组合的站点。由于我是自己尝试的,所以我只需要对此有所了解。谁能给我一个示例代码,帮助我更好地理解。

请查找示例故事:

scenario: Check the google search engine
Given : Open the google home page www.google.com
When : Enter test automation in search box
Then : Proper result should be displayed in results page

步骤 class 文件:

import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GoogleSearchEngine {

    public static WebDriver driver;

    @Given("Open the google home page $url")
    public static void openUrl(String url) throws Exception {
        try {

            driver = new FirefoxDriver();
            driver.get(url);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @When("Enter $searchKeyword in search box")
    public static void searchKeyword(String searchKeyword) throws Exception {
        try {

            driver.findElement(By.xpath(".//*[@id='gs_htif0']")).sendKeys(searchKeyword);
            driver.findElement(By.xpath(".//*[@id='tsf']/center/input[1]")).click();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Then("Proper result should be displayed in results page")
    public static void result() throws Exception {
        try {

            driver.quit();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

然后我需要开发用于驱动故事文件的testrunner。

有人可以帮我使用 TestNG 框架创建一个测试运行程序 class 文件吗?

我最近不得不做类似的事情并且惊喜地发现它是NBD。 See my answer 另一个关于描述如何设置 JBehave 的示例代码的 SO JBehave 问题。从那里,只需为您的 JBehave 测试创建一个 testNG 套件文件,如下所示:

<test name="all JBehave tests">

    <packages>
        <package name="com.foo.tests.jbehave.test1"/>
        <package name="com.foo.tests.jbehave.test2"/>
        <package name="com.foo.tests.jbehave.test3"/>
    </packages>

</test>

请记住,JBehave 的 JUnitStory and JUnitStories 让 JBehave 的东西看起来像 JUnit 的东西,所以从 TestNG 的角度来看,它只是 运行 JUnit 的东西。需要注意的一件事是整合两者之间的报告。