【问题标题】:How to run the Jbehave class through TestNG如何通过 TestNG 运行 Jbehave 类
【发布时间】:2015-12-07 08:24:23
【问题描述】:

我刚刚创建了打开谷歌的功能文件,并在搜索引擎中输入了一些值并验证了搜索结果。此外,我创建了一个包含给定、何时和然后注释的步骤类文件。现在,我想创建一个测试运行器,用于根据故事文件驱动步骤类文件。但就我而言,我想使用 TestNG 而不是 Junit 框架。我在谷歌上搜索了几个站点,都解释了如何集成 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

步骤类文件:

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 框架创建一个测试运行器类文件吗?

【问题讨论】:

    标签: java selenium-webdriver testng jbehave


    【解决方案1】:

    我最近不得不做类似的事情,并惊喜地发现它是 NBD。 See my answer 到另一个 SO JBehave 问题,以获取描述如何设置 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 的 JUnitStoryJUnitStories 使 JBehave 的东西看起来像 JUnit 的东西,所以从 TestNG 的角度来看,它只是运行 JUnit 的东西。需要注意的一件事是在两者之间集成报告。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 2020-03-04
      • 2014-12-10
      • 1970-01-01
      相关资源
      最近更新 更多