Cucumber with TestNG & Selenium (Part-2) – Create Tests

Objective

In this post, we will discuss about how to create tests using Cucumber with TestNG & Selenium. Creating Feature file, Step Definition class, Test Runner class and executing the test case using TestNG.

Configuration

  • Create a maven project and add the below dependencies for Cucumber, TestNG and Selenium.
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>4.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>4.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.14.3</version>
    <scope>test</scope>
</dependency>

Install Cucumber-Eclipse plugin

  • Install Cucumber-Eclipse plugin from here.

Create a feature file

  • In your project, locate the package, right click and navigate to New –> File

  • Now In new file window, enter the filename.feature and click on Finish button.

  • After clicking on Finish button, you can observe the test.feature file was created and automatically it was displayed with Cucumber symbol and the basic features with comments were pre-loaded to that feature file. This happens because of Cucumber-Eclipse plugin.

  • Add the actual features in test.features file.

Create Step Definitions

  • For creating step definitions easily, you should install Tidy Gherkin plugin in chrome.
  • After installing Tidy Gherkin, launch the Tidy Gherkin plugin.

  • Now, copy your feature set and paste at “Enter your Gherkin here…

  • Now, click on ‘JAVA STEPS‘ tab and observe that the basic step definition methods were created.

  • Now, copy the highlighted part and create the step definition class file and paste the same. And write the Selenium code to launch the browser, authenticating the user and validating the Home screen.
public class TestStepDefinition {
  WebDriver driver;
  @Given("I want to launch the facebook in firefox browser")
  public void i_want_to_launch_the_facebook_in_firefox_browser() {
    System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\Drivers\\geckodriver.exe");
    driver = new FirefoxDriver();
    driver.get("http://facebook.com");
    }

  @When("I want to authenticate with valid credentails")
  public void i_want_to_authenticate_with_valid_credentails() {
    driver.findElement(By.id("email")).sendKeys("username");
    driver.findElement(By.id("pass")).sendKeys("password");
    driver.findElement(By.xpath("//*[@id=\"loginbutton\"]")).click();
    
  }

  @Then("I want to verify whether user navigated to home page or not")
  public void i_want_to_verify_whether_user_navigated_to_home_page_or_not() {
      Assert.assertEquals(driver.getTitle(), "Home");
  }
}

Create Test Runner Class

  • Now, create the TestRunner class file with the below code.

Note: If we are using TestNG, then we need to extend the TestRunner class to AbstractTestNGCucumberTests.

@CucumberOptions(
    features="<Path of the Feature files>",
    glue="<Path of the Step Definition files>",
    plugin= {"pretty","html:HTML-Reports"},
    monochrome=true
    )
public class TestRunner extends AbstractTestNGCucumberTests{

}

We need to arrange the features, step definitions and Test Runner classes/files in proper packaging.

Execute the Tests

Now run the Test Runner class as TestNG test and should be able to run your tests using Cucumber with TestNG & Selenium.

Download code


About the course

NPN Training is counted among the top ten Selenium Training in Bangalore for providing the best Selenium training in Bangalore. Our selenium course content and syllabus is designed according to the industry requirement to accomplish everyone’s career goal.

 

Related Post
Cucumber with TestNG & Selenium (Part-1) ̵... Objective In this post, will discuss about What is Behavior Driven Development, Importance of Cucumber, Feature file, Step definition and Cucumber Op...

Pradeep

M.r Pradeep is QA expert working as a Research Analyst at NPN Training. He is having 12 years of experience in Quality Assurance with extensive of 4 years in Automation experience (Selenium, UFT, Rest Assured, Jmeter & Soap UI). He has helped the peers/students to understand and learn Automation with Selenium and API automation with Rest Assured with Hands-On approach.