简体   繁体   中英

How to run the Jbehave class through TestNG

I just created feature file which is open google and enter some values in search engine and validated the search result. Also, I created a steps class file which contains given, when, and then annotations. Now, I wanna create a test runner which is used to drive the steps class file according to story file. But In my case I wanna use TestNG instead of Junit framework. I googled there are several sites were explained how to integrate the Jbehave+Junit. But So, far I didn't find any site where is the combination of Jbehave+TestNG. Since I'm trying by myself I just need some clarity on this. Can somebody give me a sample code which help me to understand better.

Please find Sample Story:

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

Steps class file:

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

    }

}

Then I need to develop testrunner which used to drive the story file.

Can somebody help me to create a test runner class file using TestNG framework?

I had to do something similar recently and was pleasantly surprised to discover it was NBD. See my answer to another SO JBehave question for sample code describing how to setup JBehave. From there, simply create a testNG suite file for your JBehave tests like so:

<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>

Remember that the JBehave's JUnitStory and JUnitStories make JBehave stuff look like JUnit stuff, so from TestNG's perspective, it's just running JUnit stuff. One thing to watch out for is integrating reporting between the two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM