top of page

First Selenium Test And Mapping In Cucumber

​

​

How To Create And Run Selenium Test In Cucumber ?

​

*Before moving ahead let's convert the project in Cucumber project :

Right-click on your project.

Move to an option 'Configure' and click on 'Covert to Cucumber Project'.

​

For creating a selenium test in cucumber, first add selenium dependency in pom.xml of your project :

<dependency>

           <groupId>org.seleniumhq.selenium</groupId>
           <artifactId>selenium-java</artifactId>
           <version>4.0.0</version>

</dependency>

​

Create a java packge in src/main/java as 'stepdefinitions' .

Now create a step-definition class inside the above package as 'Steps.java' as :

​

package stepdefinitions;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import io.cucumber.java.en.Given;

public class Steps {
   
   @Given("^Navigate to google.com$")
   public void navigateToUrl() throws Throwable {

       // Write code here that turns the phrase above into concrete actions
       System.setProperty("webdriver.chrome.driver","..\\CucumberSeleniumProject\\src/main\\resources\\drivers\\chromedriver99.exe");        
       WebDriver driver = new ChromeDriver();
       driver.get("https:\\www.google.com");
       driver.close();

    }

}
 

Create a '.feature' extension file inside 'Features' folder of  the project as 'feature.feature' .

For an example,write below scenario in feature file :

​

Feature: Title of your feature
  I want to use this template for my feature file

Scenario: Navigate to a url
        Given Navigate to google.com


 
*Here,'Scenario' and 'Given' are Gherkin keywords that we will discuss in further articles.

​

Run feature.feature file as 'Cucumber Feature'.

Following output would be displayed :

​

Scenario: Navigate to a url    # Features/feature.feature:4

   Given Navigate to google.com # stepdefinitions.Steps.navigateToUrl()

1 Scenarios (1 passed)
1 Steps (1 passed)
0m37.244s

​

​

Mapping Of Step-Definition Class With Feature File

To achieve mapping ,what we pass in annotaion as argument in step-definition class must be same as the statement of that annotaion in its feature file.

​

In above example,we can see :

In step-definition class -

 @Given("^Navigate to google.com$")

​

And in feature file -

Given Navigate to google.com

​

​

​

​

​

​

Refer next page JUnit Test Runner Class

​

bottom of page