Skip to main content

Integrate Cucumber Project with TestNG and Selenium

Our whole tutorial is based on Cucumber with JUnit and there are lots of videos we have added on that since JUnit is very popular with Cucumber and used most often with it. But, now let's give it a try with TestNG and configure our project accordingly:

POM.xml: This is our pom.xml, I have tested the code with Cucumber Version 6.9.0 and TestNG version 7.1.0. So, this code might work with your version with little change.

<dependencies>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>${cucumber.version}</version>
    </dependency>
	
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-core</artifactId>
        <version>${cucumber.version}</version>
    </dependency>
	
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>${cucumber.version}</version>
    </dependency>
	
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>${testng.version}</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.141.59</version>
     </dependency>	
</dependencies>

Constants.java (com.pb.cucumbertestng.helper): This class will contain all the constants, you can change according to your configuration for now I have added some constants which I needed, so here we have Constants for glue which is step definition path, tags, features and mac chrome driver:

public class Constants
{
    public static final String GLUE = "com.pb.cucumbertestng.steps";
    public static final String TAGS = "@SmokeTest";
    public static final String FEATURES = "src/test/resources/features";
    public final static String MAC_CHROME_DRIVER = System.getProperty("user.dir") + "//drivers//chromedriverm";
}

TestBase.java (com.pb.cucumbertestng.helper) This file will contain setUpDriver() and closeDriver() method

public class TestBase 
{
    public static WebDriver driver=null;

    public void setUpDriver()
    {
        System.setProperty("webdriver.chrome.driver",Constants.MAC_CHROME_DRIVER);
        ChromeOptions chromeOptions = new ChromeOptions();
        driver = new ChromeDriver(chromeOptions);
       
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }
    
    public void closeDriver()
    {
        driver.quit();
    }
}

TestRunner.java (com.pb.cucumbertestng.helper): Here for now I have just specified tags, glue and features Cucumber Options, remaining we will add when needed.

@CucumberOptions( 
        tags = (Constants.TAGS),
        glue = {Constants.GLUE}, 
        features = {Constants.FEATURES})

public class TestRunner extends AbstractTestNGCucumberTests 
{	

}

Then you can see our TestRunner is extending AbstractTestNGCucumberTests, this will runs cucumber every detected feature as a seprate test.

Hooks.java (com.pb.cucumbertestng.steps): Here we are calling methods setUpDriver and closeDriver, which we have declared in TestBase.java after creating an object of TestBase.

public class Hooks 
{
    TestBase tb = new TestBase();
	
    @Before
    public void before() 
    {
        tb.setUpDriver();		
    }	

    @After
    public void after()
    {
        tb.closeDriver();
    }
}

Ok, so this was just basic configuration and upcoming lecture we will do more configuration and also run our first test with TestNG. 

Submitted by tgoswami on February 17, 2021

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.